Cover of Model Context Protocol in Practice, edition 2026.11.14, code VS53-BS53 Model Context Protocol in Practice VS53-BS53 14 editions since Nov 2026 Rolling Edition 2026.11.14

模型上下文协议实战

版次 2026.11.14 示例于 2026年11月19日 通过 70个示例中有70个通过 变化内容

AI 翻译。由人工以英文核验。 我们如何制作图书

译自英文版图书,版次 2026.11.14。 阅读原文

面向智能体的工具与数据,写给熟悉 API 的工程师:MCP 在 HTTP API 之上增加了什么,何时它是错误的选择,以及如何基于规范修订版 2026-07-28,使用 Python SDK(mcp 2.x)设计、构建、保护、测试和运行 MCP 服务器与客户端,并提供从 2025 年各修订版迁移的路径。

适合谁 本书写给构建或调用 HTTP API 的工程师,他们现在需要为智能体提供工具和数据,或者将智能体接入他人的系统。读者无需具备机器学习背景。

开始阅读
版次历史自 2026年十一月 以来的 14个版次每个版次及其变更内容
2026.11.12026.11.14
2026.11.14 · 2026年11月21日文字修订,新内容30节已更改

目录

12章,阅读约需 18小时 19分钟,包含 70个示例。

  1. 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.

  2. 2MCP 为 API 增添了什么

    读完本章,你应当能够说明宿主、客户端和服务器各自承担什么职责,把商店的每项能力放进合适的 Model Context Protocol 原语,读懂每个请求都携带的版本和能力信息,并判断商店在什么情况下应当保留普通的 HTTP API。

  3. 3设计模型可以使用的工具

    读完本章,你将能够把商店的 API 转换为 Model Context Protocol 工具,使模型能够依据这些工具的名称、描述、输入模式、结果和错误采取行动,并且能够衡量改动某一条描述会怎样改变模型所选的工具。

  4. 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.

  5. 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. 6通过 Streamable HTTP 提供服务

    读完本章,你将能够通过 Streamable HTTP 提供商店的 Model Context Protocol 服务器,读懂每个请求和响应所携带的内容,从耗时较长的调用中以流的方式返回进度并取消该调用,将服务器作为多个副本运行在同一个地址之后而不使用粘性会话,并拒绝浏览器被诱骗发出的请求。

  7. 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. 8使用 OAuth 授权客户端

    读完本章,你将能够用 OAuth 保护商店的 Model Context Protocol 服务器。

  9. 9保护工具免遭滥用

    读完本章,你将能够划出 Model Context Protocol 服务器与其宿主之间的信任边界,拒绝令牌透传,防止服务器沦为混淆代理人,固定工具定义以便发现其被改动,把工单文本当作数据对待,并在任何带有副作用的工具调用之前询问用户。

  10. 10构建一个能处理工单的宿主

    读完本章,你将能够把一个宿主连接到多个 Model Context Protocol 服务器,为它们的工具起不会冲突的名称,运行一个简短的循环让模型调用工具并读取结果,代表用户回答服务器的输入请求,把资源放入模型的上下文,并让一个工单队列走完这全部流程。

  11. 11测试服务器并观察其运行

    读完本章,你将能够:在内存中和通过 HTTP 测试 Model Context Protocol 服务器的工具;在会破坏客户端的模式变更发布之前将其发现;把一次已记录的宿主运行重放为回归测试;用 OpenTelemetry 追踪工具调用;衡量模型选对工具的频率;以及在不破坏现有客户端的前提下停用一个工具。

  12. 12迁移 2025 年代的服务器

    读完本章,你将能够把一个按 2025 年各修订版和 Python SDK 1.x 系列编写的 Model Context Protocol 服务器,迁移到修订版 2026-07-28 规范和 mcp 2.3.0 上。

引用本节

格式

每种格式均引用英文原书,本译本以其为准。

每种格式都会注明该版次。其链接会打开当前版次,节标题处会说明该节文本最后一次修改的时间。访问日期和检索日期即为您打开此页的当天。