Protocol Overview
MCP utilizes JSON-RPC 2.0 as its communication format. The protocol enables structured
communication between clients and servers, with clear patterns for requests, responses, notifications,
and error handling.
1. JSON-RPC 2.0 Basic Structure
- Each request or response is a JSON object
- Contains fixed version identifier: "jsonrpc": "2.0"
- Includes "method" and optional "params" for requests
- "id" is used for request-response matching
{
"jsonrpc": "2.0",
"method": "subtract",
"params": [42, 23],
"id": 1
}
{
"jsonrpc": "2.0",
"result": 19,
"id": 1
}
Communication Patterns
Request (method, params, id)
Notification (method, params)
No response
Error (code, message, id)
JSON-RPC 2.0 Operation Examples
Here's a flow illustrating the actual operation of the protocol:
Client → Server: Request
{"jsonrpc": "2.0", "method": "subtract", "params": [42, 23], "id": 1}
Server → Client: Response
{"jsonrpc": "2.0", "result": 19, "id": 1}
Client → Server: Notification
{"jsonrpc": "2.0", "method": "update", "params": [1, 2, 3, 4]}
Client → Server: Request (error case)
{"jsonrpc": "2.0", "method": "unknownMethod", "id": 2}
Server → Client: Error Response
{"jsonrpc": "2.0", "error": {"code": -32601, "message": "Method not found"}, "id": 2}
MCP Transport Methods
1. STD IO (Standard Input/Output)
- Use Case: Local MCP servers (subprocesses or CLI tools)
- Communication: Via stdin/stdout
- Advantages: High efficiency, no network delay
- Implementation: Client starts server as child process using OS pipes
- Message Format: UTF-8 encoded JSON-RPC with newline delimiters
- Communication Mode: Synchronous, messages processed in order
2. HTTP SSE (Server-Sent Events)
- Use Case: Remote servers or cloud services
- Key Feature: Supports server pushing events to clients
- Ideal For: Long connections, large result transmission, real-time updates
- Implementation:
- Client and Server communicate over HTTP protocol
- Server uses SSE to establish long-lived connections for pushing data
- Client sends requests using HTTP POST to specific endpoints
- Communication Mode: Asynchronous, event-driven model
- Server Requirements: Two endpoints
- SSE endpoint (e.g., /sse): For establishing event streams
- HTTP POST endpoint (e.g., /messages): For receiving client requests
Advantages of the MCP Design
The MCP communication protocol offers several key benefits:
- Lightweight & Language-agnostic: JSON is universally supported across
programming languages
- Flexibility: Supports both local and remote operation through different
transport methods
- Standardized: Based on the established JSON-RPC 2.0 specification
- Debugging-friendly: Human-readable format makes troubleshooting easier
- Cross-platform compatibility: Works consistently across different operating
systems
- Scalability: Can handle simple local interactions as well as distributed
systems
協議概述
MCP 採用 JSON-RPC 2.0 作為通訊格式。該協議實現了客戶端和服務器之間的結構化通信,具有清晰的請求、響應、通知和錯誤處理模式。
1. JSON-RPC 2.0 基本結構
- 每個請求或回應都是一個 JSON 物件
- 包含固定版本標識: "jsonrpc": "2.0"
- 請求包括 "method" 和可選的 "params"
- "id" 用於請求和回應的匹配
{
"jsonrpc": "2.0",
"method": "subtract",
"params": [42, 23],
"id": 1
}
{
"jsonrpc": "2.0",
"result": 19,
"id": 1
}
通信模式
JSON-RPC 2.0 實際運作過程
這是一個說明協議實際運作的流程:
客戶端 → 服務器: 請求
{"jsonrpc": "2.0", "method": "subtract", "params": [42, 23], "id": 1}
服務器 → 客戶端: 回應
{"jsonrpc": "2.0", "result": 19, "id": 1}
客戶端 → 服務器: 通知
{"jsonrpc": "2.0", "method": "update", "params": [1, 2, 3, 4]}
客戶端 → 服務器: 請求 (錯誤範例)
{"jsonrpc": "2.0", "method": "unknownMethod", "id": 2}
服務器 → 客戶端: 錯誤回應
{"jsonrpc": "2.0", "error": {"code": -32601, "message": "Method not found"}, "id": 2}
MCP 傳輸方式
1. STDIO傳輸 (標準輸入/輸出)
- 應用場景: 適用於本地運行的 MCP 伺服器,例如子程序或 CLI 工具
- 通信方式: 透過標準輸入/輸出 (stdin/stdout) 進行通信
- 優點: 無網路延遲,效率高
- 實現機制: Client 啟動 Server 為子進程,利用作業系統管道傳輸資料
- 消息格式: 使用 UTF-8 編碼的 JSON-RPC 格式,以換行符 \n 分隔
- 通訊模式: 同步阻塞,訊息需依序處理
2. HTTP SSE (Server-Sent Events)
- 應用場景: 適用於遠端伺服器或雲端服務
- 功能特點: 支援伺服器主動推送事件給 Client
- 適合場景: 適合長連線、大型結果的分段傳輸或即時狀態更新
- 實現機制:
- Client 與 Server 透過 HTTP 協議進行跨網路通信
- Server 透過 SSE (Server-Sent Events) 建立長連接主動推送資料
- Client 使用 HTTP POST 向指定端點發送請求
- 通訊模式: 採用非同步事件驅動模型,支援即時或近即時互動
- 伺服器實作要求: 提供兩個端點
- SSE 端點 (如 /sse): 用於建立長連線接收事件流
- HTTP POST 端點 (如 /messages): 用於接收 Client 的請求
MCP 設計的優勢
MCP 通信協議提供了多項重要優勢:
- 輕量且語言無關: JSON 在各種程式語言中普遍支援
- 靈活性: 通過不同的傳輸方法支援本地和遠端操作
- 標準化: 基於已建立的 JSON-RPC 2.0 規範
- 便於調試: 人類可讀的格式使故障排除更容易
- 跨平台兼容性: 在不同操作系統上一致運作
- 可擴展性: 可以處理簡單的本地互動以及分散式系統