MCPRequest
){
"messages": [
{
"role": "user",
"content": "帮我查询一下上海的天气"
}
],
"tools": [
{
"name": "weather_query",
"description": "查询天气",
"parameters": {
"type": "object",
"properties": {
"location": { "type": "string", "description": "查询地点" }
},
"required": ["location"]
}
}
]
}
messages
: 聊天上下文tools
: 提供可调用的工具(函数),使用 JSON Schema 描述tool_calls
请求调用工具MCPResponse
){
"choices": [
{
"message": {
"role": "assistant",
"tool_calls": [
{
"id": "call_123",
"type": "function",
"function": {
"name": "weather_query",
"arguments": "{\"location\": \"上海\"}"
}
}
]
},
"finish_reason": "tool_calls"
}
]
}
tool_calls
: 模型请求工具调用finish_reason = tool_calls
: 模型等待外部执行工具{
"messages": [
{
"role": "user",
"content": "帮我查询一下上海的天气"
},
{
"role": "assistant",
"tool_calls": [...]
},
{
"role": "tool",
"tool_call_id": "call_123",
"content": "{\"weather\": \"晴天,28°C\"}"
}
]
}
role: tool
返回,继续对话tool_calls
tool
消息)字段名 | 说明 |
---|---|
messages |
对话上下文 |
tools |
工具定义(JSON Schema) |
tool_calls |
模型生成的调用请求 |
tool_call_id |
工具调用唯一标识 |
tool |
工具返回结果 |
finish_reason |
模型是否结束、是否等待工具调用 |
MCP 协议的核心在于:通过结构化的工具定义(
tools
)、模型调用请求(tool_calls
)以及工具结果反馈(tool
),实现大模型的可控、结构化、插件化调用闭环。