Model Context Protocol 模型上下文协议。但却没有规定与模型如何进行交互,而是规定了 Host 和 Server 之间的交互,是让模型感知外部环境的协议,所以称为模型上下文协议,MCP规定了统一的工具发现和调用协议

MCP基础

MCP 的用途

简单来说,MCP 是能够让大模型更好地调用各类工具的协议,大模型只能进行问答而无法主动调用外部工具,而 MCP 的出现使大模型拥有了使用各种外部工具的能力,这是很重要的一个能力,我们熟悉的 Cursor 本身就是一个 MCP Host

MCP server

一个符合 MCP 协议的可执行程序,通常由 python 和 node.js 编写,可以本地使用可以联网使用,我们可以帮它们理解成一个一个工具,每个工具可以解决一个具体问题,例如查询天气、查询新闻、查看文件夹…

MCP 进阶

分析明白这段日志,就能理解 MCP 协议的内容了

完整交互日志

初始化连接

客户端请求:

1
2
3
4
5
6
7
8
9
10
11
12
13
{
"method": "initialize",
"params": {
"protocolVersion": "2024-11-05",
"capabilities": {},
"clientInfo": {
"name": "Cline",
"version": "3.12.3"
}
},
"jsonrpc": "2.0",
"id": 0
}

服务器响应:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
{
"jsonrpc": "2.0",
"id": 0,
"result": {
"protocolVersion": "2024-11-05",
"capabilities": {
"experimental": {},
"prompts": {
"listChanged": false
},
"resources": {
"subscribe": false,
"listChanged": false
},
"tools": {
"listChanged": false
}
},
"serverInfo": {
"name": "weather",
"version": "1.6.0"
}
}
}

初始化完成通知:

1
2
3
4
{
"method": "notifications/initialized",
"jsonrpc": "2.0"
}

查询工具列表

客户端请求:

1
2
3
4
5
{
"method": "tools/list",
"jsonrpc": "2.0",
"id": 1
}

服务器响应:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
{
"jsonrpc": "2.0",
"id": 1,
"result": {
"tools": [
{
"name": "get_alerts",
"description": "Get weather alerts for a US state.\n\nArgs:\n state: Two-letter US state code (e.g. CA, NY)\n",
"inputSchema": {
"properties": {
"state": {
"title": "State",
"type": "string"
}
},
"required": ["state"],
"title": "get_alertsArguments",
"type": "object"
}
},
{
"name": "get_forecast",
"description": "Get weather forecast for a location.\n\nArgs:\n latitude: Latitude of the location\n longitude: Longitude of the location\n",
"inputSchema": {
"properties": {
"latitude": {
"title": "Latitude",
"type": "number"
},
"longitude": {
"title": "Longitude",
"type": "number"
}
},
"required": ["latitude", "longitude"],
"title": "get_forecastArguments",
"type": "object"
}
}
]
}
}

查询资源

查询资源列表:

1
2
3
4
5
{
"method": "resources/list",
"jsonrpc": "2.0",
"id": 2
}

服务器响应(无资源):

1
2
3
4
5
6
7
{
"jsonrpc": "2.0",
"id": 2,
"result": {
"resources": []
}
}

查询资源模板:

1
2
3
4
5
{
"method": "resources/templates/list",
"jsonrpc": "2.0",
"id": 3
}

服务器响应(无模板):

1
2
3
4
5
6
7
{
"jsonrpc": "2.0",
"id": 3,
"result": {
"resourceTemplates": []
}
}

调用工具

调用天气预报工具:

1
2
3
4
5
6
7
8
9
10
11
12
{
"method": "tools/call",
"params": {
"name": "get_forecast",
"arguments": {
"latitude": 40.7128,
"longitude": -74.006
}
},
"jsonrpc": "2.0",
"id": 4
}

获取天气预报结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
{
"jsonrpc": "2.0",
"id": 4,
"result": {
"content": [
{
"type": "text",
"text": "\nToday:\nTemperature: 64°F\nWind: 2 to 18 mph S\nForecast: Mostly sunny. High near 64, with temperatures falling to around 62 in the afternoon. South wind 2 to 18 mph, with gusts as high as 30 mph.\n\n---\n\nTonight:\nTemperature: 57°F\nWind: 12 to 17 mph S\nForecast: Mostly cloudy. Low around 57, with temperatures rising to around 59 overnight. South wind 12 to 17 mph, with gusts as high as 29 mph.\n\n---\n\nSaturday:\nTemperature: 78°F\nWind: 12 to 21 mph SW\nForecast: Partly sunny, with a high near 78. Southwest wind 12 to 21 mph, with gusts as high as 32 mph.\n\n---\n\nSaturday Night:\nTemperature: 57°F\nWind: 15 to 18 mph W\nForecast: A chance of rain showers between 8pm and 2am. Mostly cloudy. Low around 57, with temperatures rising to around 61 overnight. West wind 15 to 18 mph. Chance of precipitation is 30%.\n\n---\n\nSunday:\nTemperature: 62°F\nWind: 14 to 17 mph NW\nForecast: Partly sunny, with a high near 62. Northwest wind 14 to 17 mph.\n"
}
],
"isError": false
}
}

日志详细解读

上面这段日志完整地展示了一次MCP客户端与服务器的交互过程,我们来逐步分析:

第一阶段:连接初始化

客户端发起连接

  • method: "initialize":客户端请求初始化连接
  • protocolVersion: "2024-11-05":使用的MCP协议版本
  • clientInfo:客户端信息,这里是Cline编辑器(版本3.12.3)
  • capabilities: {}:客户端声明自己的能力(这里为空)

服务器响应

  • protocolVersion:确认使用相同的协议版本
  • capabilities:服务器能力声明
    • tools:支持工具功能,listChanged: false 表示工具列表不会动态变化
    • resources:支持资源功能,但不支持订阅机制
    • prompts:支持提示词功能
  • serverInfo:服务器信息,这是一个名为”weather”的天气服务器(版本1.6.0)

初始化完成通知:客户端告知服务器初始化已完成,可以开始正常通信。

第二阶段:能力发现

请求工具列表:客户端查询服务器提供哪些工具。

服务器返回工具列表

  1. get_alerts工具

    • 功能:获取美国各州的天气警报
    • 参数:state(必需),两位州代码(如CA、NY)
  2. get_forecast工具

    • 功能:获取指定位置的天气预报
    • 参数:latitudelongitude(必需),地理坐标

每个工具都包含详细的inputSchema,定义了参数的类型和验证规则。

第三阶段:资源查询

查询资源:客户端检查是否有可用的资源和资源模板,这个天气服务器没有提供任何资源。

第四阶段:工具调用

调用天气预报工具

  • 工具名称:get_forecast
  • 参数:纽约市的经纬度(40.7128, -74.006)

获取天气预报结果

  • 返回详细的5天天气预报
  • 包含温度、风速、天气状况等信息
  • isError: false 表示调用成功

交互流程总结

这个完整的MCP交互展示了标准的工作流程:

  1. 建立连接:协议版本协商,能力声明
  2. 能力发现:查询可用工具、资源和模板
  3. 工具调用:根据需要调用特定工具
  4. 获取结果:接收处理结果并展示

这种标准化的交互方式使得不同的客户端(如Cline、Claude Desktop)都能以相同的方式与各种MCP服务器通信,实现了良好的互操作性。

关键技术细节

  • JSON-RPC 2.0:使用标准的JSON-RPC协议进行通信
  • 请求ID:每个请求都有唯一ID,用于匹配请求和响应
  • 类型安全:通过JSON Schema严格定义参数类型
  • 错误处理:明确的错误状态和错误信息返回机制

通过jsin schema 可以定义参数类型,从而实现类型安全,同时也可以通过错误信息返回机制来提示错误,从而实现错误处理。