↑ ↓ navigate↵ selectesc close

MCP in Production: Building & Securing Model Context Protocol Servers

course lessons.

1 Section · 5 Lessons
1

Core Architecture & Production Implementation

course overview.

1. Production Architecture Problem & Why We Built This Course

Move beyond toy local Stdio scripts. Learn how to architect, secure, and deploy remote Model Context Protocol (MCP) servers on Cloudflare Workers with OAuth 2.1, Better-Auth, WebMCP (RFC 9727), and Sanity Context Knowledge Bases. In production engineering teams, the gap between a quick weekend demo and a resilient, multi-tenant system comes down to three architectural boundaries: cold-start latency, compile-time contract validation, and predictable operational cost. Throughout MCP in Production: Building & Securing Model Context Protocol Servers, we strip away marketing buzzwords and build the exact TypeScript, edge compute, and structured data patterns that power CodingCat.dev in production.

Every lesson in this course pairs a dark-mode Architecture Diagram (contrasting the legacy anti-pattern on the left against the recommended 2026 production pattern on the right) with copy-pasteable TypeScript modules, automated verification checks, and an explicit Trade-Off Matrix. Whether you are reading the official Model Context Protocol (MCP) Official Specification (2025-03-26 / 2026 Streamable HTTP) or pairing this course with our deep dive on Blog Post: Architecting the Agent-Ready Web with WebMCP, Streamable HTTP MCP, and RFC 9727, you can apply every pattern directly to your own repository today.

🐾 AJ's Course Architecture Rule: Never adopt a tool just because it trended on social media. Measure its cold start in milliseconds, verify its TypeScript schema boundary at compile time, and check what happens to your cloud bill when traffic spikes 100x.

2. Lesson-by-Lesson Architectural Walkthrough (5 Lessons)

Work through the lessons in sequence to build the complete system from first principles, or jump directly to the specific architectural boundary you are solving right now:

  • Lesson 1: What MCP Actually Is (Architecture Diagram & Stdio vs. Streamable HTTP): Replaces the STDIO TRAP (AI Host App, stdin / stdout, Child Proc) anti-pattern with JSON-RPC 2.0 -> POST /api/mcp + Edge Worker inside src/mcp/transport.ts. Every conference talk this year mentions the Model Context Protocol, or MCP, but most tutorials stop at a local Stdio script that only runs on your laptop. Let's break down what MCP actually is on the wire and wire up Streamable HTTP in TypeScript.
  • Lesson 2: Building Your First TypeScript MCP Server on Cloudflare Workers: Replaces the CONTAINER LAG (Docker Image, Cold Boot, Idle Server) anti-pattern with Hono + MCP -> tools/list + tools/call inside src/mcp/worker.ts. Shipping an MCP server inside a heavy Docker container means paying for idle RAM and suffering six hundred millisecond cold starts. Here is how we deploy a zero-cold-start TypeScript MCP server on Cloudflare Workers with Hono.
  • Lesson 3: Securing Remote MCP Servers with OAuth 2.1 & Better-Auth: Replaces the STATIC KEYS (Shared API Key, No Scopes, No Expiry) anti-pattern with Better-Auth -> Bearer Token + Tenant Isolation inside src/mcp/auth-guard.ts. Pasting long-lived admin API keys into local JSON files works for a weekend demo, but it is a security disaster in production. Let's secure our remote MCP server with OAuth 2.1, PKCE, and Better-Auth.
  • Lesson 4: Client-Side WebMCP & RFC 9727 Agent Discovery: Replaces the DOM SCRAPING (Headless Bot, Brittle CSS, Blind Clicks) anti-pattern with /.well-known -> navigator.modelContext + Link Header inside src/pages/.well-known/api-catalog.ts. Why are AI agents still burning twenty thousand tokens scraping raw HTML DOM trees when your web app already has a structured API? Let's fix that with Client-Side WebMCP and RFC 9727 Agent Discovery.
  • Lesson 5: Connecting MCP to Verified Knowledge Bases (Sanity Context vs. Naive RAG): Replaces the NAIVE RAG (500-Token Chunks, Stale Vectors, Top-K Guess) anti-pattern with Sanity Context -> initial_context + knowledge_search inside src/mcp/sanity-context.ts. Slicing your documentation into five-hundred-token vector chunks and praying cosine similarity finds the right paragraph is why production RAG hallucinates. Here is how Sanity Context Knowledge Bases and MCP fix it.

3. Alex's 2026 Ecosystem & Architectural Tier List

Before writing a single line of configuration, you need to know how the available platforms and patterns stack up in real production environments. Here is how I rank the ecosystem from S-Tier (our default production choice on CodingCat.dev) down to D-Tier anti-patterns to avoid:

Never expose a local Stdio bridge to the public web! Streamable HTTP + OAuth 2.1 on V8 Isolates is S-Tier! 🐾
💡
TIER LIST

Alex's 2026 MCP Transport & Hosting Architectural Tier List

Ranking 5 production architectures for Model Context Protocol servers across cold-start latency, auth isolation, discovery, and cost.

SPurrfect
1 pick

Sub-5ms cold starts across 300+ edge PoPs, native JSON-RPC 2.0 over HTTP POST/SSE, and zero-cost idle scaling with scoped OAuth 2.1 Bearer tokens stored in Cloudflare D1.

Cold Start:< 5ms (V8 Isolate)
Auth Boundary:OAuth 2.1 + PKCE + D1 Session
Idle Cost:$0.00 (CPU ms billing only)
AStrong Pick
2 picks

Exposes structured browser tools directly to in-page AI assistants while reusing the user's active cookie session and eliminating 20,000-token DOM scraping.

Token Footprint:~150 tokens vs 20k HTML DOM
Discovery:/.well-known/api-catalog
Best Use Case:Interactive SaaS UI workflows

Sanity Context Knowledge Base MCP ↗

Ahead-of-Time Verified Graph

Compiles structured Portable Text and document references into an ahead-of-time knowledge graph served via initial_context and knowledge_search tools.

Retrieval Mode:Graph outline + entity search
Sync Latency:Instant on Sanity publish
Hallucination Risk:Near-zero (conflict checked)
BSolid w/ Caveats
1 pick

Ideal for local git, terminal, and filesystem helpers inside Cursor or Claude Desktop, but cannot be deployed directly for multi-tenant SaaS users.

Cold Start:200ms to 800ms OS spawn
Security Scope:Full local shell permissions
Distribution:Requires local Node/Python runtime
CSituational
0 picks
No platforms currently in C-Tier (Niche Fit, Early Preview, or Steep Quotas)
DPass / Caution
1 pick

Unauthenticated Docker SSE Wrapper with Shared Root API Key

Legacy Container Anti-Pattern

Suffers from 600ms+ container cold starts, $50+/mo idle memory charges, and catastrophic confused-deputy security vulnerabilities when one static key is shared.

Cold Start:600ms to 2,000ms container boot
Blast Radius:Total tenant database compromise
Idle Cost:$25 to $120/month per service
S: PurrfectA: Production ReadyB: Solid w/ CaveatsC: SituationalD: Pass
Click AJ to celebrate! 🐾

4. Production Starter Configuration & Local Setup

To follow along with the code examples in MCP in Production: Building & Securing Model Context Protocol Servers, start with this baseline wrangler.jsonc configuration. It enables strict TypeScript verification, observability tracing, and local development bindings that match production 1-to-1:

📄 wrangler.jsoncjson
{
  "$schema": "node_modules/wrangler/config-schema.json",
  "name": "codingcat-mcp-production",
  "main": "src/mcp/worker.ts",
  "compatibility_date": "2026-03-01",
  "compatibility_flags": ["nodejs_compat"],
  "observability": { "enabled": true, "head_sampling_rate": 1 },
  "d1_databases": [
    {
      "binding": "DB",
      "database_name": "codingcat-mcp-auth",
      "database_id": "00000000-0000-0000-0000-000000000001"
    }
  ],
  "vars": {
    "MCP_SERVER_NAME": "codingcat-catalog-mcp",
    "MCP_SERVER_VERSION": "2.1.0",
    "SANITY_PROJECT_ID": "hfh83o0w",
    "SANITY_DATASET": "production"
  }
}

5. Course-Wide Production Trade-Off Matrix

Every architectural decision involves trade-offs. Use this reference table during design reviews with your team to justify why we choose edge-native TypeScript, explicit schema validation, and deterministic workflows over legacy alternatives:

Architectural Dimension2026 CodingCat.dev Course PatternLegacy Industry DefaultProduction Impact at Scale
Compute & Cold StartsV8 Isolates / Native Browser Compositor (< 5ms)Heavy Container VMs / Main-Thread JS Bloat (500ms+)Eliminates tail-latency spikes on every request and interaction.
Contract & Type SafetyEnd-to-End TypeScript + Zod / GROQ TypeGenUntyped JSON payloads & manual interface driftCatches breaking schema changes in CI before deployment.
State & Database TopologyCloudflare D1 Batch / Durable SQLite / Sanity Content LakeExhausted TCP pools & unindexed N+1 loopsPredictable sub-10ms reads with zero connection pool errors.
Security & Multi-TenancyOAuth 2.1 PKCE + Better-Auth Scoped SessionsShared static API keys & unescaped DOM stringsZero-trust tenant isolation and $0 per-MAU SaaS tax.
AI & Agent ReadinessStreamable HTTP MCP + WebMCP + RFC 9727 + Daily Spend Caps20k-token headless DOM scraping & uncapped retry loopsCuts token spend by 95%+ while preventing runaway bills.

6. Official Specifications, Docs & Related CodingCat.dev Deep Dives

Bookmark these official specifications and related CodingCat.dev Courses, Blog Posts, and Podcast Episodes as you work through the lessons:

Ready to write code? Jump straight into Lesson 1: What MCP Actually Is (Architecture Diagram & Stdio vs. Streamable HTTP) →

✏️ Visual Architecture Diagram

how this course connects together.

Follow AJ through the sequence topology from input contract to production output.

✏️ Architecture Diagram: MCP in Production: Building & Securing Model Con…
UML SEQUENCE DIAGRAMARCHITECTURE DIAGRAM
Architecture Diagram: MCP in Production: Building & Securing Model Con…UML SEQUENCE DIAGRAM showing: Node 1: Core Architecture & Production Implementation (Schema & deterministic boundary) -> Node 2: Production (Execution & state transitions) -> Node 3: Building (Production output & telemetry). Content-driven sequence diagram for MCP in Production: Building & Securing Model Context Protocol Servers: Core Architecture & Production Implementation -> Production -> Building.loop [deterministic execution]Core Architecture & PrSchema & deterministic bProductionExecution & state transiBuildingProduction output & tele1. Core Architecture ack / verified state2. Productionack / verified state
[Diagram Walkthrough]Content-driven sequence diagram for MCP in Production: Building & Securing Model Context Protocol Servers: Core Architecture & Production Implementation -> Production -> Building.
AAA Contrast Verified