MCP 通信協議
MCP 定義了一套標準化的通信協議,使客戶端和服務器能夠以一致、可預測的方式交換資訊。這種標準化對於整個社區的互操作性至關重要。
JSON-RPC:基礎架構
MCP 的核心使用 JSON-RPC 2.0 作為客戶端和服務器之間所有通信的消息格式。JSON-RPC 是一種輕量級的遠程過程調用協議,以 JSON 編碼。
JSON-RPC 特點
- 📜 人類可讀性強,易於調試
- 🌍 語言無關性,支援在任何編程環境中實現
- 🔒 成熟穩定,具有清晰的規範和廣泛的採用
三種消息類型
MCP 使用三種基本消息類型進行通信:
1.請求(Requests)
從客戶端發送到服務器以啟動操作。請求消息包括唯一標識符、要調用的方法名稱和方法的參數(如果有)。
{
"jsonrpc": "2.0",
"id": 1,
"method": "tools/call",
"params": {
"name": "weather",
"arguments": {
"location": "San Francisco"
}
}
}
2.響應(Responses)
從服務器發送到客戶端以回覆請求。響應消息包括與相應請求相同的 id 和 result(成功時)或 error(失敗時)。
// 成功響應示例
{
"jsonrpc": "2.0",
"id": 1,
"result": {
"content": [
{
"type": "text",
"text": "San Francisco 今天的溫度是 22°C"
}
]
}
}
// 錯誤響應示例
{
"jsonrpc": "2.0",
"id": 1,
"error": {
"code": -32000,
"message": "無法取得天氣資料"
}
}
3.通知(Notifications)
單向消息,不需要響應。用於事件通知或狀態更新。通知消息沒有 id 欄位。
{
"jsonrpc": "2.0",
"method": "notifications/message",
"params": {
"level": "info",
"message": "連接已建立"
}
}
MCP交互過程的循序圖
初始化階段
sequenceDiagram
participant Client as 客戶端
participant Server as 服務器
Client->>Server: initialize
note right of Client: 客戶端發送協議版本和能力
Server-->>Client: response
note left of Server: 服務器回應支持的版本和能力
Client->>Server: initialized
note right of Client: 客戶端確認初始化完成
發現階段
sequenceDiagram
participant Client as 客戶端
participant Server as 服務器
Client->>Server: tools/list
note right of Client: 客戶端請求可用能力信息
Server-->>Client: response
note left of Server: 服務器回應可用工具列表
note over Client,Server: 此過程可為每種能力類型重複
執行階段
sequenceDiagram
participant Client as 客戶端
participant Server as 服務器
Client->>Server: tools/call
note right of Client: 客戶端根據主機需求調用能力
Server-->>Client: notification
note left of Server: 服務器發送進度更新
Server-->>Client: response
note left of Server: 服務器返回執行結果
終止階段
sequenceDiagram
participant Client as 客戶端
participant Server as 服務器
Client->>Server: shutdown
note right of Client: 客戶端請求終止連接
Server-->>Client: response
note left of Server: 服務器確認關閉請求
Client->>Server: exit
note right of Client: 客戶端發送最終退出消息
完整的MCP交互過程
sequenceDiagram
participant Host as MCP主機
participant Client as MCP客戶端
participant Server as MCP服務器
%% 初始化階段
note over Host,Server: 初始化階段
Client->>Server: initialize
Server-->>Client: response
Client->>Server: initialized
%% 發現階段
note over Host,Server: 發現階段
Client->>Server: tools/list
Server-->>Client: response
%% 執行階段
note over Host,Server: 執行階段
Host->>Client: 請求執行功能
Client->>Server: tools/call
Server-->>Client: notification
Server-->>Client: response
Client->>Host: 回傳結果
%% 終止階段
note over Host,Server: 終止階段
Client->>Server: shutdown
Server-->>Client: response
Client->>Server: exit
協議演進
MCP協議設計為可擴展和適應性強。初始化階段包括版本協商,隨著協議的演進允許向後兼容。此外,能力發現使客戶端能夠適應每個服務器提供的特定功能,使同一生態系統中的基本和高級服務器混合使用成為可能。
參考資料
- Hugging Face MCP Course. “The Communication Protocol.” https://huggingface.co/learn/mcp-course/unit1/communication-protocol
- Model Context Protocol Specification. “Messages — Model Context Protocol.” https://modelcontextprotocol.io/specification/2024-11-05/basic/messages
- Model Context Protocol Documentation. “Transports.” https://modelcontextprotocol.io/docs/concepts/transports
- Model Context Protocol Specification. “Transports — Protocol Revision 2025–03–26.” https://modelcontextprotocol.io/specification/2025-03-26/basic/transports
- Anthropic. “Introducing the Model Context Protocol.” https://www.anthropic.com/news/model-context-protocol
- Cline Docs. “MCP Transport Mechanisms — For New Coders.” https://docs.cline.bot/mcp/mcp-transport-mechanisms