- Preface
- Part 1: The Basics
- 1. What MCP Is
- The problem MCP solves
- Hosts, clients and servers
- What a server can offer
- How messages travel
- Who maintains MCP
- The ecosystem
- The current revision
- The project you will build
- 2. How the Protocol Works
- JSON-RPC in one page
- The request envelope
- Discovering a server
- Results and their types
- Errors
- HTTP headers
- Which protocol is in use?
- 3. Your First Server
- Setting up the project
- The data
- Registering a tool
- Registering a resource and a prompt
- Starting the server
- Trying it with the Inspector
- How the code works
- Exercise
- 4. Your First Client
- Connecting to a stdio server
- Choosing a protocol version
- Listing and calling tools
- Reading resources and prompts
- Failure: three kinds
- Connecting over HTTP
- Exercise
- Part 2: Building for Production
- 5. Asking the User for Input
- Why a server needs to ask
- Multi-round-trip requests
- The two rounds on the wire
- Writing the server side
- The decline trap
- The client side
- What you can ask
- Remembering between rounds
- Rules for handlers that run more than once
- Exercise
- 6. Long-Running Tasks
- Why blocking is not enough
- The Tasks extension
- Declaring support
- A limit in the SDK
- Adding the extension in front of the handler
- The three calls on the wire
- Polling from a client
- Cancelling, ownership and fallbacks
- What this project leaves out
- 7. Caching
- What can be cached
- Telling the client how long
- Public and private
- What the client does with a hint
- Measuring the saving
- Keeping answers fresh
- When not to cache
- Exercise
- 8. Deployment
- Serving over Streamable HTTP
- One server per request
- Where the handler can run
- Host and Origin checks
- A gateway that reads only headers
- Two instances, and what breaks
- Rolling out a change
- Edge and serverless platforms
- Exercise
- 9. Authorization
- The three roles in OAuth
- How a client finds out where to sign in
- What the current revision changed
- Verifying tokens in the server
- Publishing metadata
- Scopes on tools and resources
- A user's own data
- Trying it
- What this project skips
- Exercise
- 10. Testing and Debugging
- Three levels of testing
- Testing in-process
- Testing a real port
- The Inspector
- Trying it in Claude
- An index of errors
- Logging and tracing
- A short debugging routine
- Part 3: The Wider Ecosystem
- 11. Extensions
- What an extension is
- The three stages
- How both sides agree to use one
- The official extensions
- MCP Apps
- Skills over MCP
- Authorization extensions
- Writing your own
- 12. Registry and Server Cards
- Four ways to discover a server
- The MCP Registry
- Describing the project in server.json
- Server Cards
- Serving the card
- Domain-level discovery
- Trust and what discovery cannot tell you
- 13. Security
- Who and what to trust
- The defenses already in the project
- Confused deputy
- Token passthrough
- Server-side request forgery
- State handle hijacking
- Local servers
- Attacks on sign-in
- Scope minimization
- Content the model reads
- Servers you did not write
- A checklist
- 14. Agents and Many Servers
- What an agent does with servers
- A hub that connects to two servers
- Names that collide
- When one server is down
- The cost of many tools
- Staying up to date
- Replacing Roots, Sampling and Logging
- Running a fleet
- Migrating from the Previous Protocol
- Quick Reference
- Exercise Solutions
- Complete Listings
- Glossary
- Index
The MCP Handbook
A practical guide to building servers and clients
Build Model Context Protocol servers and clients in TypeScript, one working to-do server at a time.
Minimum price
$7.00
$29.00
You pay
Author earns
Buying multiple copies for your team? See below for a discount!
About
About the Book
The Model Context Protocol (MCP) is a standard way for an AI application to talk to the tools and data around it. Before MCP, every application wrote its own glue code for every service. With MCP, a service describes itself once, and any application that speaks the protocol can use it.
The protocol has changed a lot. Its latest revision removed sessions, replaced the opening handshake with metadata on every request, added a way for a server to ask the user a question in the middle of a call, and moved long-running work into an extension. Most tutorials were written for the older design. This book was written for the new one.
The MCP Handbook explains one part of MCP per chapter and shows it working in a single small project: a to-do list server built with the official TypeScript SDK v2. Every listing is cut from that project, and its 35 tests include one for every exercise solution.
What you will learn
- How MCP messages look on the wire: the request envelope, discovery, results, errors and HTTP headers
- How to build a server with tools, a resource and a prompt, and a client that chooses a protocol version
- How a tool asks the user for confirmation with multi-round-trip requests
- How to return a task handle for slow work, and let clients cache list results
- How to serve MCP over HTTP, behind a gateway and on more than one instance
- How to protect a server with OAuth tokens and scopes, and defend it against known attacks
- How to test a server in-process, debug it with the MCP Inspector, and try it in a real AI client
- How people and clients find your server through the MCP Registry and Server Cards, and how one client works with many servers
Who this book is for
Developers who want to build MCP servers and clients, or who need to understand how they behave in production. You should be comfortable reading TypeScript and running commands in a terminal. You do not need to know MCP, and you do not need to have built an AI application. If you already run servers written for the older protocol, an appendix lists what changes.
Team Discounts
Team Discounts
Get a team discount on this book!
Up to 3 members
- Minimum price
- $17.00
- Suggested price
- $72.00
Up to 5 members
- Minimum price
- $28.00
- Suggested price
- $116
Up to 10 members
- Minimum price
- $49.00
- Suggested price
- $203
Up to 15 members
- Minimum price
- $70.00
- Suggested price
- $290
Up to 25 members
- Minimum price
- $105
- Suggested price
- $435
Author
About the Author
Sandeep Kumar Patel is a senior web developer with more than 17 years of experience building web applications in object-oriented JavaScript and JSON. In 2012 he founded tutorialsavvy, a programming blog that developers have been reading ever since.
He is GATE-2005 Information Technology (IT) qualified and holds a Master's degree from VIT University, Vellore.
Contents
Table of Contents
Get the free sample chapters
Click the buttons to get the free sample in PDF or EPUB, or read the sample online here
The Leanpub 60 Day 100% Happiness Guarantee
Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.
See full terms...
Earn $8 on a $10 Purchase, and $16 on a $20 Purchase
We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.
(Yes, some authors have already earned much more than that on Leanpub.)
Learn more about writing on Leanpub
Free Updates. DRM Free.
If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).
Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.
Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.
Learn more about Leanpub's ebook formats and where to read them
Write and Publish on Leanpub
You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!
Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.
Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.