Model Context Protocol (MCP)
Open standard for connecting AI applications to external systems. Like USB-C for AI.
Architecture
Host Process
├── Client 1 ─── Server 1 (Files & Git) ─── Local Resources
├── Client 2 ─── Server 2 (Database) ─── Local Resources
└── Client 3 ─── Server 3 (APIs) ─── Remote Services
Host: Coordinator, manages clients, enforces security Client: 1:1 connection to server, handles protocol negotiation Server: Provides tools, resources, and prompts
Transport Layers
stdio
- Server as subprocess, reads JSON-RPC from stdin, writes to stdout
- Messages newline-delimited, no embedded newlines
- stderr for logging only
Streamable HTTP (current standard)
- Single HTTP endpoint (e.g.,
https://example.com/mcp) - POST for client→server messages
- Accept header:
application/json+text/event-stream - GET for server-initiated SSE stream
- Session management via
Mcp-Session-Idheader - Resumability via SSE event IDs +
Last-Event-ID
Message Format (JSON-RPC 2.0)
Request:
{"jsonrpc": "2.0", "id": 1, "method": "tools/call",
"params": {"name": "get_weather", "arguments": {"location": "NYC"}}}
Response:
{"jsonrpc": "2.0", "id": 1, "result": {"content": [{"type": "text", "text": "Sunny, 72F"}]}}
Error codes: -32602 (invalid params), -32603 (internal), -32002 (not found), -1 (user rejected)
Lifecycle
1. Initialization
Client sends:
{"method": "initialize", "params": {
"protocolVersion": "2025-03-26",
"capabilities": {"roots": {"listChanged": true}, "sampling": {}},
"clientInfo": {"name": "MyClient", "version": "1.0"}
}}
Server responds with capabilities:
{"result": {
"protocolVersion": "2025-03-26",
"capabilities": {"tools": {"listChanged": true}, "resources": {"subscribe": true}},
"serverInfo": {"name": "MyServer", "version": "1.0"}
}}
Client sends notifications/initialized.
2. Operation — normal message exchange
3. Shutdown — close stdin (stdio) or HTTP DELETE (HTTP)
Capabilities
| Side | Capability | Description |
|---|---|---|
| Client | roots |
Filesystem roots |
| Client | sampling |
LLM sampling requests |
| Server | tools |
Callable tools |
| Server | resources |
Readable resources |
| Server | prompts |
Prompt templates |
| Server | logging |
Structured logs |
| Server | completions |
Argument autocompletion |
Sampling (Server → Client LLM)
Servers request LLM completions through clients:
{"method": "sampling/createMessage", "params": {
"messages": [{"role": "user", "content": {"type": "text", "text": "Summarize this"}}],
"modelPreferences": {"hints": [{"name": "claude-3-sonnet"}],
"intelligencePriority": 0.8, "speedPriority": 0.5},
"maxTokens": 100
}}
Tool Annotations
interface ToolAnnotations {
readOnlyHint?: boolean; // default: false
destructiveHint?: boolean; // default: true (only if !readOnly)
idempotentHint?: boolean; // default: false
openWorldHint?: boolean; // default: true
}
WARNING: Annotations are hints only, not guarantees. Treat as untrusted unless from trusted servers.
MCP vs Function Calling
| Aspect | MCP | Function Calling |
|---|---|---|
| Type | Open protocol | Provider-specific API |
| Scope | Cross-application | Single provider |
| Connection | Persistent, stateful | Stateless per-request |
| Discovery | Dynamic (tools/list) | Static per-request |
| Features | Tools + Resources + Prompts | Tools only |
The N*M Problem: Without MCP: N apps × M integrations = N×M work. With MCP: N + M.