AI Engineering
The Model Context Protocol (MCP) in Practice
Building, Integrating, and Scaling Custom Tool Servers for AI Agents
A hands-on guide to designing, building, testing, and deploying secure stdio and SSE MCP servers in Python and TypeScript.
About this book
The Model Context Protocol (MCP) in Practice is a hands-on guide to building custom MCP servers: the programs that let a language model use real tools, read live data, and work with organizational systems through one open protocol instead of one bespoke integration per client. It has sixteen chapters in five parts, follows the real arc of building a server from protocol to production, and is written for engineers who build things. It covers the server side of MCP only, and it does not teach how to write a client or host.
The first part explains what crosses the wire: the JSON-RPC 2.0 message shapes, the stdio and SSE transports, the initialization handshake and capability negotiation, and the three primitives every server is built from, which are tools, resources, and prompt templates. The second part builds the same small server twice, once in Python and once in TypeScript, so readers can separate what belongs to the protocol from what belongs to one SDK. The code uses pinned versions of both SDKs, and fast-moving details are gathered into clearly marked version-sensitive callouts.
The third part is the craft of design and security. It shows how to name and describe tools, write schemas, and shape results so that a model uses them correctly, how to serve live data as bounded resources, and how to package workflows as prompts. It then treats security as defense in depth, with capability minimization, scoped credentials, path and subprocess safety, injection defenses, and dry-run gates, all built on the premise that a model can be steered by the content it reads. The fourth part connects servers to Claude Code, Claude Desktop, Cursor, and Zed, and builds four reference servers: a WordPress content server in Python that can draft but never publish, a GitLab server in TypeScript that previews before it acts, a DevOps server in Python that runs only allowlisted operations, and a project-scoped memory server in Python backed by SQLite.
The final part proves and ships the work. It covers layered testing with the MCP Inspector, pytest, and vitest, protocol probes and stdio integration tests, Docker packaging for both languages, deployment over SSE behind a reverse proxy with authentication and health checks, versioning, and a decision framework for when a server should stay local. The SSE transport it demonstrates is now a legacy option in the SDKs, and the book explains the Streamable HTTP direction. Each chapter includes a hands-on lab, and from the design chapters onward each provides a reusable checklist. The safety helpers and storage layers of the reference servers have tests that were executed, while running each full server against a live system is left to the reader in the labs and the release checklist.
Readers come away able to choose the right primitive for a capability, design tool surfaces a model can use, secure a server against manipulation, connect it to real clients, test it in layers, and decide whether and how to deploy it. It suits backend, platform, DevOps, and AI engineers who are comfortable with HTTP APIs, JSON, and the command line, and it requires no machine-learning background.
More books
-
AI Engineering
Building Autonomous Coding Agents
Design, Safety, and Local-First Operation of Unattended AI Development Systems
-
AI Engineering
Agentic Coding Harnesses, Compared & Explained
Master Claude Code, Aider, OpenCode, Goose, and Codex — Then Run Them Locally
-
AI Engineering
AI-Assisted Development
Working With Coding Agents