Agent 和 tool calling 怎么工作?
MCP 标准化了什么?
先弄清楚:每个概念是什么
- Agent
- LLM 在循环里自己做决定:看现状 → 选动作 → 看结果,直到任务完成。
- Tool calling(function calling)
- 给模型一份工具说明书。模型不执行,只输出一个结构化调用,由 runtime 执行后把结果放回 context。例:模型输出
get_weather({"location": "New York"}),runtime 返回 "72°F, partly cloudy",模型据此回答。 - ReAct
- Thought → Action → Observation 交替。Thought 是写给自己的推理,不影响环境。Action 调工具。Observation 是工具返回。
- MCP(Model Context Protocol)
- LLM 应用连接外部 tool 和数据的开放协议,消息用 JSON-RPC 2.0,思路来自 Language Server Protocol。host 是 LLM 应用,里面每个 client 只连一个 server,server 暴露 tools、resources、prompts。例:5 个应用 × 20 个工具,各自对接要写 100 个集成;都讲 MCP 时只要 5 个 client 实现 + 20 个 server。
30 秒回答
We give the model a list of tools, each with a name, a description and a JSON Schema for its arguments. The model doesn't execute anything: it emits a structured call, a function name plus JSON arguments. The runtime validates and executes it, appends the result to the context and calls the model again, until the model answers. ReAct is this loop with explicit reasoning: thought, action, observation. MCP is an open protocol on JSON-RPC that standardizes how applications connect to tools and data: a host runs one client per server, and servers expose tools, resources and prompts, so a tool is implemented once and reused by every app.
一轮 tool call 怎么走
- 把 tools 的 name、description、JSON Schema 放进请求(MCP 里由 client 调
tools/list拿到)。 - 模型输出
{name, arguments}并停止生成。 - runtime 校验参数、检查权限(敏感操作先问用户),再执行(MCP 里是
tools/call)。 - 结果或错误信息追加进 context,再调模型;直到模型直接回答或到 max steps。
MCP 的三种 server 原语
| 原语 | 谁决定用 | 例子 | 方法 |
|---|---|---|---|
| Tools | model-controlled:模型决定调 | 查天气、跑 SQL | tools/list、tools/call |
| Resources | application-driven:应用决定放进 context | 文件、DB schema,用 URI 标识 | resources/list、resources/read |
| Prompts | user-controlled:用户从菜单选 | slash command 模板 | prompts/list、prompts/get |
传输层:本地用 stdio(client 启动 server 子进程),远程用 Streamable HTTP。
为什么不预先把 tool output 塞进 prompt(deep research 组原题)
- 事先不知道调哪个 tool、调几次
- 后一步的 argument 依赖前一步的结果,比如先 search 拿到 URL 才能 fetch
- 全部预跑一遍占 context、费钱,结果还会过期
- 有些 tool 是动作不是数据(发邮件、写数据库),不能"预先执行"
常见失败和对策
- 选错 tool、编造 argument:按 JSON Schema 校验,把错误信息回给模型重试,见 tool argument。
- 卡在循环里:反复生成同样的 thought 和 action(ReAct 在 HotpotQA 上 47% 的失败是 reasoning error,含这种 loop)。→ 设 max steps,检测重复。
- 工具返回没用的信息:占 ReAct 失败的 23%。→ 改进检索、改写 query。
- error compounding:每步 95% 正确,20 步全对只有 \(0.95^{20} \approx 36\%\)。
- tool output 里的 prompt injection:当数据不当指令,敏感操作要用户确认,见 RAG。
必答点
- 循环:schema → 模型输出
{name, arguments}→ runtime 执行 → 结果回 context → 再调模型 - 模型只"提议"调用;执行、校验、权限都在 runtime
- ReAct = Thought / Action / Observation 交替:比纯 CoT grounded,但会卡 loop
- MCP:JSON-RPC,host 里每个 client 对一个 server;server 暴露 tools / resources / prompts;tool 写一次,所有应用复用
- 动态调用的理由;失败模式
追问
MCP 和 function calling 是什么关系?
function calling 是模型这一层的能力,读 schema、输出调用。MCP 是应用和 tool 提供方之间的协议。host 把 MCP server 的 tools 转成模型的 tool 定义。
tool 调用失败怎么处理?
MCP 分两种。protocol error(未知 tool、请求格式错)走 JSON-RPC error。tool execution error 放在结果里标 isError: true,回给模型自己改。
MCP 有哪些安全问题?
tool 等于任意代码执行,host 调用前要用户同意。不可信 server 的 tool 描述也不可信。server 看不到整段对话和别的 server。权限分级见 agent 的权限。
ReAct 和 CoT 比怎样?
HotpotQA 上 CoT 的失败 56% 是 hallucination,ReAct 是 0%,因为事实来自检索。但 ReAct 的 reasoning error 更多(47% vs 16%)。论文里效果最好的是 ReAct 和 CoT-SC 互相 fallback。
来源Yao et al. 2023, ReAct §2(p.3)、§3.1–3.2(pp.4–5)、§3.3 Table 2(p.6)· Schick et al. 2023, Toolformer §2(pp.2–4)· MCP specification 2026-07-28:Overview、Architecture、Server Features(Tools、Resources、Prompts)、Transports