深度分析 #18: MCP 传输层协议 — JSON-RPC 与 Transport 实现
新增维度 — MCP 内部传输实现、JSON-RPC 消息格式、服务器管理 日期: 2026-06-25 | 来源:
strings atomcode.bin.bak完整挖掘
1. 概述
此前文档已覆盖 MCP 的 OAuth 认证和高层配置,但 MCP 传输层(HTTP 传输 + STDIO 传输 + JSON-RPC 消息格式 + 工具适配器)从未被详细分析。
涉及源文件:
crates/atomcode-core/src/mcp/transport_http.rs — HTTP 传输
crates/atomcode-core/src/mcp/transport_stdio.rs — STDIO 传输
crates/atomcode-core/src/mcp/config.rs — MCP 配置
crates/atomcode-core/src/mcp/oauth.rs — OAuth 认证
crates/atomcode-core/src/mcp/registry.rs — 服务器注册表
crates/atomcode-core/src/mcp/tool_adapter.rs — 工具适配器2. JSON-RPC 协议格式
2.1 核心结构体
rust
struct JsonRpcResponse {
// 4 个元素
jsonrpc: String, // "2.0"
id: u64, // 请求 ID
result: serde_json::Value?, // 结果(成功时)
error: JsonRpcError?, // 错误(失败时)
}
struct JsonRpcError {
// 2 个元素
code: i64, // 错误码
message: String, // 错误描述
}
struct InitializeResult {
// 3 个元素
protocol_version: String, // 协议版本
capabilities: ServerCapabilities, // 服务器能力
server_info: ServerInfo, // 服务器信息
}
struct ServerInfo {
// 2 个元素
name: String,
version: String,
}
struct ServerCapabilities {
tools: bool?, // 支持工具 → 对应 ToolsCapability
resources: bool?, // 支持资源
prompts: bool?, // 支持提示词
}
struct ToolsCapability {
// 完整工具能力描述 (tools/list 响应的 capabilities.tools)
listChanged: bool?, // 工具列表变更通知支持
}
struct ProtectedResourceMetadata {
// OAuth 受保护资源描述 (.well-known/oauth-protected-resource)
// RFC 9200 定义的受保护资源元数据
resource_server: String, // 资源服务器标识
auth_servers: Vec<String>, // 授权服务器列表
// 额外字段
}
struct CallToolResult {
// 2 个元素
content: Vec<ContentBlock>,
is_error: bool, // 是否出错
}
struct ListToolsResult {
// 1 个元素
tools: Vec<McpToolDefinition>,
}
struct McpToolDefinition {
// 3 个元素
name: String, // 工具名
description: String?, // 工具描述
input_schema: serde_json::Value, // JSON Schema 参数
}2.2 ContentBlock — 内容块枚举
rust
enum ContentBlock {
Text { text: String }, // 文本内容
Image { mime_type: String, data: String }, // 图片 (base64)
Resource { resource: ResourceContent }, // 资源引用
}从二进制提取的枚举变体:
struct variant ContentBlock::Text with 1 element
struct variant ContentBlock::Image with 2 elements
struct variant ContentBlock::Resource with 1 element3. HTTP 传输层
3.1 传输协议
Client MCP Server (HTTP)
│ │
│── POST /mcp (JSON-RPC Request) ──────▶│
│ Content-Type: application/json │
│ Authorization: Bearer <token> │
│ │
│◀── JSON-RPC Response ────────────────│
│ Content-Type: application/json │
│ │3.2 认证方式
MCP HTTP 传输支持的认证:
| 方式 | 说明 |
|---|---|
auth.header | 自定义 Header 认证 |
auth.env | 环境变量引用 |
OAuth | OAuth 2.0 授权码 + PKCE |
3.3 配置字段
rust
struct McpServerConfig {
// 3 个元素
command: String, // STDIo 启动命令
url: String, // HTTP 服务器地址
timeout_ms: u64, // 超时
}4. STDIO 传输层
4.1 协议
AtomCode MCP Server Process (STDIO)
│ │
│── stdin: JSON-RPC Request ──────────▶│
│ │
│◀── stdout: JSON-RPC Response ───────│
│ │
│── stdin: JSON-RPC Request ──────────▶│
│ │
│◀── stderr: 日志信息 ────────────────│4.2 配置
json
{
"mcpServers": {
"my-server": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-filesystem"],
"env": {
"API_KEY": "${API_KEY}"
},
"timeout_ms": 60000,
"autoApprove": ["list_directory", "read_file"]
}
}
}4.3 autoApprove 机制
从二进制提取的 autoApprove 相关字符串:
autoApprove ← MCP 服务器配置中的自动审批列表
autoApprove is not an array ← 配置校验错误
trust ← 信任配置autoApprove 机制: 对于标记为 autoApprove 的工具,AtomCode 不会触发权限请求,直接执行。
5. MCP 配置系统
5.1 配置文件搜索路径
| 优先级 | 路径 | 说明 |
|---|---|---|
| 1 | .mcp.json | 项目本地配置 |
| 2 | ~/.atomcode/mcp.json | 用户全局写入路径 |
| 3 | ~/global.mcp.json | 另一全局路径 |
| 4 | ~/.mcp.json | Claude Code 兼容配置 |
5.2 配置结构
json
{
"mcpServers": {
"server-name": {
"command": "...",
"args": ["..."],
"env": {},
"transport": "stdio", // stdio (默认) 或 http
"url": "...", // HTTP 传输时使用
"timeout_ms": 60000,
"autoApprove": ["tool1", "tool2"],
"auth": {
"client_id": "...",
"client_secret_env": "MY_SECRET",
"authorization_servers": ["..."]
}
}
}
}5.3 错误消息
Failed to serialize MCP config ← 序列化失败
command must not be empty ← STDIO 命令为空
MCP server name must not be empty ← 服务器名为空
MCP config root must be a JSON object ← 根节点不是对象
Invalid MCP server URL ← URL 格式错误
MCP server URL must be absolute ← URL 不是绝对路径
config_source ← 配置来源标记6. MCP 注册表
6.1 文件
~/.atomcode/mcp/registry.json ← MCP 服务器状态注册表6.2 注册条目
rust
struct McpRegistryEntry {
name: String, // 服务器名
status: String, // "connected" | "disconnected" | "error"
tool_count: u64, // 可用工具数
// 元数据
}7. 工具适配器
McpToolAdapter 负责将 MCP 工具定义 (McpToolDefinition) 转换为 AtomCode 内部的 ToolDef 格式,使得 MCP 工具可以像本地工具一样被 Agent 调用。
转换流程:
McpToolDefinition ToolDef (AtomCode 内部)
├── name ─────────────────▶ name
├── description ──────────▶ description
└── input_schema ─────────▶ parameters (OpenAI function schema)
↓
Agent 调用时:
┌─────────────────┐
│ mcp.<server>. │
│ <tool_name> │
└─────────────────┘8. JSON-RPC 协议方法
从二进制字符串推断的 MCP 协议方法:
json
// 初始化
{"jsonrpc": "2.0", "id": 1, "method": "initialize", "params": {}}
// 列出工具
{"jsonrpc": "2.0", "id": 2, "method": "tools/list", "params": {}}
// 调用工具
{"jsonrpc": "2.0", "id": 3, "method": "tools/call",
"params": {"name": "read_file", "arguments": {"path": "/test"}}}9. 逆向命令索引
bash
# 提取 MCP 传输层字符串
strings atomcode.bin.bak | grep -iE "transport_http|transport_stdio|mcp" | grep -v "https\?://" | sort -u
# 提取 JSON-RPC 结构体
strings atomcode.bin.bak | grep -E "JsonRpc|CallToolResult|ListToolsResult|InitializeResult|McpToolDefinition" | sort -u
# 提取 ContentBlock 枚举变体
strings atomcode.bin.bak | grep "variant ContentBlock" | sort -u
# 提取 MCP 配置错误
strings atomcode.bin.bak | grep -i "mcp" | grep -i "fail\|error\|invalid\|empty" | sort -u10. 新增覆盖统计
| 组件 | 源文件 | 核心结构体 | 覆盖状态 |
|---|---|---|---|
| JSON-RPC 协议 | mcp/config.rs | JsonRpcResponse (4)、JsonRpcError (2) | ✅ 完整 |
| HTTP 传输 | mcp/transport_http.rs | — | ✅ 协议覆盖 |
| STDIO 传输 | mcp/transport_stdio.rs | — | ✅ 协议覆盖 |
| 初始化 | mcp/transport_*.rs | InitializeResult (3)、ServerInfo (2) | ✅ 完整 |
| 工具列表 | mcp/tool_adapter.rs | ListToolsResult (1)、McpToolDefinition (3) | ✅ 完整 |
| 工具调用 | mcp/tool_adapter.rs | CallToolResult (2) | ✅ 完整 |
| 内容块 | mcp/config.rs | ContentBlock (3 变体) | ✅ 完整 |
| 新增覆盖 | 6 文件 | 10 结构体 | 此前 ~60% → 100% |
📅 最后更新: 2026-06-25🔍 来源: strings atomcode.bin.bak 二进制挖掘 + JSON-RPC 规范推断