MCP 服务器与 LLM 交互流程文档
基于实际运行日志整理,记录 LLM 客户端 (Claude Code) 与 MCP 服务器的完整交互流程
核心流程概览
阶段 0: LLM 启动 MCP 服务器
触发方式: LLM 客户端读取配置文件 (如 claude_desktop_config.json),通过配置的命令启动 MCP 服务器
配置示例:
{
"mcpServers": {
"mcp-demo": {
"command": "java",
"args": ["-jar", "/path/to/mcp-demo.jar"]
}
}
}
服务器启动流程:
- 进程启动: LLM 客户端通过子进程启动 MCP 服务器
- 工具注册: 服务器内部注册所有可用工具 (calculator, weather, text_processor)
- 监听就绪: 服务器开始监听标准输入 (stdin),准备接收 JSON-RPC 请求
日志记录:
09:38:50.339 [main] INFO - === MCP Demo Server 启动 ===
09:38:50.352 [main] INFO - 已注册工具: calculator
09:38:50.360 [main] INFO - 已注册工具: weather
09:38:50.360 [main] INFO - 已注册工具: text_processor
阶段 1: 初始化握手
1.1 LLM 客户端发起初始化
请求: initialize
方向: LLM 客户端 → MCP 服务器
{
"method": "initialize",
"params": {
"protocolVersion": "2025-06-18",
"capabilities": { "roots": {} },
"clientInfo": { "name": "claude-code", "version": "2.0.67" }
},
"jsonrpc": "2.0",
"id": 0
}
关键字段:
protocolVersion: 协议版本协商capabilities: 客户端声明的能力 (如支持 roots 管理)clientInfo: 客户端身份标识
1.2 MCP 服务器响应初始化
响应: initialize result
方向: MCP 服务器 → LLM 客户端
{
"jsonrpc": "2.0",
"id": 0,
"result": {
"protocolVersion": "2025-06-18",
"capabilities": { "tools": {} },
"serverInfo": { "name": "mcp-demo-server", "version": "1.0.0" }
}
}
关键字段:
protocolVersion: 服务器确认协议版本capabilities.tools: 服务器声明支持工具调用serverInfo: 服务器身份标识
阶段 2: LLM 客户端就绪通知
通知: notifications/initialized
方向: LLM 客户端 → MCP 服务器
{
"method": "notifications/initialized",
"jsonrpc": "2.0"
}
特点:
- 这是一个单向通知 (无
id字段,MCP 服务器不需要返回响应) - 表示 LLM 客户端已完成初始化,MCP 服务器可以开始接收请求
阶段 3: 工具发现
请求: tools/list
方向: LLM 客户端 → MCP 服务器
{
"method": "tools/list",
"jsonrpc": "2.0",
"id": 1
}
响应: 返回所有可用工具的元数据
方向: MCP 服务器 → LLM 客户端
{
"jsonrpc": "2.0",
"id": 1,
"result": {
"tools": [
{
"name": "calculator",
"description": "执行基本的数学运算,支持加减乘除",
"inputSchema": {
"type": "object",
"properties": {
"operation": { "type": "string", "enum": ["add", "subtract", "multiply", "divide"] },
"a": { "type": "number" },
"b": { "type": "number" }
},
"required": ["operation", "a", "b"]
}
},
{
"name": "weather",
"description": "查询指定城市的天气信息(模拟数据)",
"inputSchema": {
"type": "object",
"properties": {
"city": { "type": "string" }
},
"required": ["city"]
}
},
{
"name": "text_processor",
"description": "处理文本,支持转换大小写、反转字符串、统计字数等操作",
"inputSchema": {
"type": "object",
"properties": {
"text": { "type": "string" },
"action": { "type": "string", "enum": ["uppercase", "lowercase", "reverse", "count"] }
},
"required": ["text", "action"]
}
}
]
}
}
工具元数据说明:
name: 工具的唯一标识符description: 功能描述 (供 LLM 理解工具用途)inputSchema: JSON Schema 格式的参数定义 (用于参数验证)
阶段 4: 工具调用 (可多次)
示例 1: 调用计算器工具
请求: tools/call
方向: LLM 客户端 → MCP 服务器
{
"method": "tools/call",
"params": {
"name": "calculator",
"arguments": {
"operation": "add",
"a": 1000,
"b": 9
}
},
"jsonrpc": "2.0",
"id": 2
}
响应: 返回执行结果
方向: MCP 服务器 → LLM 客户端
{
"jsonrpc": "2.0",
"id": 2,
"result": {
"content": [
{
"type": "text",
"text": "计算结果: 1000.0 加法 9.0 = 1009.0"
}
]
}
}
示例 2: 调用天气查询工具
请求: tools/call
方向: LLM 客户端 → MCP 服务器
{
"method": "tools/call",
"params": {
"name": "weather",
"arguments": {
"city": "北京"
}
},
"jsonrpc": "2.0",
"id": 3
}
响应: 返回执行结果
方向: MCP 服务器 → LLM 客户端
{
"jsonrpc": "2.0",
"id": 3,
"result": {
"content": [
{
"type": "text",
"text": "城市: 北京\n天气: 小雨\n温度: 33°C\n湿度: 53%\n\n(这是模拟数据,仅供演示使用)"
}
]
}
}
完整交互时序
[阶段 0: 启动]
LLM 客户端读取配置 → 启动 MCP 服务器子进程 → MCP 服务器注册工具 → 监听 stdin
[阶段 1: 握手]
LLM 客户端 → MCP 服务器: initialize (协议版本、客户端信息)
MCP 服务器 → LLM 客户端: initialize result (服务器能力、版本确认)
[阶段 2: 就绪]
LLM 客户端 → MCP 服务器: notifications/initialized (单向通知)
[阶段 3: 发现]
LLM 客户端 → MCP 服务器: tools/list
MCP 服务器 → LLM 客户端: 返回工具列表 (name, description, inputSchema)
[阶段 4: 执行]
LLM 客户端 → MCP 服务器: tools/call (name, arguments)
MCP 服务器 → LLM 客户端: 返回执行结果 (content)
(可重复多次)
[持续运行]
MCP 服务器保持运行,等待后续工具调用...
核心协议特性
通信机制
- 传输方式: 标准输入输出 (stdio)
- 协议规范: JSON-RPC 2.0
- 消息格式: 每行一个完整的 JSON 对象
- 编码: UTF-8
消息类型
- 请求 (Request): 包含
method,jsonrpc,id,需要响应 - 响应 (Response): 包含
jsonrpc,id,result或error - 通知 (Notification): 只有
method和jsonrpc,无id,不需要响应
协议版本
- 客户端和服务器必须协商一致的
protocolVersion - 本示例使用版本:
2025-06-18
错误处理
标准 JSON-RPC 错误响应格式:
{
"jsonrpc": "2.0",
"id": 2,
"error": {
"code": -32602,
"message": "Invalid params",
"data": "详细错误信息"
}
}
完整测试日志
[main] INFO com.example.mcp.Main - === MCP Demo Server 启动 ===
[main] INFO com.example.mcp.server.McpServer - 已注册工具: calculator
[main] INFO com.example.mcp.server.McpServer - 已注册工具: weather
[main] INFO com.example.mcp.server.McpServer - 已注册工具: text_processor
[main] INFO com.example.mcp.Main - 已注册 3 个测试工具
[main] INFO com.example.mcp.server.McpServer - MCP 服务器启动
[main] DEBUG com.example.mcp.server.McpServer - 收到请求: {"method":"initialize","params":{"protocolVersion":"2025-06-18","capabilities":{"roots":{}},"clientInfo":{"name":"claude-code","version":"2.0.67"}},"jsonrpc":"2.0","id":0}
[main] INFO com.example.mcp.server.McpServer - 收到初始化请求: claude-code v2.0.67
[main] INFO com.example.mcp.server.McpServer - 客户端协议版本: 2025-06-18
[main] INFO com.example.mcp.server.McpServer - 初始化成功,准备发送响应
[main] DEBUG com.example.mcp.server.McpServer - 发送响应: {"jsonrpc":"2.0","id":0,"result":{"protocolVersion":"2025-06-18","capabilities":{"tools":{}},"serverInfo":{"name":"mcp-demo-server","version":"1.0.0"}}}
[main] INFO com.example.mcp.server.McpServer - 响应已发送,method=success
[main] DEBUG com.example.mcp.server.McpServer - 收到请求: {"method":"notifications/initialized","jsonrpc":"2.0"}
[main] INFO com.example.mcp.server.McpServer - 客户端初始化完成
[main] DEBUG com.example.mcp.server.McpServer - 收到请求: {"method":"tools/list","jsonrpc":"2.0","id":1}
[main] DEBUG com.example.mcp.server.McpServer - 发送响应: {"jsonrpc":"2.0","id":1,"result":{"tools":[{"name":"calculator","description":"执行基本的数学运算,支持加减乘除","inputSchema":{"type":"object","properties":{"operation":{"type":"string","enum":["add","subtract","multiply","divide"],"description":"要执行的运算类型"},"a":{"type":"number","description":"第一个数字"},"b":{"type":"number","description":"第二个数字"}},"required":["operation","a","b"]}},{"name":"weather","description":"查询指定城市的天气信息(模拟数据)","inputSchema":{"type":"object","properties":{"city":{"type":"string","description":"要查询的城市名称"}},"required":["city"]}},{"name":"text_processor","description":"处理文本,支持转换大小写、反转字符串、统计字数等操作","inputSchema":{"type":"object","properties":{"text":{"type":"string","description":"要处理的文本"},"action":{"type":"string","enum":["uppercase","lowercase","reverse","count"],"description":"要执行的操作: uppercase(转大写), lowercase(转小写), reverse(反转), count(统计)"}},"required":["text","action"]}}]}}
[main] INFO com.example.mcp.server.McpServer - 响应已发送,method=success
参考文档
mcpsdk:https://github.com/modelcontextprotocol
已有的mcp:https://github.com/modelcontextprotocol/servers