用户只说人话,不带任何结构。应用要做两件事:把它塞进 messages,把可用工具准备好。
input用户原话
帮我查下订单 A20261127001 的物流,如果已经签收了,告诉我签收时间。
context应用组装出的初始 messages
[
{
"role": "system",
"content": "你是订单助手。涉及订单状态、物流、签收时间的问题,必须先调用工具查询,禁止凭常识推测。"
},
{
"role": "user",
"content": "帮我查下订单 A20261127001 的物流,如果已经签收了,告诉我签收时间。"
}
]
system 里写明「必须先调工具、禁止推测」,能明显降低模型凭常识编造物流状态的概率。
调工具之前必须先握手。没有握手就拿不到 Session-Id,后面所有请求都会被 400 拒绝。
requestJSON-RPC · initialize复制
POST https://mcp.example.com/order HTTP/1.1
Content-Type: application/json
Accept: application/json, text/event-stream
Authorization: Bearer <MCP_TOKEN>
{
"jsonrpc": "2.0",
"id": 0,
"method": "initialize",
"params": {
"protocolVersion": "2025-06-18",
"capabilities": {},
"clientInfo": { "name": "order-bot", "version": "1.0.0" }
}
}
Accept 必须同时包含 application/json 和 text/event-stream,少一个就协商失败。
服务端在响应头里下发 Mcp-Session-Id。记住它,之后每一个 MCP 请求都要带上。
response200 OK复制
HTTP/1.1 200 OK
Content-Type: application/json
Mcp-Session-Id: 7c1f9a2e-4b6d-4f83-9a11-2d5e8b0c3a77
{
"jsonrpc": "2.0",
"id": 0,
"result": {
"protocolVersion": "2025-06-18",
"capabilities": { "tools": { "listChanged": true } },
"serverInfo": { "name": "order-mcp", "version": "2.3.1" }
}
}
follow-up握手完成的确认通知(无返回)复制
{ "jsonrpc": "2.0", "method": "notifications/initialized" }
这条 notifications/initialized 没有 id,是不需要响应的单向通知,但漏发会让部分服务端保持未就绪状态。
这就是「给出 MCP」那一步。清单只需取一次并缓存,不必每轮对话都问。
requestJSON-RPC · tools/list复制
POST https://mcp.example.com/order HTTP/1.1
Content-Type: application/json
Accept: application/json, text/event-stream
Mcp-Session-Id: 7c1f9a2e-4b6d-4f83-9a11-2d5e8b0c3a77
Authorization: Bearer <MCP_TOKEN>
{
"jsonrpc": "2.0",
"id": 1,
"method": "tools/list",
"params": {}
}
每个工具三件套:name / description / inputSchema。它们会被原样翻译成 OpenAI 的 tools。
responseresult.tools[]复制
{
"jsonrpc": "2.0",
"id": 1,
"result": {
"tools": [
{
"name": "get_order",
"description": "按订单号查询订单主信息:金额、订单状态、下单时间、收货城市。",
"inputSchema": {
"type": "object",
"properties": {
"order_no": {
"type": "string",
"description": "订单号,形如 A20261127001"
}
},
"required": ["order_no"],
"additionalProperties": false
}
},
{
"name": "get_logistics",
"description": "查询订单的物流轨迹与签收状态。需要判断是否签收、何时签收时使用。",
"inputSchema": {
"type": "object",
"properties": {
"order_no": { "type": "string", "description": "订单号" },
"with_trace": {
"type": "boolean",
"default": true,
"description": "是否返回完整轨迹节点"
}
},
"required": ["order_no"],
"additionalProperties": false
}
}
]
}
}
description 不是注释,它就是提示词。写得越具体,模型选错工具的概率越低。
转换规则只有三条:name 照抄、description 照抄、inputSchema 搬到 parameters,外层统一包成 {"type":"function",…}。
requestchat.completions复制
POST https://api.openai.com/v1/chat/completions HTTP/1.1
Authorization: Bearer <OPENAI_API_KEY>
Content-Type: application/json
{
"model": "gpt-4.1",
"messages": [
{ "role": "system", "content": "你是订单助手。涉及订单状态、物流、签收时间的问题,必须先调用工具查询,禁止凭常识推测。" },
{ "role": "user", "content": "帮我查下订单 A20261127001 的物流,如果已经签收了,告诉我签收时间。" }
],
"tools": [
{
"type": "function",
"function": {
"name": "get_order",
"description": "按订单号查询订单主信息:金额、订单状态、下单时间、收货城市。",
"parameters": {
"type": "object",
"properties": {
"order_no": { "type": "string", "description": "订单号,形如 A20261127001" }
},
"required": ["order_no"],
"additionalProperties": false
}
}
},
{
"type": "function",
"function": {
"name": "get_logistics",
"description": "查询订单的物流轨迹与签收状态。需要判断是否签收、何时签收时使用。",
"parameters": {
"type": "object",
"properties": {
"order_no": { "type": "string", "description": "订单号" },
"with_trace": { "type": "boolean", "default": true, "description": "是否返回完整轨迹节点" }
},
"required": ["order_no"],
"additionalProperties": false
}
}
}
],
"tool_choice": "auto",
"parallel_tool_calls": true,
"temperature": 0.2
}
parallel_tool_calls: true 时模型可能一次返回多个 call(比如同时查订单和物流),每个都得单独回一条 role=tool,缺一个就 400。
注意 content 为 null、finish_reason 为 tool_calls。模型只负责发出调用意向,HTTP 请求是你的代码发的。
response200 OK · chat.completion复制
{
"id": "chatcmpl-Ck9x2pQ7s1xZ0kLm",
"object": "chat.completion",
"created": 1795768740,
"model": "gpt-4.1-2025-04-14",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": null,
"refusal": null,
"tool_calls": [
{
"id": "call_pQ7s1xZ0kLmN8vR2",
"type": "function",
"function": {
"name": "get_logistics",
"arguments": "{\"order_no\":\"A20261127001\",\"with_trace\":true}"
}
}
]
},
"finish_reason": "tool_calls"
}
],
"usage": { "prompt_tokens": 318, "completion_tokens": 31, "total_tokens": 349 }
}
arguments 是一段 JSON 字符串,不是对象。先 json.loads 再交给 MCP,并且要 try/except —— 模型偶尔会产出非法 JSON。
把 tool_call.id 留在手里,把 function.name 当 params.name,把解析后的 arguments 当 params.arguments。这一步与 OpenAI 无关,是标准 JSON-RPC。
requestJSON-RPC · tools/call复制
POST https://mcp.example.com/order HTTP/1.1
Content-Type: application/json
Accept: application/json, text/event-stream
Mcp-Session-Id: 7c1f9a2e-4b6d-4f83-9a11-2d5e8b0c3a77
Authorization: Bearer <MCP_TOKEN>
{
"jsonrpc": "2.0",
"id": 42,
"method": "tools/call",
"params": {
"name": "get_logistics",
"arguments": {
"order_no": "A20261127001",
"with_trace": true
}
}
}
JSON-RPC 的 id(这里是 42)是你自己编的,跟 tool_call.id 没有关系,别混用。
结果是 数组,元素可能是 text / image / resource。isError 为 true 时也别抛异常 —— 把错误文本照样回填,模型通常能自己改参数重试。
responseJSON-RPC · result复制
{
"jsonrpc": "2.0",
"id": 42,
"result": {
"content": [
{
"type": "text",
"text": "{\"order_no\":\"A20261127001\",\"carrier\":\"顺丰速运\",\"waybill\":\"SF1234567890123\",\"status\":\"SIGNED\",\"status_text\":\"已签收\",\"signed_at\":\"2026-11-26T15:42:08+08:00\",\"receiver\":\"张*\",\"trace\":[{\"at\":\"2026-11-25T09:12:00+08:00\",\"node\":\"杭州转运中心 已揽收\"},{\"at\":\"2026-11-26T06:30:00+08:00\",\"node\":\"到达上海浦东派送点\"},{\"at\":\"2026-11-26T15:42:08+08:00\",\"node\":\"已签收,签收人:本人\"}]}"
}
],
"structuredContent": {
"order_no": "A20261127001",
"status": "SIGNED",
"signed_at": "2026-11-26T15:42:08+08:00"
},
"isError": false
}
}
parsedcontent[0].text 反序列化后复制
{
"order_no": "A20261127001",
"carrier": "顺丰速运",
"waybill": "SF1234567890123",
"status": "SIGNED",
"status_text": "已签收",
"signed_at": "2026-11-26T15:42:08+08:00",
"receiver": "张*",
"trace": [
{ "at": "2026-11-25T09:12:00+08:00", "node": "杭州转运中心 已揽收" },
{ "at": "2026-11-26T06:30:00+08:00", "node": "到达上海浦东派送点" },
{ "at": "2026-11-26T15:42:08+08:00", "node": "已签收,签收人:本人" }
]
}
回填时把 content 各元素拍平成一个字符串更省 token。直接回填上面这段业务 JSON,比再套一层 {"content":[…]} 更好懂。
三条铁律:assistant 那条带 tool_calls 的消息必须原样带上;tool_call_id 必须一一对应;tools 定义再传一次。
requestchat.completions · 第二轮复制
{
"model": "gpt-4.1",
"messages": [
{
"role": "system",
"content": "你是订单助手。涉及订单状态、物流、签收时间的问题,必须先调用工具查询,禁止凭常识推测。"
},
{
"role": "user",
"content": "帮我查下订单 A20261127001 的物流,如果已经签收了,告诉我签收时间。"
},
{
"role": "assistant",
"content": null,
"tool_calls": [
{
"id": "call_pQ7s1xZ0kLmN8vR2",
"type": "function",
"function": {
"name": "get_logistics",
"arguments": "{\"order_no\":\"A20261127001\",\"with_trace\":true}"
}
}
]
},
{
"role": "tool",
"tool_call_id": "call_pQ7s1xZ0kLmN8vR2",
"name": "get_logistics",
"content": "{\"order_no\":\"A20261127001\",\"carrier\":\"顺丰速运\",\"waybill\":\"SF1234567890123\",\"status\":\"SIGNED\",\"status_text\":\"已签收\",\"signed_at\":\"2026-11-26T15:42:08+08:00\",\"receiver\":\"张*\",\"trace\":[{\"at\":\"2026-11-25T09:12:00+08:00\",\"node\":\"杭州转运中心 已揽收\"},{\"at\":\"2026-11-26T06:30:00+08:00\",\"node\":\"到达上海浦东派送点\"},{\"at\":\"2026-11-26T15:42:08+08:00\",\"node\":\"已签收,签收人:本人\"}]}"
}
],
"tools": [ "…与请求①完全相同的两个工具定义…" ],
"tool_choice": "auto",
"temperature": 0.2
}
content 必须是字符串。塞对象进去不会报「类型错」,而是直接 400,报错信息还很难懂。
这次 content 有值、finish_reason 为 stop、没有 tool_calls —— 说明模型认为信息够了。
response200 OK · chat.completion复制
{
"id": "chatcmpl-Ck9x9tR3mB1yQw7e",
"object": "chat.completion",
"created": 1795768752,
"model": "gpt-4.1-2025-04-14",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "订单 A20261127001 已经签收了。\n\n- 承运商:顺丰速运,运单号 SF1234567890123\n- 签收时间:2026-11-26 15:42(北京时间)\n- 签收人:本人\n\n轨迹:11-25 09:12 杭州转运中心揽收 → 11-26 06:30 到达上海浦东派送点 → 11-26 15:42 已签收。",
"refusal": null
},
"finish_reason": "stop"
}
],
"usage": { "prompt_tokens": 612, "completion_tokens": 104, "total_tokens": 716 }
}
工程上一定要给这个循环设最大轮数(比如 6 轮)。模型有时候会来回调同一个工具,不设上限就是无限烧钱。
最后一步不涉及任何 API:把模型的 Markdown 渲染出来,整条链路结束。整个往返通常 2~4 秒。
output页面渲染结果
订单 A20261127001 已经签收了。
· 承运商:顺丰速运,运单号 SF1234567890123
· 签收时间:2026-11-26 15:42(北京时间)
· 签收人:本人
轨迹:11-25 09:12 杭州转运中心揽收 → 11-26 06:30
到达上海浦东派送点 → 11-26 15:42 已签收。
注意时间:MCP 返回的是带时区的 ISO 时间(+08:00),模型转述时最好显式标注「北京时间」,避免跨时区用户误读。