# MCP Protocol

> Model Context Protocol specification -- client-host-server architecture, JSON-RPC 2.0, transport layers (stdio/HTTP/SSE), capabilities negotiation, sampling, and lifecycle management.

- Skill: `leolin990405/mcp-protocol` (Agent Skill)
- Install (CLI): `npx get-skill add leolin990405/mcp-protocol`
- Raw SKILL.md: https://api.skillmd.com/api/skills/leolin990405/mcp-protocol/raw
- Safety review: pending (external: skill-scanner PASS, skillspector PASS)
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: AI & ML
- Author: LeoLin990405 (https://skillmd.com/u/leolin990405)
- Updated: 2026-11-11
- Page: https://skillmd.com/skills/leolin990405/mcp-protocol

---


# 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-Id` header
- Resumability via SSE event IDs + `Last-Event-ID`

## Message Format (JSON-RPC 2.0)

**Request**:
```json
{"jsonrpc": "2.0", "id": 1, "method": "tools/call",
 "params": {"name": "get_weather", "arguments": {"location": "NYC"}}}
```

**Response**:
```json
{"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:
```json
{"method": "initialize", "params": {
  "protocolVersion": "2025-03-26",
  "capabilities": {"roots": {"listChanged": true}, "sampling": {}},
  "clientInfo": {"name": "MyClient", "version": "1.0"}
}}
```

Server responds with capabilities:
```json
{"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:
```json
{"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

```typescript
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.

## Sources

- [MCP Specification](https://modelcontextprotocol.io/specification/2025-03-26)
- [Full Docs for LLMs](https://modelcontextprotocol.io/llms-full.txt)
- [TypeScript SDK](https://github.com/modelcontextprotocol/typescript-sdk)
- [Python SDK](https://github.com/modelcontextprotocol/python-sdk)

