模型上下文协议实战
面向智能体的工具与数据,写给熟悉 API 的工程师:MCP 在 HTTP API 之上增加了什么,何时它是错误的选择,以及如何基于规范修订版 2026-07-28,使用 Python SDK(mcp 2.x)设计、构建、保护、测试和运行 MCP 服务器与客户端,并提供从 2025 年各修订版迁移的路径。
适合谁 本书写给构建或调用 HTTP API 的工程师,他们现在需要为智能体提供工具和数据,或者将智能体接入他人的系统。读者无需具备机器学习背景。
在 Kindle 或任意电子阅读器上阅读
EPUB 格式也适用于 Apple Books、Kobo 及大多数电子阅读器。
发送到 Kindle
将本版次的 EPUB 发送至 简体中文.
-
批准我们的地址
亚马逊仅向您已批准的地址所发送的内容投递至 Kindle,且每项邮件服务须单独批准。请将我们的地址添加到您的已批准个人文档电子邮件列表中。
books@rollingedition.com打开亚马逊的个人文档设置. 在亚马逊上,该选项位于“管理您的内容和设备”、“偏好设置”、“个人文档设置”下。
已发送。几分钟后将出现在您的 Kindle 书库中。
如果未能送达,几乎总是第 1 步的批准设置有问题:请检查 books@rollingedition.com 是否已列入您的已批准个人文档电子邮件列表。
您已保存图书的每个新版次也将发送到您的 Kindle。
这不是 Kindle 地址。地址应以 @kindle.com 结尾,可在亚马逊的“个人文档设置”中找到。
你今天已发送{count}本图书,这是单个账户一天可发送的最大数量。请明天再试。
今日可发送的书籍已达上限。请明天再试。
图书未能发送,请几分钟后重试。
本版次体积过大,无法通过电子邮件发送。
发送到 Kindle 功能随 Labs 提供。
这不是 Kindle 地址。地址应以 @kindle.com 结尾,可在亚马逊的“个人文档设置”中找到。
图书未能发送,请几分钟后重试。
或者下载 EPUB 文件,并将其拖放到亚马逊的“发送至 Kindle”页面。
Kindle is a trademark of Amazon.com, Inc.
目录
12章,阅读约需 18小时 19分钟,包含 70个示例。
-
1MCP end to end
By the end of this chapter you can put an existing HTTP API behind a Model Context Protocol server over stdio, call its tool from a client, read every message the two exchange, and let a model choose the tool through a small host.
-
2MCP 为 API 增添了什么
读完本章,你应当能够说明宿主、客户端和服务器各自承担什么职责,把商店的每项能力放进合适的 Model Context Protocol 原语,读懂每个请求都携带的版本和能力信息,并判断商店在什么情况下应当保留普通的 HTTP API。
-
3设计模型可以使用的工具
读完本章,你将能够把商店的 API 转换为 Model Context Protocol 工具,使模型能够依据这些工具的名称、描述、输入模式、结果和错误采取行动,并且能够衡量改动某一条描述会怎样改变模型所选的工具。
-
4Serve data as resources and prompts
By the end of this chapter you can publish the shop's policies and records as Model Context Protocol resources, address many records with one resource template, complete an argument while a user types it, tell a client when a resource changes, and package a refund review as a prompt the user can pick.
-
5Ask the user in the middle of a call
By the end of this chapter you can make the shop's refund tool stop in the middle of a call, ask the person at the host to confirm a refund, and carry on with the answer, using Model Context Protocol elicitation inside a Multi Round-Trip Request on a server that keeps no call open while it waits.
-
6通过 Streamable HTTP 提供服务
读完本章,你将能够通过 Streamable HTTP 提供商店的 Model Context Protocol 服务器,读懂每个请求和响应所携带的内容,从耗时较长的调用中以流的方式返回进度并取消该调用,将服务器作为多个副本运行在同一个地址之后而不使用粘性会话,并拒绝浏览器被诱骗发出的请求。
-
7Run long work as tasks
By the end of this chapter you can negotiate a Model Context Protocol extension, run a slow restock report as a task that the client polls, let tasks expire and cancel them, and say what MCP Apps add to a tool.
-
8使用 OAuth 授权客户端
读完本章,你将能够用 OAuth 保护商店的 Model Context Protocol 服务器。
-
9保护工具免遭滥用
读完本章,你将能够划出 Model Context Protocol 服务器与其宿主之间的信任边界,拒绝令牌透传,防止服务器沦为混淆代理人,固定工具定义以便发现其被改动,把工单文本当作数据对待,并在任何带有副作用的工具调用之前询问用户。
-
10构建一个能处理工单的宿主
读完本章,你将能够把一个宿主连接到多个 Model Context Protocol 服务器,为它们的工具起不会冲突的名称,运行一个简短的循环让模型调用工具并读取结果,代表用户回答服务器的输入请求,把资源放入模型的上下文,并让一个工单队列走完这全部流程。
-
11测试服务器并观察其运行
读完本章,你将能够:在内存中和通过 HTTP 测试 Model Context Protocol 服务器的工具;在会破坏客户端的模式变更发布之前将其发现;把一次已记录的宿主运行重放为回归测试;用 OpenTelemetry 追踪工具调用;衡量模型选对工具的频率;以及在不破坏现有客户端的前提下停用一个工具。
-
12迁移 2025 年代的服务器
读完本章,你将能够把一个按 2025 年各修订版和 Python SDK 1.x 系列编写的 Model Context Protocol 服务器,迁移到修订版 2026-07-28 规范和
mcp2.3.0 上。