Notes
All notes

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:

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:

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:

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:

  1. 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.
  2. Keep tools narrow and least-privilege. search_docs is 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

Security properties to enforce (MCP is explicitly not a security boundary by itself):

\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:

The cost is a new supply chain and attack surface. Standardization concentrates risk as well as convenience; govern it accordingly.

Pitfalls

QQuestionSelf-check

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.