Understanding MCP Communication Protocol: JSON‑RPC 2.0 and HTTP SSE Lifecycle

This article explains the MCP communication protocol, detailing how JSON‑RPC 2.0 structures requests and responses, how tools are listed and invoked via HTTP SSE, and walks through the full client‑server lifecycle with concrete code examples and diagrams.

Ubiquitous Tech
Ubiquitous Tech
Ubiquitous Tech
Understanding MCP Communication Protocol: JSON‑RPC 2.0 and HTTP SSE Lifecycle

During MCP development, client and server communicate through a standardized protocol. MCP adopts JSON‑RPC 2.0 as the "talking format", which is a lightweight, stateless remote‑procedure‑call protocol supported by virtually all programming languages.

The JSON‑RPC request body must contain "jsonrpc": "2.0", a method string, optional params, and an id. A typical request and response look like:

{
  "jsonrpc": "2.0",
  "method": "subtract",
  "params": [42, 23],
  "id": 1
}
{
  "jsonrpc": "2.0",
  "result": 19,
  "id": 1
}

The article uses a coffee‑ordering analogy to illustrate the fields: method is the action (e.g., "buy"), params carries details (e.g., "item": "coffee", "count": 2), and id matches the response to the request. An error response is shown as:

{
  "jsonrpc": "2.0",
  "error": { "code": -32601, "message": "Method not found" },
  "id": 1
}

To inspect MCP traffic, the author uses the @modelcontextprotocol/inspector tool, started via npx -y @modelcontextprotocol/inspector. This tool reveals the raw JSON‑RPC messages exchanged between client and server.

When a client needs the list of available tools, it sends a tools/list request:

{
  "method": "tools/list",
  "params": { "_meta": { "progressToken": 2 } },
  "jsonrpc": "2.0",
  "id": 2
}

The server responds with a JSON object containing a tools array, each entry describing the tool name, description, and input schema. Example snippet:

{
  "tools": [
    {
      "name": "get-toutiao-trending",
      "description": "获取今日头条热榜,包含时政要闻、社会事件、国际新闻、科技发展及娱乐八卦等多领域的热门中文资讯",
      "inputSchema": {
        "type": "object",
        "properties": {},
        "required": [],
        "additionalProperties": false
      }
    }
  ]
}

To invoke a tool, the client sends a tools/call request with the tool name and arguments:

{
  "method": "tools/call",
  "params": {
    "name": "get-toutiao-trending",
    "arguments": {},
    "_meta": { "progressToken": 2 }
  }
}

The server returns a result payload such as:

{
  "content": [
    { "type": "text", "text": "xxxxxx" }
  ],
  "isError": false
}

MCP servers using the HTTP SSE mode expose two endpoints: GET /sse to establish a long‑lived SSE channel, and POST /message for client requests, responses, or notifications.

The full client‑server interaction follows six stages:

Connection establishment : client GETs /sse, server creates an SseEmitter and a session.

Session initialization : client POSTs an InitializeRequest, server replies with InitializeResponse.

Tool list request : client POSTs tools/list, server processes the request and pushes the tool list via SSE.

Heartbeat : client sends a ping, server replies with pong and updates the session timestamp.

Tool call : client POSTs tools/call, server executes the corresponding handler and pushes the result through SSE.

Connection close : client signals termination, server removes the session and SSE emitter.

The article includes a Mermaid sequence diagram (shown below) that visualises these stages.

sequenceDiagram
    participant Client
    participant Server
    participant SessionMap as Map<String, McpServerSession>
    participant EmitterMap as Map<String, SseEmitter>
    participant Handler as RequestHandler
    %% 阶段一:连接建立
    rect rgb(210, 236, 247)
    note over Client,Handler: 阶段一:连接建立
    Client->>Server: GET /sse
    Server->>EmitterMap: 创建并存储 SseEmitter
    Server->>SessionMap: 创建并存储 McpServerSession
    Server-->>Client: 返回 SSE 连接及 /message 接口地址
    end
    %% 阶段二:会话初始化
    rect rgb(250, 236, 236)
    note over Client,Handler: 阶段二:会话初始化
    Client->>Server: POST /message (InitializeRequest)
    Server->>SessionMap: 获取 McpServerSession
    Server->>EmitterMap: 获取对应 SseEmitter
    Server-->>Client: SSE 返回 InitializeResponse
    end
    %% 阶段三:资源请求
    rect rgb(225, 250, 225)
    note over Client,Handler: 阶段三:资源请求
    Client->>Server: POST /message (tools/list)
    Server->>Handler: 处理请求
    Handler->>Handler: 调用工具处理器
    Handler->>EmitterMap: 推送处理结果
    EmitterMap-->>Client: SSE 返回工具列表
    end
    %% 阶段四:连接维持
    rect rgb(240, 230, 250)
    note over Client,Handler: 阶段四:连接维持
    Client->>Server: POST /message (ping)
    Server->>SessionMap: 更新时间戳
    Server->>EmitterMap: 推送 pong 响应
    EmitterMap-->>Client: SSE 返回 pong
    end
    %% 阶段五:工具调用
    rect rgb(255, 230, 230)
    note over Client,Handler: 阶段五:工具调用
    Client->>Server: POST /message (tools/call)
    Server->>Handler: 根据 method 调用具体工具处理器
    Handler-->>Server: 返回调用结果
    Server->>EmitterMap: 推送执行结果
    EmitterMap-->>Client: SSE 返回工具调用结果
    end
    %% 阶段六:连接关闭
    rect rgb(230, 230, 230)
    note over Client,Handler: 阶段六:连接关闭
    Client->>Server: 主动断开连接
    Server->>EmitterMap: 移除 SseEmitter
    Server->>SessionMap: 移除 Session
    end

MCP was proposed by Anthropic in November 2024 as an "AI version of HTTP or SMTP" to unify data exchange between AI models and traditional platforms. Version 0618 introduced "structured tool output", allowing tool results to be returned as well‑formed JSON instead of free‑form text.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

backendMCPProtocolTool CallAnthropicJSON-RPCHTTP SSE
Ubiquitous Tech
Written by

Ubiquitous Tech

A ubiquitous public account for pirate enthusiasts, regularly sharing curated experiences, tech learning, and growth insights. Currently publishing articles on AI RAG customer service, AI MCP technology, and open-source design. Personal free Knowledge Planet: Awakening New World Programmer.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.