# diogofrj/mcp-turnkey architecture

> Architecture diagram of the GitHub repository diogofrj/mcp-turnkey, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-11). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/diogofrj/mcp-turnkey
- Repository: https://github.com/diogofrj/mcp-turnkey

## Overview

MCP Turnkey is both a project scaffold and a Python MCP server template. Its hosted workflow registers an MCP client, obtains user consent for upstream OAuth, stores that user’s credential, then serves tenant-scoped tools using the client’s validated credential. Local stdio login is a separate path. The graph preserves documented security, storage, rate-limit, and deployment capabilities; several internal adapter connections are omitted where the excerpts do not establish their runtime calls.

Main entry points: [`template/src/mcp_bootstrap/container.py`](https://github.com/diogofrj/mcp-turnkey/blob/main/template/src/mcp_bootstrap/container.py) (Composition roots), [`template/src/mcp_bootstrap/asgi.py`](https://github.com/diogofrj/mcp-turnkey/blob/main/template/src/mcp_bootstrap/asgi.py) (HTTP routes), [`template/src/mcp_bootstrap/application/auth.py`](https://github.com/diogofrj/mcp-turnkey/blob/main/template/src/mcp_bootstrap/application/auth.py) (Local login), [`template/src/mcp_bootstrap/infrastructure/supabase_token_store.py`](https://github.com/diogofrj/mcp-turnkey/blob/main/template/src/mcp_bootstrap/infrastructure/supabase_token_store.py) (Encrypted token storage)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_scaffold["Project setup"]
  node_scaffolder["Project scaffold<br/>[scaffold.py]"]
  node_generated["Generated server"]
end

subgraph group_transport["MCP transport"]
  node_asgi["HTTP routes<br/>[asgi.py]"]
  node_mcp_server["MCP tools<br/>[server.py]"]
  node_container["Composition roots<br/>[container.py]"]
  node_config["Server configuration<br/>[config.py]"]
  node_middleware["Request hardening<br/>[middleware.py]"]
  node_logging["Structured logging<br/>[logging.py]"]
end

subgraph group_oauth["OAuth and tenancy"]
  node_consent["Consent flow<br/>[consent.py]"]
  node_authserver["OAuth server rules<br/>[authserver.py]"]
  node_auth["Local login<br/>[auth.py]"]
  node_token_access["Token lifecycle<br/>[token_access.py]"]
  node_domain["Identity and token models<br/>[models.py]"]
  node_ports["Application ports<br/>[ports.py]"]
  node_oauth_client["Provider OAuth client<br/>[oauth_client.py]"]
  node_rate_limit["OAuth rate limits<br/>[rate_limit.py]"]
end

subgraph group_tools["Tools and upstream access"]
  node_items["Example item tools<br/>[items.py]"]
  node_upstream_api["Upstream API adapter<br/>[upstream_client.py]"]
end

subgraph group_adapters["Infrastructure"]
  node_supabase["Supabase access<br/>[supabase_client.py]"]
  node_token_store[("Encrypted token storage")]
  node_crypto["AES-GCM encryption<br/>[crypto_aesgcm.py]"]
  node_auth_store["OAuth state storage"]
  node_cache["Rate-limit cache<br/>[upstash_cache.py]"]
end

node_operator(("Project creator"))
node_mcp_client(("MCP client"))
node_browser(("User browser"))
node_provider{{"OAuth provider"}}

node_operator -->|"runs scaffold"| node_scaffolder
node_scaffolder -->|"copies template"| node_generated
node_mcp_client -->|"connects over HTTP"| node_asgi
node_browser -->|"follows OAuth routes"| node_asgi
node_asgi -->|"routes consent"| node_consent
node_asgi -->|"routes OAuth"| node_authserver
node_asgi -->|"applies limits"| node_rate_limit
node_asgi -->|"resolves services"| node_container
node_asgi -->|"serves MCP"| node_mcp_server
node_asgi -->|"installs middleware"| node_middleware
node_mcp_server -->|"calls tool logic"| node_items
node_container -->|"wires local login"| node_auth
node_container -->|"wires token access"| node_token_access
node_container -->|"builds API adapter"| node_upstream_api
node_container -->|"builds OAuth client"| node_oauth_client
node_container -->|"builds token store"| node_token_store
node_container -->|"builds cipher"| node_crypto
node_container -->|"builds auth store"| node_auth_store
node_container -->|"builds cache"| node_cache
node_items -->|"requests resources"| node_upstream_api
node_upstream_api -->|"calls API"| node_provider
node_oauth_client -->|"exchanges credentials"| node_provider
node_consent -->|"uses OAuth rules"| node_authserver
node_auth -->|"exchanges login"| node_oauth_client
node_auth -->|"updates token model"| node_domain
node_token_access -->|"manages credentials"| node_domain
node_token_store -->|"reads and writes"| node_supabase
node_token_store -->|"seals credentials"| node_crypto
node_auth_store -->|"persists OAuth state"| node_supabase
node_rate_limit -->|"counts requests"| node_cache
node_asgi -->|"loads settings"| node_config
node_middleware -->|"logs requests"| node_logging
node_provider -->|"redirects after login"| node_browser

click node_scaffolder "https://github.com/diogofrj/mcp-turnkey/blob/main/scripts/scaffold.py"
click node_generated "https://github.com/diogofrj/mcp-turnkey/tree/main/template/src/mcp_bootstrap"
click node_asgi "https://github.com/diogofrj/mcp-turnkey/blob/main/template/src/mcp_bootstrap/asgi.py"
click node_mcp_server "https://github.com/diogofrj/mcp-turnkey/blob/main/template/src/mcp_bootstrap/server.py"
click node_container "https://github.com/diogofrj/mcp-turnkey/blob/main/template/src/mcp_bootstrap/container.py"
click node_config "https://github.com/diogofrj/mcp-turnkey/blob/main/template/src/mcp_bootstrap/config.py"
click node_middleware "https://github.com/diogofrj/mcp-turnkey/blob/main/template/src/mcp_bootstrap/middleware.py"
click node_consent "https://github.com/diogofrj/mcp-turnkey/blob/main/template/src/mcp_bootstrap/application/consent.py"
click node_authserver "https://github.com/diogofrj/mcp-turnkey/blob/main/template/src/mcp_bootstrap/application/authserver.py"
click node_auth "https://github.com/diogofrj/mcp-turnkey/blob/main/template/src/mcp_bootstrap/application/auth.py"
click node_token_access "https://github.com/diogofrj/mcp-turnkey/blob/main/template/src/mcp_bootstrap/application/token_access.py"
click node_domain "https://github.com/diogofrj/mcp-turnkey/blob/main/template/src/mcp_bootstrap/domain/models.py"
click node_ports "https://github.com/diogofrj/mcp-turnkey/blob/main/template/src/mcp_bootstrap/domain/ports.py"
click node_items "https://github.com/diogofrj/mcp-turnkey/blob/main/template/src/mcp_bootstrap/application/items.py"
click node_upstream_api "https://github.com/diogofrj/mcp-turnkey/blob/main/template/src/mcp_bootstrap/infrastructure/upstream_client.py"
click node_oauth_client "https://github.com/diogofrj/mcp-turnkey/blob/main/template/src/mcp_bootstrap/infrastructure/oauth_client.py"
click node_supabase "https://github.com/diogofrj/mcp-turnkey/blob/main/template/src/mcp_bootstrap/infrastructure/supabase_client.py"
click node_token_store "https://github.com/diogofrj/mcp-turnkey/blob/main/template/src/mcp_bootstrap/infrastructure/supabase_token_store.py"
click node_crypto "https://github.com/diogofrj/mcp-turnkey/blob/main/template/src/mcp_bootstrap/infrastructure/crypto_aesgcm.py"
click node_auth_store "https://github.com/diogofrj/mcp-turnkey/blob/main/template/src/mcp_bootstrap/infrastructure/authserver_store.py"
click node_cache "https://github.com/diogofrj/mcp-turnkey/blob/main/template/src/mcp_bootstrap/infrastructure/upstash_cache.py"
click node_rate_limit "https://github.com/diogofrj/mcp-turnkey/blob/main/template/src/mcp_bootstrap/application/rate_limit.py"
click node_logging "https://github.com/diogofrj/mcp-turnkey/blob/main/template/src/mcp_bootstrap/observability/logging.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_scaffolder,node_generated,node_mcp_client,node_browser toneBlue
class node_asgi,node_mcp_server,node_container,node_config,node_middleware,node_logging toneAmber
class node_consent,node_authserver,node_auth,node_token_access,node_domain,node_ports,node_oauth_client,node_rate_limit toneMint
class node_items,node_upstream_api toneRose
class node_supabase,node_token_store,node_crypto,node_auth_store,node_cache,node_operator,node_provider toneIndigo
```

## Components

### Project setup

- [Project scaffold](https://github.com/diogofrj/mcp-turnkey/blob/main/scripts/scaffold.py): `scripts/scaffold.py`
- [Generated server](https://github.com/diogofrj/mcp-turnkey/tree/main/template/src/mcp_bootstrap): `template/src/mcp_bootstrap`

### MCP transport

- [HTTP routes](https://github.com/diogofrj/mcp-turnkey/blob/main/template/src/mcp_bootstrap/asgi.py): `template/src/mcp_bootstrap/asgi.py`
- [MCP tools](https://github.com/diogofrj/mcp-turnkey/blob/main/template/src/mcp_bootstrap/server.py): `template/src/mcp_bootstrap/server.py`
- [Composition roots](https://github.com/diogofrj/mcp-turnkey/blob/main/template/src/mcp_bootstrap/container.py): `template/src/mcp_bootstrap/container.py`
- [Server configuration](https://github.com/diogofrj/mcp-turnkey/blob/main/template/src/mcp_bootstrap/config.py): `template/src/mcp_bootstrap/config.py`
- [Request hardening](https://github.com/diogofrj/mcp-turnkey/blob/main/template/src/mcp_bootstrap/middleware.py): `template/src/mcp_bootstrap/middleware.py`
- [Structured logging](https://github.com/diogofrj/mcp-turnkey/blob/main/template/src/mcp_bootstrap/observability/logging.py): `template/src/mcp_bootstrap/observability/logging.py`

### OAuth and tenancy

- [Consent flow](https://github.com/diogofrj/mcp-turnkey/blob/main/template/src/mcp_bootstrap/application/consent.py): `template/src/mcp_bootstrap/application/consent.py`
- [OAuth server rules](https://github.com/diogofrj/mcp-turnkey/blob/main/template/src/mcp_bootstrap/application/authserver.py): `template/src/mcp_bootstrap/application/authserver.py`
- [Local login](https://github.com/diogofrj/mcp-turnkey/blob/main/template/src/mcp_bootstrap/application/auth.py): `template/src/mcp_bootstrap/application/auth.py`
- [Token lifecycle](https://github.com/diogofrj/mcp-turnkey/blob/main/template/src/mcp_bootstrap/application/token_access.py): `template/src/mcp_bootstrap/application/token_access.py`
- [Identity and token models](https://github.com/diogofrj/mcp-turnkey/blob/main/template/src/mcp_bootstrap/domain/models.py): `template/src/mcp_bootstrap/domain/models.py`
- [Application ports](https://github.com/diogofrj/mcp-turnkey/blob/main/template/src/mcp_bootstrap/domain/ports.py): `template/src/mcp_bootstrap/domain/ports.py`
- [Provider OAuth client](https://github.com/diogofrj/mcp-turnkey/blob/main/template/src/mcp_bootstrap/infrastructure/oauth_client.py): `template/src/mcp_bootstrap/infrastructure/oauth_client.py`
- [OAuth rate limits](https://github.com/diogofrj/mcp-turnkey/blob/main/template/src/mcp_bootstrap/application/rate_limit.py): `template/src/mcp_bootstrap/application/rate_limit.py`

### Tools and upstream access

- [Example item tools](https://github.com/diogofrj/mcp-turnkey/blob/main/template/src/mcp_bootstrap/application/items.py): `template/src/mcp_bootstrap/application/items.py`
- [Upstream API adapter](https://github.com/diogofrj/mcp-turnkey/blob/main/template/src/mcp_bootstrap/infrastructure/upstream_client.py): `template/src/mcp_bootstrap/infrastructure/upstream_client.py`

### Infrastructure

- [Supabase access](https://github.com/diogofrj/mcp-turnkey/blob/main/template/src/mcp_bootstrap/infrastructure/supabase_client.py): `template/src/mcp_bootstrap/infrastructure/supabase_client.py`
- [Encrypted token storage](https://github.com/diogofrj/mcp-turnkey/blob/main/template/src/mcp_bootstrap/infrastructure/supabase_token_store.py): `template/src/mcp_bootstrap/infrastructure/supabase_token_store.py`
- [AES-GCM encryption](https://github.com/diogofrj/mcp-turnkey/blob/main/template/src/mcp_bootstrap/infrastructure/crypto_aesgcm.py): `template/src/mcp_bootstrap/infrastructure/crypto_aesgcm.py`
- [OAuth state storage](https://github.com/diogofrj/mcp-turnkey/blob/main/template/src/mcp_bootstrap/infrastructure/authserver_store.py): `template/src/mcp_bootstrap/infrastructure/authserver_store.py`
- [Rate-limit cache](https://github.com/diogofrj/mcp-turnkey/blob/main/template/src/mcp_bootstrap/infrastructure/upstash_cache.py): `template/src/mcp_bootstrap/infrastructure/upstash_cache.py`

### Other components

- **Project creator**
- **MCP client**
- **User browser**
- **OAuth provider**

## Connections

- Project creator → Project scaffold: runs scaffold (evidence: `README.md`)
- Project scaffold → Generated server: copies template (evidence: `README.md`)
- MCP client → HTTP routes: connects over HTTP (evidence: `README.md`)
- User browser → HTTP routes: follows OAuth routes (evidence: `README.md`)
- HTTP routes → Consent flow: routes consent (evidence: `template/src/mcp_bootstrap/asgi.py`)
- HTTP routes → OAuth server rules: routes OAuth (evidence: `template/src/mcp_bootstrap/asgi.py`)
- HTTP routes → OAuth rate limits: applies limits (evidence: `template/src/mcp_bootstrap/asgi.py`)
- HTTP routes → Composition roots: resolves services (evidence: `template/src/mcp_bootstrap/asgi.py`)
- HTTP routes → MCP tools: serves MCP (evidence: `template/src/mcp_bootstrap/asgi.py`)
- HTTP routes → Request hardening: installs middleware (evidence: `template/src/mcp_bootstrap/asgi.py`)
- MCP tools → Example item tools: calls tool logic (evidence: `template/src/mcp_bootstrap/server.py`)
- Composition roots → Local login: wires local login (evidence: `template/src/mcp_bootstrap/container.py`)
- Composition roots → Token lifecycle: wires token access (evidence: `template/src/mcp_bootstrap/container.py`)
- Composition roots → Upstream API adapter: builds API adapter (evidence: `template/src/mcp_bootstrap/container.py`)
- Composition roots → Provider OAuth client: builds OAuth client (evidence: `template/src/mcp_bootstrap/container.py`)
- Composition roots → Encrypted token storage: builds token store (evidence: `template/src/mcp_bootstrap/container.py`)
- Composition roots → AES-GCM encryption: builds cipher (evidence: `template/src/mcp_bootstrap/container.py`)
- Composition roots → OAuth state storage: builds auth store (evidence: `template/src/mcp_bootstrap/container.py`)
- Composition roots → Rate-limit cache: builds cache (evidence: `template/src/mcp_bootstrap/container.py`)
- Example item tools → Upstream API adapter: requests resources
- Upstream API adapter → OAuth provider: calls API (evidence: `README.md`)
- Provider OAuth client → OAuth provider: exchanges credentials (evidence: `template/src/mcp_bootstrap/infrastructure/oauth_client.py`)
- Consent flow → OAuth server rules: uses OAuth rules (evidence: `template/src/mcp_bootstrap/application/authserver.py`)
- Local login → Provider OAuth client: exchanges login (evidence: `template/src/mcp_bootstrap/application/auth.py`)
- Local login → Identity and token models: updates token model (evidence: `template/src/mcp_bootstrap/application/auth.py`)
- Token lifecycle → Identity and token models: manages credentials (evidence: `template/src/mcp_bootstrap/application/token_access.py`)
- Encrypted token storage → Supabase access: reads and writes (evidence: `template/src/mcp_bootstrap/infrastructure/supabase_token_store.py`)
- Encrypted token storage → AES-GCM encryption: seals credentials (evidence: `README.md`)
- OAuth state storage → Supabase access: persists OAuth state (evidence: `template/src/mcp_bootstrap/infrastructure/authserver_store.py`)
- OAuth rate limits → Rate-limit cache: counts requests (evidence: `template/src/mcp_bootstrap/application/rate_limit.py`)
- HTTP routes → Server configuration: loads settings (evidence: `template/src/mcp_bootstrap/asgi.py`)
- Request hardening → Structured logging: logs requests (evidence: `template/src/mcp_bootstrap/middleware.py`)
- OAuth provider → User browser: redirects after login (evidence: `README.md`)
