MCP (AI Engg #12)
Every agent framework used to invent its own way to connect tools: LangChain had one interface, LlamaIndex another, each IDE another. If you wrote a “read my files” tool, you rewrote it for each. In late 2024 Anthropic proposed the Model Context Protocol to end that, and by 2026 it had become the default plug for tools, data, and prompts — including a major 2026-07-28 revision that made the core stateless so servers scale like ordinary web services.
This chapter explains what MCP is, the host/client/server architecture, the 2026 protocol changes you must know, how to expose a tool as an MCP server, and how to consume one safely.
The problem MCP solves
Think of USB-C. Before it, every device had a different port; after it, one cable. MCP is that cable between AI applications and capabilities (tools, files, databases, prompts). Write one MCP server for your internal wiki and every MCP-capable host — Claude Desktop, an IDE, your own agent — can use it.
The protocol defines a small set of primitives:
- Tools — callable functions the model can invoke (
search_docs(query)). - Resources — addressable data the model can read (a file, a row, a URL).
- Prompts — reusable templates the host can offer.
Everything is exchanged as typed JSON-RPC-style messages over a transport: stdio for local processes, HTTP for remote servers.
\begin{tikzpicture}[font=\rmfamily, >=stealth,
b/.style={draw, thick, rounded corners=4pt, minimum width=2.4cm, minimum height=0.75cm, font=\rmfamily\small, align=center},
a/.style={->, thick}]
\node[b, fill=blue!10] (h) at (0,1.5) {HOST\\Claude / IDE / your app};
\node[b, fill=orange!16] (c) at (0,0) {CLIENT $\times N$\\one per server};
\node[b, fill=green!12] (s1) at (-2.8,-1.6) {SERVER\\files};
\node[b, fill=green!12] (s2) at (0,-1.6) {SERVER\\database};
\node[b, fill=green!12] (s3) at (2.8,-1.6) {SERVER\\SaaS API};
\draw[a] (h) -- (c);
\draw[a] (c) -- (s1); \draw[a] (c) -- (s2); \draw[a] (c) -- (s3);
\node[font=\rmfamily\small, text=black!60, anchor=north] at (0,-2.25) {one client $\leftrightarrow$ one server; host fans out};
\end{tikzpicture}
Host, client, server
The three roles are precise and worth memorizing:
- Host. The AI application the user interacts with — Claude Desktop, an IDE, your product. It owns the model, the conversation, and the security policy. The host may run many clients.
- Client. A connector inside the host, maintaining a one-to-one connection to exactly one server. It translates the host’s needs into protocol messages.
- Server. A program exposing capabilities. A filesystem server exposes file tools; a Postgres server exposes query tools; your company could expose a “CRM” server.
The host decides what a server is allowed to do (which tools, which scopes). The server does not decide; it declares. That separation is the security model: policy lives at the host, capability lives at the server.
The 2026-07-28 specification: what changed
The 2026-07-28 revision is the biggest change since launch. Its headline is a stateless protocol core: MCP moved from a bidirectional, session-based protocol to request/response, so any server instance can handle any request behind a normal load balancer. Consequences:
- Horizontal scaling is trivial. No sticky sessions, no in-memory session store to share; deploy many identical servers.
- Auth and task identity travel per request, not via a long-lived session. You send credentials/context with each call.
- Better fit for serverless and multi-region deployments (Cloudflare, Google, Microsoft all shipped guidance).
- Migration cost: servers and clients that assumed sessions must be updated. If you maintain an MCP server, read the migration notes and move early.
The primitives (tools, resources, prompts) remain; the state model changed. For the working engineer, the practical rule is: do not stash session state in the server process — return cursors/tokens and accept identity on each request.
\begin{tikzpicture}[font=\rmfamily, >=stealth,
b/.style={draw, thick, rounded corners=4pt, minimum width=1.9cm, minimum height=0.66cm, font=\rmfamily\small},
a/.style={->, thick}]
\node[b, fill=red!8] (old) at (-2.6,0) {pre-2026\\stateful session};
\node[b, fill=green!12] (new) at (1.4,0) {2026-07-28\\stateless request};
\draw[a, red!70!black] (old) -- (new);
\node[font=\rmfamily\small, text=black!60, anchor=north, align=center] at (-2.6,-0.6) {sticky, one box};
\node[font=\rmfamily\small, text=black!60, anchor=north, align=center] at (1.4,-0.6) {any box, load-balanced};
\end{tikzpicture}
Building an MCP server
A tool server, in spirit (the Python mcp SDK handles the transport):
# server.py — expose one tool. Conceptual; SDK wiring elided.
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("docs")
@mcp.tool()
def search_docs(query: str, limit: int = 5) -> list[dict]:
"""Search internal documentation. Use for product/policy questions."""
return kb.search(query, limit=limit)
@mcp.resource("doc://handbook")
def handbook() -> str:
"""The employee handbook, as a readable resource."""
return open("handbook.md").read()
if __name__ == "__main__":
mcp.run() # stdio transport by default; HTTP for remote
Two design notes that determine whether the model uses your server well:
- Write tool descriptions for the model.
search_docs(query, limit)needs a docstring like “Search internal documentation. Use for product/policy questions.” The model routes by description. - Keep tools narrow and least-privilege.
search_docsis safe;execute_sql(anything)is a liability. Expose scoped operations.
For local development, run the server over stdio and point a host at the command. For production, run it over HTTP with authentication (OAuth/token), an allow-list of tools, and per-tenant scopes.
Consuming a server
On the host side, the flow is: connect → list tools/resources → advertise them to the model → execute requested calls → return results.
# host-side sketch
async with mcp_client("http://localhost:8000/mcp") as session:
tools = await session.list_tools() # discover
# hand tools to the LLM as function-calling schemas ...
result = await session.call_tool("search_docs", {"query": "SSO", "limit": 3})
The beauty is uniformity: the host code does not care whether search_docs is a local Python function, a database server, or a SaaS connector. One integration, many capabilities.
Local vs remote, and the security model
- Local (stdio). The server runs as a child process on the user’s machine. Great for files, git, local databases. Trust boundary: the user’s own machine.
- Remote (HTTP). A service many users connect to. Needs authentication, authorization, rate limits, and input validation. Treat every remote server as untrusted until proven otherwise.
Security properties to enforce (MCP is explicitly not a security boundary by itself):
- Least privilege per server. Grant the smallest scope that works.
- Prompt-injection defense. Content a server returns is data, never instructions. A malicious resource can try to hijack the model; isolate and moderate it (Chapter 13).
- Tool allow-lists per host/user. Do not auto-expose every tool a server declares.
- Audit every call. Log who called what, with what args and result.
\begin{tikzpicture}[font=\rmfamily, >=stealth,
b/.style={draw, thick, rounded corners=4pt, minimum width=2.0cm, minimum height=0.68cm, font=\rmfamily\small, align=center},
a/.style={->, thick}]
\node[b, fill=blue!10] (h) at (0,0) {host policy\\allow-list};
\node[b, fill=orange!16] (s) at (3.0,0) {server\\declares tools};
\node[b, fill=green!12] (r) at (6.0,0) {resource/tool result\\= untrusted data};
\draw[a] (h) -- (s); \draw[a] (s) -- (r);
\node[font=\rmfamily\small, text=black!60, anchor=north] at (3.0,-0.6) {capability, not permission};
\end{tikzpicture}
Why this matters for your stack
MCP turns “integrate N tools with M frameworks” into “write a server once.” For an AI engineer in 2026 it means:
- Your internal capabilities (docs, tickets, DBs) get a single, reusable integration.
- Coding agents (Chapter 15) gain tools without custom code per tool.
- The tool ecosystem becomes portable — swap the host, keep the servers.
The cost is a new supply chain and attack surface. Standardization concentrates risk as well as convenience; govern it accordingly.
Pitfalls
- Exposing everything. Every toast tool is an attack surface. Least privilege, always.
- Session assumptions post-2026. Do not store per-user state in the server process; the spec is stateless now. Return cursors, accept identity per request.
- Trusting server output. A remote server’s content can carry injected instructions. Treat it as data; moderate and isolate.
- Vague tool descriptions. The model routes by description; a vague one never gets picked. Write them like API docs.
- No auth on remote servers. An open HTTP MCP server in production is an open door.
What is the host/client/server split, and why does it matter for security? What is the headline change in the 2026-07-28 spec?
ASolution
The host is the user-facing app and owns policy; each client is a one-to-one connector; servers declare capabilities. Policy living in the host (not the server) is what makes least-privilege and allow-listing possible. The headline change is a stateless core: MCP moved to request/response so servers scale horizontally behind a load balancer, with auth/identity per request instead of a session.
Next: Safety, Observability and Evals (AI Engg #13) — the seatbelts that let you ship any of this.