# iannuttall/mcp-boilerplate architecture

> Architecture diagram of the GitHub repository iannuttall/mcp-boilerplate, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-21). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/iannuttall/mcp-boilerplate
- Repository: https://github.com/iannuttall/mcp-boilerplate

## Overview

##### Project Type & Purpose
- This is a serverless, full-stack MCP (Model Context Protocol) server running on Cloudflare Workers.
- It provides:
– AI “tools” (free and paid) via an SSE endpoint (/sse) for Cursor, Claude, etc.
– OAuth login (Google or GitHub) backed by Cloudflare KV (“OAUTH_KV”).
– Stripe payment flows (one-time, metered, subscription).
– A few static pages (index.html, payment-success.html).

##### Core Components
A. Client Layer
- Users via browser (for login and static pages)
- AI Assistants (Claude Desktop, Cloudflare AI Playground, MCP Inspector) connecting to /sse
B. Cloudflare Worker
- Entrypoint (src/index.ts) mounting routes:
– / (serves index.html)
– /callback/google, /callback/github (OAuth handlers)
– /sse (SSE tool API)
– /payment/success (serves payment-success.html)
– /webhooks/stripe (Stripe webhooks handler)
- Submodules:
– src/auth: GitHubHandler, GoogleHandler, OAuth orchestration
– src/tools: individual tool functions (add.ts, calculate.ts, subscriptionAdd.ts, etc.)
– src/helpers/constants.ts (shared values)
– src/webhooks/stripe.ts (optional webhook processing)
C. Data Stores & Config
- Cloudflare KV namespace “OAUTH_KV” for session and auth state
- Wrangler config (wrangler.jsonc) & .dev.vars for environment secrets
D. External Services
- OAuth Providers: Google OAuth2, GitHub OAuth2
- Stripe API for checkout, subscription, metered billing, Customer Billing Portal
E. Logging & Observability (optional)
- Wrangler logs, Cloudflare dashboard

##### Interactions & Data Flows
1. User visits root URL → Worker serves index.html
2. User clicks “Login” → redirected to Google/GitHub → callback hits Worker → Handler writes to KV → sets encrypted cookie
3. AI assistant connects to /sse → Worker authenticates via cookie + KV → dispatches tool calls
4. Free tool request → Worker executes src/tools/* → returns SSE messages
5. Paid tool request → Worker checks Stripe payment status → if unpaid, generates Stripe Checkout session → redirect/URL sent back
6. On payment complete → Stripe redirects to /payment/success (static) → Worker displays confirmation
7. Optionally, Stripe webhook /webhooks/stripe → updates subscription or metered usage logs if implemented

##### Architectural Patterns & Principles
- Serverless micro-services style: each route is a function in index.ts
- Agent pattern: tools are registered via a common “agent” API (experimental_PaidMcpAgent)
- Separation of concerns: auth, business logic (tools), config, and webhooks each in their own directory
- Event-driven: SSE for real-time tool responses, webhooks for asynchronous Stripe events

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Clients
    subgraph "Clients"
        Browser(("Browser")):::frontend
        AIAssistants(("AI Assistants")):::frontend
    end

    %% Static Pages
    subgraph "Static Pages"
        IndexHTML["index.html"]:::static
        SuccessHTML["payment-success.html"]:::static
    end

    %% Cloudflare Worker
    subgraph "Cloudflare Worker"
        IndexEntrypoint["Worker Entrypoint (src/index.ts)"]:::worker

        subgraph "Auth Module"
            GHHandler["GitHubHandler"]:::auth
            GHandler["GoogleHandler"]:::auth
            OAuthOrch["OAuth Orchestration"]:::auth
            OAuthUtils["Workers OAuth Utils"]:::auth
        end

        subgraph "Tools Module"
            ToolsIndex["tools/index.ts"]:::tools
            AddTool["add.ts"]:::tools
            CalcTool["calculate.ts"]:::tools
            OneTimeTool["onetimeAdd.ts"]:::tools
            MeteredTool["meteredAdd.ts"]:::tools
            SubsTool["subscriptionAdd.ts"]:::tools
            CheckHistory["checkPaymentHistory.ts"]:::tools
        end

        subgraph "Webhooks Module"
            StripeWebhook["stripe.ts"]:::payment
        end

        KV["OAUTH_KV"]:::datastore
        Constants["constants.ts"]:::common
        Config["wrangler.jsonc"]:::config
        DevVars[".dev.vars.example"]:::config
    end

    %% External Services
    subgraph "External Services"
        Google["Google OAuth2"]:::external
        GitHubExt["GitHub OAuth2"]:::external
        StripeAPI["Stripe API"]:::external
    end

    %% Flows
    Browser -->|"GET /"| IndexHTML
    IndexHTML -->|"serve"| IndexEntrypoint

    Browser -->|"Login click"| Google
    Browser -->|"Login click"| GitHubExt
    Google -->|"HTTPS GET /callback/google"| IndexEntrypoint
    GitHubExt -->|"HTTPS GET /callback/github"| IndexEntrypoint

    IndexEntrypoint --> OAuthOrch
    OAuthOrch --> KV
    OAuthOrch -->|"set encrypted cookie"| Browser

    AIAssistants -->|"SSE GET /sse"| IndexEntrypoint
    IndexEntrypoint --> ToolsIndex
    ToolsIndex --> AddTool
    ToolsIndex --> CalcTool
    ToolsIndex --> OneTimeTool
    ToolsIndex --> MeteredTool
    ToolsIndex --> SubsTool
    ToolsIndex --> CheckHistory

    AddTool -->|"SSE msg"| AIAssistants
    CalcTool -->|"SSE msg"| AIAssistants
    OneTimeTool -->|"create Checkout"| StripeAPI
    MeteredTool -->|"create Checkout"| StripeAPI
    SubsTool -->|"create Checkout"| StripeAPI

    StripeAPI -->|"redirect to /payment/success"| SuccessHTML
    StripeAPI -->|".webhook event"| StripeWebhook
    StripeWebhook --> Constants

    %% Click Events
    click IndexEntrypoint "https://github.com/iannuttall/mcp-boilerplate/blob/main/src/index.ts"
    click GHHandler "https://github.com/iannuttall/mcp-boilerplate/blob/main/src/auth/github-handler.ts"
    click GHandler "https://github.com/iannuttall/mcp-boilerplate/blob/main/src/auth/google-handler.ts"
    click OAuthOrch "https://github.com/iannuttall/mcp-boilerplate/blob/main/src/auth/oauth.ts"
    click OAuthUtils "https://github.com/iannuttall/mcp-boilerplate/blob/main/src/auth/workers-oauth-utils.ts"
    click AddTool "https://github.com/iannuttall/mcp-boilerplate/blob/main/src/tools/add.ts"
    click CalcTool "https://github.com/iannuttall/mcp-boilerplate/blob/main/src/tools/calculate.ts"
    click OneTimeTool "https://github.com/iannuttall/mcp-boilerplate/blob/main/src/tools/onetimeAdd.ts"
    click MeteredTool "https://github.com/iannuttall/mcp-boilerplate/blob/main/src/tools/meteredAdd.ts"
    click SubsTool "https://github.com/iannuttall/mcp-boilerplate/blob/main/src/tools/subscriptionAdd.ts"
    click CheckHistory "https://github.com/iannuttall/mcp-boilerplate/blob/main/src/tools/checkPaymentHistory.ts"
    click ToolsIndex "https://github.com/iannuttall/mcp-boilerplate/blob/main/src/tools/index.ts"
    click StripeWebhook "https://github.com/iannuttall/mcp-boilerplate/blob/main/src/webhooks/stripe.ts"
    click Constants "https://github.com/iannuttall/mcp-boilerplate/blob/main/src/helpers/constants.ts"
    click Config "https://github.com/iannuttall/mcp-boilerplate/blob/main/wrangler.jsonc"
    click DevVars "https://github.com/iannuttall/mcp-boilerplate/blob/main/.dev.vars.example"
    click IndexHTML "https://github.com/iannuttall/mcp-boilerplate/blob/main/src/pages/index.html"
    click SuccessHTML "https://github.com/iannuttall/mcp-boilerplate/blob/main/src/pages/payment-success.html"

    %% Styles
    classDef frontend fill:#f2f2f2,stroke:#333,stroke-width:1px
    classDef static fill:#d9d9d9,stroke:#333,stroke-width:1px
    classDef worker fill:#e6f7ff,stroke:#1890ff,stroke-width:2px
    classDef auth fill:#bae7ff,stroke:#1890ff,stroke-width:1px
    classDef tools fill:#ffe7ba,stroke:#fa8c16,stroke-width:1px
    classDef payment fill:#d9f7be,stroke:#52c41a,stroke-width:1px
    classDef datastore fill:#fff1b8,stroke:#d4b106,stroke-width:1px
    classDef external fill:#f9f0ff,stroke:#722ed1,stroke-width:1px
    classDef common fill:#fff0f6,stroke:#eb2f96,stroke-width:1px
    classDef config fill:#f0f0f0,stroke:#8c8c8c,stroke-width:1px
```
