大模型和MCP是怎样协作的

前言

大模型与MCP(Model Context Protocol)结合处理一个问题时,通常需要调用大模型两次

  1. 获取可以使用的MCP服务
  2. 大模型决定是否及如何调用MCP工具(一次决策)
  3. MCP客户端执行工具调用(一次执行)
  4. 大模型整合结果并生成最终回答(一次总结)

OpenAI + MCP

在 OpenAI 的体系中,MCP(Model Context Protocol)本身并不是 OpenAI 原生提出的协议(最初由 Anthropic 提出),但自 2025 年 3 月起,OpenAI 宣布全面兼容 MCP,使其成为跨厂商通用标准。

因此,在 OpenAI 的 Agent 或智能体应用中,MCP 工具的调用流程实际上复用了 OpenAI 原有的 Function Calling 机制,只是底层工具由 MCP Server 提供,并通过 MCP Client 封装为符合 OpenAI tools 格式的函数。

下面是一个完整的 OpenAI + MCP 结合使用时,大模型决策调用 MCP 工具并返回结果的示例,包括两次模型调用的完整交互过程。

场景设定

用户提问:

“北京今天天气怎么样?”

系统已注册一个 MCP 工具:

  • 工具名:get_weather
  • 描述:获取指定城市的当前天气
  • 参数:{"city": "string"}

一次决策

第一步:第一次调用 OpenAI 模型(决策阶段)

请求(发送给 OpenAI API)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
{
"model": "gpt-4o",
"messages": [
{"role": "user", "content": "北京今天天气怎么样?"}
],
"tools": [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "获取指定城市的当前天气",
"parameters": {
"type": "object",
"properties": {
"city": {"type": "string", "description": "城市名称,如 '北京'、'上海'"}
},
"required": ["city"]
}
}
}
]
}

OpenAI 返回(模型决策结果)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
{
"choices": [
{
"message": {
"role": "assistant",
"content": null,
"tool_calls": [
{
"id": "call_abc123xyz",
"type": "function",
"function": {
"name": "get_weather",
"arguments": "{\"city\": \"北京\"}"
}
}
]
},
"finish_reason": "tool_calls"
}
]
}

注意

此时模型没有生成最终回答,而是输出了一个 tool_calls,表示“我需要调用 get_weather 工具,参数是 {"city": "北京"}”。

一次执行

第二步:MCP Client 调用 MCP Server 执行工具

你的应用程序(MCP Host/Client)解析 tool_calls,发现要调用 get_weather,于是:

  1. 通过 MCP 协议(如 JSON-RPC over SSE 或 stdio)向 MCP Server 发送请求;
  2. MCP Server 调用 OpenWeatherMap API 获取真实数据;
  3. 返回结构化结果,例如:
1
2
3
4
5
6
{
"temperature": 22,
"unit": "Celsius",
"condition": "多云",
"humidity": 65
}

一次总结

第三步:第二次调用 OpenAI 模型(整合结果并输出)

将工具执行结果作为新消息追加到对话历史中,再次调用模型:

请求(第二次调用)

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
{
"model": "gpt-4o",
"messages": [
{"role": "user", "content": "北京今天天气怎么样?"},
{
"role": "assistant",
"tool_calls": [
{
"id": "call_abc123xyz",
"type": "function",
"function": {
"name": "get_weather",
"arguments": "{\"city\": \"北京\"}"
}
}
]
},
{
"role": "tool",
"tool_call_id": "call_abc123xyz",
"name": "get_weather",
"content": "{\"temperature\": 22, \"unit\": \"Celsius\", \"condition\": \"多云\", \"humidity\": 65}"
}
]
}

OpenAI 返回(最终回答)

1
2
3
4
5
6
7
8
9
10
11
{
"choices": [
{
"message": {
"role": "assistant",
"content": "北京今天天气多云,气温22摄氏度,湿度65%。建议外出时携带薄外套。"
},
"finish_reason": "stop"
}
]
}

注意

经过大模型的加工,用户可以看到的最终自然语言回答。

关键点总结

阶段 模型行为 输出内容 角色
第一次调用 决策是否调用工具 tool_calls(含工具名+参数) Planner
MCP 执行 外部执行(非模型) 结构化数据(JSON) Executor
第二次调用 整合工具结果 自然语言回答 Summarizer

注意

在 OpenAI 兼容 MCP 的架构中,MCP Server 被封装为一个“Function”,通过 OpenAI 的 tools 接口暴露给模型。

模型并不知道背后是 MCP,它只看到标准的 Function Calling 格式。

补充

MCP 如何“对接”到 OpenAI 的 tools?

你的 MCP Client(如基于 openai-agents SDK)会在启动时:

  1. 连接到 MCP Server;
  2. 调用 ListTools() 获取所有可用工具的 schema;
  3. 将这些 schema 转换为 OpenAI 的 tools 格式
  4. 在每次对话前注入到 chat.completions.create(tools=...) 中。

这样就实现了 “MCP 提供能力,OpenAI 模型调用能力” 的无缝集成。