MCP与LLM交互流程

/ 0评 / 0

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"]
    }
  }
}

服务器启动流程:

  1. 进程启动: LLM 客户端通过子进程启动 MCP 服务器
  2. 工具注册: 服务器内部注册所有可用工具 (calculator, weather, text_processor)
  3. 监听就绪: 服务器开始监听标准输入 (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
}

关键字段:

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" }
  }
}

关键字段:


阶段 2: LLM 客户端就绪通知

通知: notifications/initialized
方向: LLM 客户端 → MCP 服务器

{
  "method": "notifications/initialized",
  "jsonrpc": "2.0"
}

特点:


阶段 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"]
        }
      }
    ]
  }
}

工具元数据说明:


阶段 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 服务器保持运行,等待后续工具调用...

核心协议特性

通信机制

消息类型

  1. 请求 (Request): 包含 method, jsonrpc, id,需要响应
  2. 响应 (Response): 包含 jsonrpc, id, resulterror
  3. 通知 (Notification): 只有 methodjsonrpc,无 id,不需要响应

协议版本

错误处理

标准 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

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注