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

Model Context Protocol 實務

版次 2026.11.14 範例於 2026年11 月 19 通過測試 70 個範例中有 70 個通過 變更內容

由 AI 翻譯。經人工以英文驗證。 我們如何製作書籍

譯自英文書,版次 2026.11.14。 閱讀原文

為代理程式(agent)提供工具與資料,寫給熟悉 API 的工程師:說明 MCP 在 HTTP API 之上增加了什麼、何時不該選用 MCP,以及如何依據規格修訂版 2026-07-28,使用 Python SDK(mcp 2.x)設計、建置、保護、測試與營運 MCP 伺服器與用戶端,並提供從 2025 年各修訂版遷移的路徑。

適合對象 本書寫給建置或呼叫 HTTP API 的工程師,這些讀者現在需要為代理程式(agent)提供工具與資料,或是將代理程式連接到他人的系統。不需要具備機器學習的背景。

開始閱讀
版次歷史自 2026年十一月 以來的 14 個版次每個版次及其變動內容
2026.11.12026.11.14
2026.11.14 · 2026年11 月 21散文修訂、新內容30 節 已變更

目錄

12 章,閱讀時間約 18 小時 37 分鐘,附有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 增添了什麼

    讀完本章後,你能說明主機(host)、用戶端(client)與伺服器(server)各自負責什麼,能把商店的每項功能放進正確的 Model Context Protocol 原語(primitive),能讀懂每個請求所攜帶的版本與能力資訊,也能判斷商店何時應該保留單純的 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 伺服器、讀懂每個請求與回應所攜帶的內容、將長時間呼叫的進度以串流方式回傳並取消該呼叫、以多個副本在同一個位址後執行伺服器而不使用黏性工作階段(sticky session),並拒絕瀏覽器被誘騙送出的請求。

  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 伺服器及其主機劃定信任邊界、拒絕權杖直通(token passthrough)、避免伺服器淪為困惑的代理人(confused deputy)、固定工具定義以便發現被更動的定義、將工單文字視為資料,以及在任何具有副作用的工具呼叫之前詢問使用者。

  10. 10建構能處理工單的主機端

    讀完本章後,你可以將一個主機端連接到多個 Model Context Protocol 伺服器,為它們的工具取不會衝突的名稱,執行一段簡短的迴圈讓模型呼叫工具並讀取結果,代替使用者回應伺服器的輸入請求,將資源放入模型的上下文,並讓一整個工單佇列通過上述所有環節。

  11. 11測試伺服器並觀察其運作

    讀完本章後,你可以在記憶體內與透過 HTTP 測試 Model Context Protocol 伺服器的工具、在發布前找出會破壞用戶端的 schema 變更、將錄製的主機端執行重播為迴歸測試、以 OpenTelemetry 追蹤工具呼叫、衡量模型選對工具的頻率,以及在不破壞既有用戶端的情況下淘汰一項工具。

  12. 12遷移 2025 年代的伺服器

    讀完本章後,你可以將依 2025 年各版修訂與 Python SDK 1.x 系列撰寫的 Model Context Protocol 伺服器,遷移到修訂版本 2026-07-28 的規格與 mcp 2.3.0。

引用本節

格式

各種格式皆引用英文原書,本譯本即依循該原書。

每種格式都會標示本版。其連結會開啟目前版次,節標題會說明該節內文最後一次變動的時間。存取日期與擷取日期即為你開啟此頁的當日。