MCP Patterns
Patterns for building, composing, and securing Model Context Protocol servers. Based on the 2025-11-25 specification — the latest stable release maintained by the Agentic AI Foundation (Linux Foundation), co-founded by Anthropic, Block, and OpenAI.
Scaffolding a new server? Use Anthropic's mcp-builder skill (claude install anthropics/skills) for project setup and evaluation creation. This skill focuses on patterns, security, and advanced features after initial setup.
Deploying to Cloudflare? See the building-mcp-server-on-cloudflare skill for Workers-specific deployment patterns.
Decision Tree — Which Rule to Read
What are you building?
│
├── New MCP server
│ ├── Setup & primitives ──────► rules/server-setup.md
│ ├── Transport selection ─────► rules/server-transport.md
│ └── Scaffolding ─────────────► mcp-builder skill (anthropics/skills)
│
├── Authentication & authorization
│ └── OAuth 2.1 + OIDC ───────► rules/auth-oauth21.md
│
├── Advanced server features
│ ├── Tool composition ────────► rules/advanced-composition.md
│ ├── Resource caching ────────► rules/advanced-resources.md
│ ├── Elicitation (user input) ► rules/elicitation.md
│ ├── Sampling (agent loops) ──► rules/sampling-tools.md
│ └── Interactive UI ──────────► rules/apps-ui.md
│
├── Client-side consumption
│ └── Connecting to servers ───► rules/client-patterns.md
│
├── Security hardening
│ ├── Prompt injection defense ► rules/security-injection.md
│ └── Zero-trust & verification ► rules/security-hardening.md
│
├── Testing & debugging
│ └── Inspector + unit tests ──► rules/testing-debugging.md
│
├── Discovery & ecosystem
│ └── Registries & catalogs ──► rules/registry-discovery.md
│
└── Browser-native tools
└── WebMCP (W3C) ───────────► rules/webmcp-browser.md
Quick Reference
| Category |
Rule |
Impact |
Key Pattern |
| Server |
server-setup.md |
HIGH |
FastMCP lifespan, Tool/Resource/Prompt primitives |
| Server |
server-transport.md |
HIGH |
stdio for CLI, Streamable HTTP for production |
| Auth |
auth-oauth21.md |
HIGH |
PKCE, RFC 8707 resource indicators, token validation |
| Advanced |
advanced-composition.md |
MEDIUM |
Pipeline, parallel, and branching tool composition |
| Advanced |
advanced-resources.md |
MEDIUM |
Resource caching with TTL, LRU eviction, lifecycle |
| Advanced |
elicitation.md |
MEDIUM |
Server-initiated structured input from users |
| Advanced |
sampling-tools.md |
MEDIUM |
Server-side agent loops with tool calling |
| Advanced |
apps-ui.md |
MEDIUM |
Interactive UI via MCP Apps + @mcp-ui/* SDK |
| Client |
client-patterns.md |
MEDIUM |
TypeScript/Python MCP client connection patterns |
| Security |
security-injection.md |
HIGH |
Description sanitization, encoding normalization |
| Security |
security-hardening.md |
HIGH |
Zero-trust allowlist, hash verification, rug pull detection |
| Quality |
testing-debugging.md |
MEDIUM |
MCP Inspector, unit tests, transport debugging |
| Ecosystem |
registry-discovery.md |
LOW |
Official registry API, server metadata |
| Ecosystem |
webmcp-browser.md |
LOW |
W3C browser-native agent tools (complementary) |
Total: 14 rules across 6 categories
Key Decisions
| Decision |
Recommendation |
| Transport |
stdio for CLI/Desktop, Streamable HTTP for production (SSE deprecated) |
| Language |
TypeScript for production (better SDK support, type safety) |
| Auth |
OAuth 2.1 with PKCE (S256) + RFC 8707 resource indicators |
| Server lifecycle |
Always use FastMCP lifespan for resource management |
| Error handling |
Return errors as text content (Claude can interpret and retry) |
| Tool composition |
Pipeline for sequential, asyncio.gather for parallel |
| Resource caching |
TTL + LRU eviction with memory cap |
| Tool trust model |
Zero-trust: explicit allowlist + hash verification |
| User input |
Elicitation for runtime input; never request PII via elicitation |
| Interactive UI |
MCP Apps with @mcp-ui/* SDK; sandbox all iframes |
| Token handling |
Never pass through client tokens to downstream services |
Spec & Governance
- Protocol: Model Context Protocol, spec version 2025-11-25
- Governance: Agentic AI Foundation (Linux Foundation, Dec 2025)
- Platinum members: AWS, Anthropic, Block, Bloomberg, Cloudflare, Google, Microsoft, OpenAI
- Adoption: 10,000+ servers; Claude, Cursor, Copilot, Gemini, ChatGPT, VS Code
- Spec URL: https://modelcontextprotocol.io/specification/2025-11-25
Feature Maturity
| Feature |
Spec Version |
Status |
| Tools, Resources, Prompts |
2024-11-05 |
Stable |
| Streamable HTTP transport |
2025-03-26 |
Stable (replaces SSE) |
| OAuth 2.1 + Elicitation (form) |
2025-06-18 |
Stable |
| Sampling with tool calling |
2025-11-25 |
Stable |
| Elicitation URL mode |
2025-11-25 |
Stable |
| MCP Apps (UI extension) |
2026-01-26 |
Extension (ext-apps) |
| WebMCP (browser-native) |
2026-02-14 |
W3C Community Draft |
Example
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("my-server")
@mcp.tool()
async def search(query: str) -> str:
"""Search documents. Returns matching results."""
results = await db.search(query)
return "\n".join(r.title for r in results[:10])
Common Mistakes
- No lifecycle management (connection/resource leaks on shutdown)
- Missing input validation on tool arguments
- Returning secrets in tool output (API keys, credentials)
- Unbounded response sizes (Claude has context limits)
- Trusting tool descriptions without sanitization (injection risk)
- No hash verification on tool invocations (rug pull vulnerability)
- Storing auth tokens in session IDs (credential leak)
- Blocking synchronous code in async server (use
asyncio.to_thread())
- Using SSE transport instead of Streamable HTTP (deprecated since March 2025)
- Passing through client tokens to downstream services (confused deputy)
Ecosystem
| Resource |
What For |
mcp-builder skill (anthropics/skills) |
Scaffold new MCP servers + create evals |
building-mcp-server-on-cloudflare skill |
Deploy MCP servers on Cloudflare Workers |
@mcp-ui/* packages (npm) |
Implement MCP Apps UI standard |
| MCP Registry |
Discover servers: https://registry.modelcontextprotocol.io/ |
| MCP Inspector |
Debug and test servers interactively |
Related Skills
ork:llm-integration — LLM function calling patterns
ork:security-patterns — General input sanitization and layered security
ork:api-design — REST/GraphQL API design patterns
1---2name: mcp-patterns-23description: MCP server building, advanced patterns, and security hardening. Use when building MCP servers, implementing tool handlers, adding authentication, creating interactive UIs, hardening MCP security, or debugging MCP integrations.4license: MIT5---67# MCP Patterns89Patterns for building, composing, and securing Model Context Protocol servers. Based on the **2025-11-25 specification** — the latest stable release maintained by the [Agentic AI Foundation](https://agenticaifoundation.org/) (Linux Foundation), co-founded by Anthropic, Block, and OpenAI.1011> **Scaffolding a new server?** Use Anthropic's `mcp-builder` skill (`claude install anthropics/skills`) for project setup and evaluation creation. This skill focuses on **patterns, security, and advanced features** after initial setup.12>13> **Deploying to Cloudflare?** See the `building-mcp-server-on-cloudflare` skill for Workers-specific deployment patterns.1415## Decision Tree — Which Rule to Read1617```18What are you building?19│20├── New MCP server21│ ├── Setup & primitives ──────► rules/server-setup.md22│ ├── Transport selection ─────► rules/server-transport.md23│ └── Scaffolding ─────────────► mcp-builder skill (anthropics/skills)24│25├── Authentication & authorization26│ └── OAuth 2.1 + OIDC ───────► rules/auth-oauth21.md27│28├── Advanced server features29│ ├── Tool composition ────────► rules/advanced-composition.md30│ ├── Resource caching ────────► rules/advanced-resources.md31│ ├── Elicitation (user input) ► rules/elicitation.md32│ ├── Sampling (agent loops) ──► rules/sampling-tools.md33│ └── Interactive UI ──────────► rules/apps-ui.md34│35├── Client-side consumption36│ └── Connecting to servers ───► rules/client-patterns.md37│38├── Security hardening39│ ├── Prompt injection defense ► rules/security-injection.md40│ └── Zero-trust & verification ► rules/security-hardening.md41│42├── Testing & debugging43│ └── Inspector + unit tests ──► rules/testing-debugging.md44│45├── Discovery & ecosystem46│ └── Registries & catalogs ──► rules/registry-discovery.md47│48└── Browser-native tools49 └── WebMCP (W3C) ───────────► rules/webmcp-browser.md50```5152## Quick Reference5354| Category | Rule | Impact | Key Pattern |55|----------|------|--------|-------------|56| **Server** | `server-setup.md` | HIGH | FastMCP lifespan, Tool/Resource/Prompt primitives |57| **Server** | `server-transport.md` | HIGH | stdio for CLI, Streamable HTTP for production |58| **Auth** | `auth-oauth21.md` | HIGH | PKCE, RFC 8707 resource indicators, token validation |59| **Advanced** | `advanced-composition.md` | MEDIUM | Pipeline, parallel, and branching tool composition |60| **Advanced** | `advanced-resources.md` | MEDIUM | Resource caching with TTL, LRU eviction, lifecycle |61| **Advanced** | `elicitation.md` | MEDIUM | Server-initiated structured input from users |62| **Advanced** | `sampling-tools.md` | MEDIUM | Server-side agent loops with tool calling |63| **Advanced** | `apps-ui.md` | MEDIUM | Interactive UI via MCP Apps + @mcp-ui/* SDK |64| **Client** | `client-patterns.md` | MEDIUM | TypeScript/Python MCP client connection patterns |65| **Security** | `security-injection.md` | HIGH | Description sanitization, encoding normalization |66| **Security** | `security-hardening.md` | HIGH | Zero-trust allowlist, hash verification, rug pull detection |67| **Quality** | `testing-debugging.md` | MEDIUM | MCP Inspector, unit tests, transport debugging |68| **Ecosystem** | `registry-discovery.md` | LOW | Official registry API, server metadata |69| **Ecosystem** | `webmcp-browser.md` | LOW | W3C browser-native agent tools (complementary) |7071**Total: 14 rules across 6 categories**7273## Key Decisions7475| Decision | Recommendation |76|----------|----------------|77| Transport | stdio for CLI/Desktop, Streamable HTTP for production (SSE deprecated) |78| Language | TypeScript for production (better SDK support, type safety) |79| Auth | OAuth 2.1 with PKCE (S256) + RFC 8707 resource indicators |80| Server lifecycle | Always use FastMCP lifespan for resource management |81| Error handling | Return errors as text content (Claude can interpret and retry) |82| Tool composition | Pipeline for sequential, `asyncio.gather` for parallel |83| Resource caching | TTL + LRU eviction with memory cap |84| Tool trust model | Zero-trust: explicit allowlist + hash verification |85| User input | Elicitation for runtime input; never request PII via elicitation |86| Interactive UI | MCP Apps with @mcp-ui/* SDK; sandbox all iframes |87| Token handling | Never pass through client tokens to downstream services |8889## Spec & Governance9091- **Protocol**: Model Context Protocol, spec version **2025-11-25**92- **Governance**: Agentic AI Foundation (Linux Foundation, Dec 2025)93- **Platinum members**: AWS, Anthropic, Block, Bloomberg, Cloudflare, Google, Microsoft, OpenAI94- **Adoption**: 10,000+ servers; Claude, Cursor, Copilot, Gemini, ChatGPT, VS Code95- **Spec URL**: https://modelcontextprotocol.io/specification/2025-11-259697### Feature Maturity9899| Feature | Spec Version | Status |100|---------|-------------|--------|101| Tools, Resources, Prompts | 2024-11-05 | Stable |102| Streamable HTTP transport | 2025-03-26 | Stable (replaces SSE) |103| OAuth 2.1 + Elicitation (form) | 2025-06-18 | Stable |104| Sampling with tool calling | 2025-11-25 | Stable |105| Elicitation URL mode | 2025-11-25 | Stable |106| MCP Apps (UI extension) | 2026-01-26 | Extension (ext-apps) |107| WebMCP (browser-native) | 2026-02-14 | W3C Community Draft |108109## Example110111```python112from mcp.server.fastmcp import FastMCP113114mcp = FastMCP("my-server")115116@mcp.tool()117async def search(query: str) -> str:118 """Search documents. Returns matching results."""119 results = await db.search(query)120 return "\n".join(r.title for r in results[:10])121```122123## Common Mistakes1241251. No lifecycle management (connection/resource leaks on shutdown)1262. Missing input validation on tool arguments1273. Returning secrets in tool output (API keys, credentials)1284. Unbounded response sizes (Claude has context limits)1295. Trusting tool descriptions without sanitization (injection risk)1306. No hash verification on tool invocations (rug pull vulnerability)1317. Storing auth tokens in session IDs (credential leak)1328. Blocking synchronous code in async server (use `asyncio.to_thread()`)1339. Using SSE transport instead of Streamable HTTP (deprecated since March 2025)13410. Passing through client tokens to downstream services (confused deputy)135136## Ecosystem137138| Resource | What For |139|----------|----------|140| `mcp-builder` skill (anthropics/skills) | Scaffold new MCP servers + create evals |141| `building-mcp-server-on-cloudflare` skill | Deploy MCP servers on Cloudflare Workers |142| `@mcp-ui/*` packages (npm) | Implement MCP Apps UI standard |143| MCP Registry | Discover servers: https://registry.modelcontextprotocol.io/ |144| MCP Inspector | Debug and test servers interactively |145146## Related Skills147148- `ork:llm-integration` — LLM function calling patterns149- `ork:security-patterns` — General input sanitization and layered security150- `ork:api-design` — REST/GraphQL API design patterns