跳到主要内容

生产站(下)— Agent 与 MCP:让模型接管多步任务

这一章讲三件事: agent 到底比聊天机器人多了什么(三样:工具、循环、记忆); 原书四级课程阶梯每级加什么能力、验收是什么; MCP 为什么要存在——以及一条真实的消息流长什么样。 对应原书生产部第 9–10 章。读完你能判断:眼前这个任务该用一次 RAG 还是该上 agent。

1. 这一章讲什么

前两章的系统都有一个共同形状:用户发一次请求,系统走一条固定管线,答完即止。 管线是工程师写的,模型只管管线上那一步。 Agent 把这个形状翻过来:

之前的系统: 用户 → [工程师写的固定管线] → 回答
agent: 用户 → [模型自己决定下一步:检索?调工具?再想想?还是交卷?] → 回答
↑___________________________________|
(这个圈叫循环,可以转很多圈)

图说:决策权从代码移到模型手里。这是本章一切内容的总纲。

原书把这一段排成两章:agent一章(全书最长,1.6 万字符)+ MCP 一章。 一个讲「怎么造会自己干活的系统」,一个讲「它怎么接上外面的世界」—— 这就是本章的两半。

2. Agent 是什么:比聊天机器人多三样东西

原书资源里那门 UC Berkeley 课列的主题清单很有代表性:推理、规划、工具使用、 基础设施、检索增强、评测、安全……1。清单很长,拆开其实三样:

多出来的东西是什么解决什么
工具调用模型输出「我要调用某函数、参数是这些」,外部代码替它执行,结果塞回给它模型只会写字;查库、发邮件、算数都得靠外面的手
循环模型每步之后决定「继续调工具还是交卷」,程序按它的决定接着跑多步任务没法一次答完,得允许它边做边看
记忆跨步骤、跨对话保留状态上下文窗口装不下整个任务历史(第 5 节)

工具调用的一次往返(演示):用户问「查一下 PR #42 的状态」→ 模型不直接编答案,而是输出一段结构化请求:{"tool": "get_pull_request", "args": {"id": 42}} → 外部代码执行查询 → 把结果塞回模型 → 模型据此作答。 模型负责决定,代码负责执行——分工在这一点上定死。

原书那门 crewAI 课程把多智能体(multiple agents:几个 agent 分工协作)的设计要素 列成六条:角色扮演(各配专长人设)、记忆、工具、聚焦(拆任务)、护栏(处理错误、幻觉与死循环)、 协作(排着来、同时来、分层来)2六条里「护栏」最容易被漏做,也是唯一不做就一定出事的: 一个会自己决定下一步的系统,必须有人按得住它的死循环。

3. 四级课程阶梯:每级加一样能力

原书 agent 章本质是一份课程路线,四级每级加的能力不同3:

学什么能力增量原书代表资源
① 基础agent 的概念与构成看懂架构Nvidia 导读、UC Berkeley 课程、Arize 训练营4
② 多智能体多个 agent 编队会分工协作crewAI 两门课(创始人亲授)5
③ 专项挂到具体场景会接真数据数据库 agent(你用人话问,它转成 SQL——数据库的查询语言)6、Letta 记忆、LangGraph、agentic RAG7
④ 评测验证它真能用会量化TruLens 效果/幻觉/偏析检测、TaskWeaver、AgentBench8

Agentic RAG 值得单讲,因为它是第 05 章与本章的接缝:普通 RAG 管线 是固定形状(检索一次、作答一次);agentic RAG 把检索本身交给 agent—— 原书那门课的四级进阶正好是能力 ladder:先做路由器(来了问题,选问答引擎还是摘要引擎) → 加工具调用(模型不仅选函数,还自己推断该传什么参数) → 多步推理(先搜一篇,读完发现不够,再决定搜什么) → 多文档 agent(在几十篇论文间自主导航、汇总、对比)9。 每升一级,「检索」就从「管线的一环」变成「模型的决策」。

评测级有一句要紧的定调: AgentBench 是第一个专门评「模型当 agent 用」的基准, 在 8 类不同环境里测多步任务10含义:模型考试分数高,不等于它当 agent 能用—— 前者测单次问答,后者测「连续决策不翻车」。选 agent 底座要看的榜单和选聊天模型的不一样。

4. 记忆:MemGPT 把模型当操作系统用

原书对 agent 记忆的讲解,全部集中在 Letta(MemGPT)那一节,而且讲得比别处用心11。 问题从上下文窗口的性质来:窗口有限——「LLM 只能用输入上下文窗口里的信息, 而窗口空间有限;上下文越长,成本越高、处理越慢」12

MemGPT 论文(《把 LLM 当操作系统》)的思路13:既然内存不够,就像操作系统管内存那样—— 两级记忆:

主记忆(上下文内) 空间小、随时可用 —— 对话进行中的关键事实
↕ 模型自己决定换页:满了就把不常用的挪出去,要用了再调回来
归档记忆(外部存储) 空间大、要检索 —— 全部历史,持久可搜

原书给的三个用法:对话超限就搬到持久库、摘要后保关键事实;
把姓名、日期、偏好这类事实持久保存并随时调入;任务状态(如研究进度)持久跟踪[^14]

图说:「换页」由 agent 自己通过工具调用完成,不是工程师写死规则——
这是「把 LLM 当操作系统」的字面含义。

这一条在书架上有完整拆解可对照:核心记忆(上下文内)与归档记忆(上下文外)的分界、 以及「agent 自编辑记忆」的实现14

5. MCP:把 N×M 的对接死局压成 N+M

它解决什么问题

上一节的 agent 要干活,就得接工具:查 GitHub、读文件、发邮件、查数据库…… 在 MCP 出现之前,每个应用要接每个工具,都得单独写对接—— 应用与工具两两配对,对接工作量是「应用数 × 工具数」的乘积。 MCP(模型上下文协议,Anthropic 2024 年发布的开放标准)做的事: 定一份双方都遵守的对话规则(协议),应用实现一次「客户端」,工具实现一次「服务器」, 从此任意应用配任意工具——乘积变成加法。原书对它的定位:「正在成为 上下文感知 AI 生产里最重要的标准之一」15

架构与原书给的入门路径

原书列的资源从零到生产排得很整齐:Replit 的零基础导读16、带 demo 项目的实战文17、 Hugging Face 系统课18、Anthropic 官方短课19、官方文档20、服务器合集清单21。 其中 Anthropic 短课的提纲几乎就是协议大纲:客户端-服务器架构、 本地 MCP 服务器暴露工具/资源/提示模板(用 FastMCP 框架写,用 Inspector 工具调)、 连 Anthropic 官方的文件系统与网页抓取参考服务器、以及路线图上的 注册表、服务器发现、授权认证22

协议细节我们书架上有逐章拆解(消息格式、服务器对外提供的三类标准件——行话叫原语、传输方式),本节只用其结论23

主走查:一条 MCP 消息流的一生

用原书自己的例子:给 Claude Desktop 挂一个「PR 审查服务器」, 它从 GitHub 取变更、交给 Claude 分析、把审查意见存进 Notion24

用户在 Claude Desktop 里说:「帮我审查 PR #42」

├─ ① 发现:客户端(桌面应用)向 PR 审查服务器发「你有哪些工具?」
│ 服务器报上:fetch_pr_details、analyze_diff、save_review 三个工具及参数表

├─ ② 决定:模型读到工具清单,决定先调 fetch_pr_details,参数 {"pr": 42}

├─ ③ 执行:客户端把调用转成协议消息发给服务器;服务器去 GitHub 取回变更
│ (标题「Fix login timeout」,改动 3 个文件,+84 / -12 行 —— 演示数据)

├─ ④ 作答:结果塞回模型,模型产出审查意见:「超时逻辑没处理并发重连,建议…」

└─ ⑤ 收尾:模型决定调 save_review,把意见写进 Notion 对应页面

图说:①③⑤ 是协议规定的消息往返,②④ 是模型在决策。
消息的机械部分(JSON-RPC 格式、会话建立)为演示简化;协议原文见我们书架的拆解[^24]。

这条走查里值得停一下的是 ①: 「发现」是协议的关键动作——客户端不用预先知道 服务器有什么工具,问一句就行。所以新接一个工具,应用侧零改动; 这就是 N×M 变 N+M 的机制所在。原书那句「让服务器与桌面应用之间的通信标准化, 从而模块化、可扩展」25,说的就是这件事。

6. 作者的判断与证据

  1. 「agent 是下一个突破」(书内转述):UC Berkeley 课程的介绍原话—— agent 将以「智能任务自动化与个性化」改变日常生活26这是转述课程宣传, 不是作者论证,本书没有给任何支持证据;按三个声音的规矩,标记为课程方的乐观判断。
  2. 「MCP 值得单列一章」(书内结构证据):全书 27 章里,MCP 是唯一一个 「单个协议」拿到独立章节的主题——在 2025 年成书的材料里,这是相当超前的选材。 (对照:本库协议书架 2026 年的 MCP 规范拆解覆盖的内容,原书资源已指到入口。)
  3. 「评测收尾」(书内结构证据):agent 章以评测小节收束,列出效果、幻觉、偏析 三个检查面27——与全书「验收标准」的暗线一致。

判断(我们的,不是书里的): agent 章的四级阶梯里,最该补的一级是原书没有的 第 0 级:先判断要不要 agent。判断法:任务能否写成固定管线?能,写管线 (便宜、可控、可测);只有「下一步取决于上一步读到的内容」的任务,才值得上 agent。 大量 agent 化的项目,退回管线后成本降一个量级。 如果错,会错在: 如果模型能力涨到「自由决策的稳定性接近固定管线」, 先 agent 后管线的取舍会反转——届时判断标准要重写,但「先问能不能不 agent」 的提问方式仍然成立。

7. 边界与局限

  1. 原书没有一行机制。 工具调用的往返格式、循环怎么写、MCP 消息长什么样, 原书全部没讲(资源指路,机制为零);本章全部机制来自补充。
  2. 课程清单的时效极短。 所列课程以 DeepLearning.AI 短课为主,半年一换代; 四级阶梯的「形状」比清单本身长寿。
  3. 多智能体的必要性未被检验。 原书多智能体级占了最大篇幅(crewAI 两门课), 但没有讨论「单 agent 循环够用时为什么要编队」——这正是第 6 节判断块的问题。
  4. MCP 的安全面未展开。 原书 MCP 章不提注入、不提授权细节(路线图里有一句 授权认证22);协议书架的安全拆解是必需的补充阅读。
  5. 评测基准只有名字。 AgentBench 的 8 类环境是什么、怎么算分,原书零展开。

8. 可带走的

  1. agent = LLM + 工具调用 + 循环(+ 记忆);决策权移到模型手里是它与管线的全部区别;
  2. 工具调用往返:模型输出结构化请求 → 代码执行 → 结果回填 → 模型作答,分工定死;
  3. 多智能体六要素里,护栏(防死循环、防幻觉扩散)是唯一不做必出事的;
  4. agentic RAG 四级进阶:路由 → 工具调用 → 多步推理 → 多文档导航; 它把「检索」从管线一环变成模型决策;
  5. agent 底座要用 agent 基准(如 8 类环境的 AgentBench)选,聊天榜单不算数;
  6. 记忆 = 两级存储 + 模型自主换页(MemGPT 思路),窗口有限是问题根源;
  7. MCP 把「应用 × 工具」的乘积对接压成加法:客户端、服务器各实现一次; 「工具发现」是机制核心——新工具接入,应用零改动;
  8. 上 agent 前先问:这任务能不能写成固定管线?能,就别 agent。

9. 原文地图

主题原书章原文位置
agent 章的四段结构9. Top Free Learning Resources to Master LLM Agentstext/22-ch09-9-top-free-learning-resources-to-master-llm-agen.txt:14(搜「Foundational Courses」) · text/22-ch09-9-top-free-learning-resources-to-master-llm-agen.txt:81(搜「Practical Multi-Agent Systems」) · text/22-ch09-9-top-free-learning-resources-to-master-llm-agen.txt:178(搜「Specialized Agent Development」) · text/22-ch09-9-top-free-learning-resources-to-master-llm-agen.txt:335(搜「Evaluating Agents」)
Berkeley 课主题清单9. Top Free Learning Resources to Master LLM Agentstext/22-ch09-9-top-free-learning-resources-to-master-llm-agen.txt:26(搜「Large Language Model Agents — UC Berkeley」) · text/22-ch09-9-top-free-learning-resources-to-master-llm-agen.txt:46(搜「Foundation of LLMs」) · text/22-ch09-9-top-free-learning-resources-to-master-llm-agen.txt:50(搜「Planning, tool use」)
crewAI 六要素9. Top Free Learning Resources to Master LLM Agentstext/22-ch09-9-top-free-learning-resources-to-master-llm-agen.txt:131(搜「Role-playing」) · text/22-ch09-9-top-free-learning-resources-to-master-llm-agen.txt:133(搜「Memory: Provide agents」) · text/22-ch09-9-top-free-learning-resources-to-master-llm-agen.txt:140(搜「infinite loops」)
数据库 agent 与 SQL9. Top Free Learning Resources to Master LLM Agentstext/22-ch09-9-top-free-learning-resources-to-master-llm-agen.txt:184(搜「Database Agent」) · text/22-ch09-9-top-free-learning-resources-to-master-llm-agen.txt:208(搜「natural language to SQL」)
MemGPT 两级记忆9. Top Free Learning Resources to Master LLM Agentstext/22-ch09-9-top-free-learning-resources-to-master-llm-agen.txt:219(搜「LLMs as Operating Systems」) · text/22-ch09-9-top-free-learning-resources-to-master-llm-agen.txt:223(搜「limited space」) · text/22-ch09-9-top-free-learning-resources-to-master-llm-agen.txt:228(搜「MemGPT research paper」) · text/22-ch09-9-top-free-learning-resources-to-master-llm-agen.txt:235(搜「Control Conversation Memory」) · text/22-ch09-9-top-free-learning-resources-to-master-llm-agen.txt:254(搜「two tiers of memory」)
LangGraph 与 agentic RAG9. Top Free Learning Resources to Master LLM Agentstext/22-ch09-9-top-free-learning-resources-to-master-llm-agen.txt:267(搜「AI Agents in LangGraph」) · text/22-ch09-9-top-free-learning-resources-to-master-llm-agen.txt:293(搜「persistence in agents」) · text/22-ch09-9-top-free-learning-resources-to-master-llm-agen.txt:301(搜「Building Agentic RAG」) · text/22-ch09-9-top-free-learning-resources-to-master-llm-agen.txt:312(搜「a router」) · text/22-ch09-9-top-free-learning-resources-to-master-llm-agen.txt:320(搜「reason over tools in multiple steps」)
agent 评测9. Top Free Learning Resources to Master LLM Agentstext/22-ch09-9-top-free-learning-resources-to-master-llm-agen.txt:349(搜「TruLens」) · text/23-ch10-10-master-mcp-the-best-free-learning-resources.txt:2(搜「8 distinct environments」)
MCP 定位与资源序10. Master MCP: The Best Free Learning Resourcestext/23-ch10-10-master-mcp-the-best-free-learning-resources.txt:7(搜「one of the most important」) · text/23-ch10-10-master-mcp-the-best-free-learning-resources.txt:20(搜「Replit」) · text/23-ch10-10-master-mcp-the-best-free-learning-resources.txt:43(搜「Hugging Face Course」)
PR review server 实例10. Master MCP: The Best Free Learning Resourcestext/23-ch10-10-master-mcp-the-best-free-learning-resources.txt:28(搜「PR review server」) · text/23-ch10-10-master-mcp-the-best-free-learning-resources.txt:31(搜「Fetch PR details」) · text/23-ch10-10-master-mcp-the-best-free-learning-resources.txt:37(搜「Save reviews to Notion」) · text/23-ch10-10-master-mcp-the-best-free-learning-resources.txt:39(搜「standardize communication」)
Anthropic 短课提纲10. Master MCP: The Best Free Learning Resourcestext/23-ch10-10-master-mcp-the-best-free-learning-resources.txt:64(搜「Rich-Context AI Apps」) · text/23-ch10-10-master-mcp-the-best-free-learning-resources.txt:78(搜「client-server architecture」) · text/23-ch10-10-master-mcp-the-best-free-learning-resources.txt:85(搜「FastMCP」) · text/23-ch10-10-master-mcp-the-best-free-learning-resources.txt:90(搜「filesystem」) · text/23-ch10-10-master-mcp-the-best-free-learning-resources.txt:101(搜「MCP registry API」)
服务器合集10. Master MCP: The Best Free Learning Resourcestext/23-ch10-10-master-mcp-the-best-free-learning-resources.txt:113(搜「Awesome MCP Servers」)

Footnotes

  1. 出处:「9. Top Free Learning Resources to Master LLM Agents」第 44–66 段(text/22-ch09-9-top-free-learning-resources-to-master-llm-agen.txt:44,搜「Specifically, this course will include the following topics」)。主题清单含推理、规划与工具使用、agent 基础设施、检索增强生成、评测、隐私安全、多智能体协作等。

  2. 出处:「9. Top Free Learning Resources to Master LLM Agents」第 128–142 段(text/22-ch09-9-top-free-learning-resources-to-master-llm-agen.txt:128,搜「explore key components of」)。六要素:Role-playing、Memory、Tools、Focus、Guardrails、Cooperation。

  3. 出处:「9. Top Free Learning Resources to Master LLM Agents」第 14、81、178、335 段(text/22-ch09-9-top-free-learning-resources-to-master-llm-agen.txt:14,搜「Foundational Courses」;:81 搜「Practical Multi-Agent Systems」;:178 搜「Specialized Agent Development」;:335 搜「Evaluating Agents」)。四小节即四级。

  4. 出处:「9. Top Free Learning Resources to Master LLM Agents」第 18、26、69 段(text/22-ch09-9-top-free-learning-resources-to-master-llm-agen.txt:18,搜「Nvidia」;:26 搜「UC Berkeley」;:69 搜「Arize AI」)。

  5. 出处:「9. Top Free Learning Resources to Master LLM Agents」第 86–90 段(text/22-ch09-9-top-free-learning-resources-to-master-llm-agen.txt:86,搜「crewAI」)。两门课均由 crewAI 创始人 João Moura 主讲。

  6. 出处:「9. Top Free Learning Resources to Master LLM Agents」第 184、208 段(text/22-ch09-9-top-free-learning-resources-to-master-llm-agen.txt:184,搜「Database Agent」;:208 搜「natural language to SQL」)。

  7. 出处:「9. Top Free Learning Resources to Master LLM Agents」第 219、267、301 段(text/22-ch09-9-top-free-learning-resources-to-master-llm-agen.txt:219,搜「LLMs as Operating Systems」;:267 搜「AI Agents in LangGraph」;:301 搜「Building Agentic RAG」)。

  8. 出处:「9. Top Free Learning Resources to Master LLM Agents」第 340–361 段(text/22-ch09-9-top-free-learning-resources-to-master-llm-agen.txt:340,搜「Build, Evaluate, and Iterate」;:348 搜「TruLens」;:359 搜「AgentBench」)。

  9. 出处:「9. Top Free Learning Resources to Master LLM Agents」第 311–323 段(text/22-ch09-9-top-free-learning-resources-to-master-llm-agen.txt:312,搜「a router」;:319 搜「reason over tools in multiple steps」;:322 搜「multi-document agent」)。

  10. 出处:「10. Master MCP: The Best Free Learning Resources」第 1–3 段(text/23-ch10-10-master-mcp-the-best-free-learning-resources.txt:1,搜「first benchmark」;:2 搜「8 distinct environments」)。此段是上一章 AgentBench 条目的跨页续文。

  11. 出处:「9. Top Free Learning Resources to Master LLM Agents」第 219–265 段(text/22-ch09-9-top-free-learning-resources-to-master-llm-agen.txt:219,搜「LLMs as Operating Systems」)。该节是 agent 章里机制描述最密的一节。

  12. 出处:「9. Top Free Learning Resources to Master LLM Agents」第 223–225 段(text/22-ch09-9-top-free-learning-resources-to-master-llm-agen.txt:223,搜「limited space」;:225 搜「costs more and causes slower processing」)。

  13. 出处:「9. Top Free Learning Resources to Master LLM Agents」第 228–231 段(text/22-ch09-9-top-free-learning-resources-to-master-llm-agen.txt:228,搜「MemGPT research paper」)。MemGPT 论文名即《Towards LLMs as Operating Systems》,作者 Charles Packer 与 Sarah Wooders 是 Letta 创始人。

  14. 补充(不在书里,依据我们的 frontier 书架):两级记忆与 agent 自编辑记忆的实现。依据: shelf=ai-frontier-reference/letta#01-memory-tiers.md 事实=该拆解写明核心记忆(上下文内)与归档记忆(外部存储)两层、由 agent 通过工具自行换页。

  15. 出处:「10. Master MCP: The Best Free Learning Resources」第 7–8 段(text/23-ch10-10-master-mcp-the-best-free-learning-resources.txt:7,搜「one of the most important」)。N×M 收敛成 N+M 的对比为通用知识补充;同类表述见协议书架: 依据: shelf=ai-protocol-reference/mcp-spec#index.md 事实=MCP 是模型与外部工具/数据源之间的开放标准,客户端-服务器架构。

  16. 出处:「10. Master MCP: The Best Free Learning Resources」第 20 段(text/23-ch10-10-master-mcp-the-best-free-learning-resources.txt:20,搜「Replit」)。原文:零基础导读,「不需要深入的大模型技术背景」。

  17. 出处:「10. Master MCP: The Best Free Learning Resources」第 26–40 段(text/23-ch10-10-master-mcp-the-best-free-learning-resources.txt:28,搜「PR review server」)。

  18. 出处:「10. Master MCP: The Best Free Learning Resources」第 43–61 段(text/23-ch10-10-master-mcp-the-best-free-learning-resources.txt:43,搜「Hugging Face Course」)。

  19. 出处:「10. Master MCP: The Best Free Learning Resources」第 64–105 段(text/23-ch10-10-master-mcp-the-best-free-learning-resources.txt:64,搜「Rich-Context AI Apps」)。讲师 Elie Schoppik 见第 66 段。

  20. 出处:「10. Master MCP: The Best Free Learning Resources」第 107–110 段(text/23-ch10-10-master-mcp-the-best-free-learning-resources.txt:107,搜「Official MCP Documents」)。

  21. 出处:「10. Master MCP: The Best Free Learning Resources」第 113–133 段(text/23-ch10-10-master-mcp-the-best-free-learning-resources.txt:113,搜「Awesome MCP Servers」)。

  22. 出处:「10. Master MCP: The Best Free Learning Resources」第 84–101 段(text/23-ch10-10-master-mcp-the-best-free-learning-resources.txt:85,搜「FastMCP」;:89 搜「filesystem」;:100 搜「MCP registry API」)。 2

  23. 补充(不在书里,依据我们的 protocol 书架):MCP 的消息格式与服务器原语。依据: shelf=ai-protocol-reference/mcp-spec#01-jsonrpc-and-messages.md 事实=MCP 消息走 JSON-RPC 2.0,请求/响应/通知三类;另见 shelf=ai-protocol-reference/mcp-spec#03-server-primitives.md 事实=服务器向客户端暴露 tools、resources、prompts 三类原语。

  24. 出处:「10. Master MCP: The Best Free Learning Resources」第 28–40 段(text/23-ch10-10-master-mcp-the-best-free-learning-resources.txt:28,搜「PR review server」;:30 搜「Fetch PR details」;:36 搜「Save reviews to Notion」)。

  25. 出处:「10. Master MCP: The Best Free Learning Resources」第 39 段(text/23-ch10-10-master-mcp-the-best-free-learning-resources.txt:39,搜「standardize communication」)。

  26. 出处:「9. Top Free Learning Resources to Master LLM Agents」第 29–31 段(text/22-ch09-9-top-free-learning-resources-to-master-llm-agen.txt:30,搜「upcoming breakthrough」)。

  27. 出处:「9. Top Free Learning Resources to Master LLM Agents」第 347–351 段(text/22-ch09-9-top-free-learning-resources-to-master-llm-agen.txt:349,搜「effectiveness, hallucinations, and bias」)。