Model Context Protocol in Practice
Written with AI. Verified by people. How we make books
Anyone can check this book or correct it. Report an error · How to fix one
Tools and data for agents, for engineers who know APIs: what MCP adds to an HTTP API and when it's the wrong choice, and how to design, build, secure, test and run MCP servers and clients on specification revision 2026-07-28 with the Python SDK, mcp 2.x, with a migration path from the 2025 revisions.
Who it's for For engineers who build or call HTTP APIs and now need to give agents tools and data, or connect an agent to someone else's. No machine learning background needed.
Read on Kindle or any e-reader
The EPUB also works in Apple Books, Kobo and most e-readers.
Send to Kindle
-
Approve our address
Amazon delivers to a Kindle only from addresses you've approved, once for each service that emails it. Add ours to your Approved Personal Document E-mail List.
books@rollingedition.comOpen Amazon's Personal Document Settings. On Amazon, it's under Manage Your Content and Devices, Preferences, Personal Document Settings.
Sent. It'll appear in your Kindle library in a few minutes.
If it doesn't arrive, it's almost always the approval in step 1: check that books@rollingedition.com is on your Approved Personal Document E-mail List.
Each new edition of your saved books will go to your Kindle too.
That isn't a Kindle address. It ends in @kindle.com: find it in Amazon's Personal Document Settings.
You've sent {count} books today, the most we send for one account in a day. Try again tomorrow.
We've sent as many books as we can today. Try again tomorrow.
The book didn't send. Try again in a few minutes.
This edition is too large to email.
Send to Kindle is for members.
That isn't a Kindle address. It ends in @kindle.com: find it in Amazon's Personal Document Settings.
The book didn't send. Try again in a few minutes.
Or download the EPUB and drop it on Amazon's Send to Kindle page.
Kindle is a trademark of Amazon.com, Inc.
Labs
Work through this book in the Lab
Run every example exactly as the book did, change the code to see what happens, and try your own ideas on the book's code and data. Nothing to install, and your work is saved. Opens in about 30 seconds.
Run every example exactly as the book did, change the code to see what happens, and try your own ideas on the book's code and data. Nothing to install, and your work is saved. Included with membership: 20 hours a month with Member, or 60 with Member Plus.
Checking your hours…
Checking for your Lab…
Contents
12 chapters, about 5 hours 25 minutes to read or 7 hours 32 minutes to listen to, with 70 examples.
-
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.
-
2What MCP adds to an API
By the end of this chapter you can say what the host, the client and the server each do, put each of the shop's capabilities in the right Model Context Protocol primitive, read the version and capabilities that every request carries, and decide when the shop should keep a plain HTTP API instead.
-
3Design tools a model can use
By the end of this chapter you can turn the shop's API into a small set of Model Context Protocol tools whose names, descriptions, input schemas, results and errors a model can act on, and measure how a change to a description changes which tool a model picks.
-
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 and ask the person at the host to confirm a refund, with Model Context Protocol elicitation inside a Multi Round-Trip Request, on a server that keeps no call open while it waits for the answer.
-
6Serve over Streamable HTTP
By the end of this chapter you can serve the shop's Model Context Protocol server over Streamable HTTP, read what each request and response carries, stream progress from a long call and cancel it, run the server as replicas behind one address with no sticky sessions, and refuse requests that a browser was tricked into sending.
-
7Run long work as tasks
By the end of this chapter you can negotiate a Model Context Protocol extension, run the shop's 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.
-
8Authorize clients with OAuth
By the end of this chapter you can protect the shop's Model Context Protocol server with OAuth.
-
9Secure tools against misuse
By the end of this chapter you can draw the trust boundaries around a Model Context Protocol server and its host, refuse token passthrough, keep a server from acting as a confused deputy, pin tool definitions so a changed one is caught, treat ticket text as data, and ask the user before any tool call with side effects.
-
10Build a host that works through tickets
By the end of this chapter you can connect a host to several Model Context Protocol servers, give their tools names that can't collide, run a short loop in which a model calls tools and reads their results, answer a server's input requests on the user's behalf, put resources into the model's context, and work a queue of tickets through all of it.
-
11Test a server and watch it run
By the end of this chapter you can test a Model Context Protocol server's tools in memory and over HTTP, catch a schema change that would break clients before it ships, replay a recorded host run as a regression test, trace tool calls with OpenTelemetry, measure how often a model chooses the right tool, and retire a tool without breaking the clients that use it.
-
12Migrate a 2025-era server
By the end of this chapter you can move a Model Context Protocol server written for the 2025 revisions and the Python SDK's 1.x line to the specification at revision 2026-07-28 and
mcp2.3.0: find what it depends on, remove its sessions, move it off the HTTP+SSE transport, replace sampling, roots, logging and Dynamic Client Registration, port the code, and serve clients of both eras while the old ones are still around.