MCP全称 Model Context Protocol,为AI模型推理提供标准化的上下文信息交互协议

// 和 mcp 服务器打交道遵循 JSON-RPC 结构
// mcp客户端请求mcp服务器
{
"jsonrpc": "2.0",
"id": "1",
"method": "tools/list"
}
// 响应
{
"jsonrpc": "2.0",
"id": "1",
"result": {
"tools": [{
"name": "get_weather",
"description": "查询城市天气",
"inputSchema": {
"type": "object",
"properties": {
"city": {"type": "string"}
}
}
}]
}
}
// 和 LLM 打交道用 function call(tools接受一个数组,意味着可以有多个工具)
// 将查询和工具列表发送给LLM
{
"messages": [{
"role": "user",
"content": "北京今天多少度?"
}],
"tools": [{
"name": "get_weather",
"description": "查询城市天气",
"inputSchema": {
"type": "object",
"properties": {"city": {"type": "string"}}
}
}]
}
// LLM 响应:使用推理能力获取具体需要调用的能力
{
"tool_calls": [{
"name": "get_weather",
"arguments": {"city": "北京"}
}]
}
// 和 mcp 服务器打交道遵循 JSON-RPC 结构
// 请求
{
"jsonrpc": "2.0",
"id": "2",
"method": "tools/call",
"params": {
"name": "get_weather",
"arguments": {"city": "北京"}
}
}
// 响应(MCP Server返回第三方API数据)
{
"jsonrpc": "2.0",
"id": "2",
"result": {
"content": [{
"type": "text",
"text": "{\\"temp\\": 25, \\"condition\\": \\"晴\\"}"
}]
}
}
// mcp 客户端请求
{
"messages": [
{"role": "user", "content": "北京今天多少度?"},
{"role": "tool", "content": "{\\"temp\\": 25, \\"condition\\": \\"晴\\"}"}
]
}
// LLM 响应
{
"role": "assistant",
"content": "北京今天晴,气温25°C"
}