Build and Deploy MCP Integrations
Build a Model Context Protocol client and server, then verify the integration with a compatible host.
Revisions by date, the legacy handshake, per-request _meta and version negotiation.
How can different server processes handle later calls in the same workflow?
A self-describing request carries enough protocol context to be interpreted independently. Durable workflow state can travel through an explicit handle, so processing does not depend on returning to the same worker.
First request declares its protocol version
Server returns a handle for continuing the work
Later request declares its version and carries that handle
First request declares its protocol version
Server returns a handle for continuing the work
Later request declares its version and carries that handle
Given First request declares its protocol version and Later request declares its version and carries that handle, write down the missing step that connects them.
Server returns a handle for continuing the work
Now take Server returns a handle for continuing the work and the result above. Write an input and an implementation that could produce this trace.
First request declares its protocol version. Check that your implementation produces the result shown above.
The load balancer sends a follow-up to a worker that never saw the first request. What information lets it continue?
It needs the request's protocol context and the work handle. The worker can use the handle to recover the shared durable state.
Check this when debugging. Thinking every MCP server still needs initialize first (modern ones don't, legacy ones do).
A protocol used by thousands of apps can't change all at once. MCP names each revision by date and lets clients and servers agree on one. In 2026 it also made a big design change: it dropped the opening handshake, so every request now stands on its own. That makes servers far easier to scale, but much existing software still speaks the older style. This lesson covers the versions, the old handshake, the new per-request metadata, and how a client settles on a version. Discovery and the two eras covers how a server describes itself, and how the two eras live side by side.
Each revision can change messages, so both must speak the same one
A protocol used by thousands of apps can't change everywhere at once. Each side says which revisions it speaks, and they use one both know, so old and new apps can still talk.
This rests on JSON-RPC 2.0 in depth: every message is a request, a reply or a notification, and a reply carries its request's id. The question here is what else a message must say about itself.
MCP versions are dates, like the editions of an atlas: 2024-11-05 (the first public release), 2025-03-26, 2025-06-18, 2025-11-25 and 2026-07-28. A date sorts naturally and says when it was published. There's no guessing whether version 3 came before version 2.1. The spec calls everything up to 2025-11-25 legacy, and 2026-07-28 onward modern.
Think of a phone call. You say hello, agree on a language, and from then on you both remember what was said.
In legacy MCP, a client opens a connection and first sends initialize with its preferred version, its capabilities and its name. The server answers with the version it chose and its own capabilities. The client then sends a notifications/initialized notification, and only then do normal requests begin.
Everything agreed in the handshake is state, which means information remembered between messages. It lasts for the life of the connection, or of a session identified by an Mcp-Session-Id header over HTTP. That's the flow you used in Phase 1, and the academy's practice server still answers it.
def kind(msg): if "method" in msg: return "request" if "id" in msg else "notification" return "error" if "error" in msg else "result" messages = [{"id": 1, "method": "tools/list"}, {"method": "notifications/progress"}, {"id": 1, "result": {}}, {"id": None, "error": {"code": -32700}}] print([kind(m) for m in messages])
['request', 'notification', 'result', 'error']
The four shapes are told apart by which keys are present. The last one is a parse error: the server couldn't read the message, so it can't know the id and sends null. In the modern revisions of MCP, each request also carries its own protocol version, which is this lesson's subject.
Three messages before any real work, and every later message relies on them. That memory is the state the next section is about.
State has costs. Picture a bank where only one teller holds your file. Every time you come in, you have to wait for that one teller, even when nine others are free.
A busy server runs on several machines behind a load balancer, a machine that shares requests out. If sessions live in memory, every request in a session must reach the machine that holds that session's state. Load balancers can do that, with sticky sessions, but it's extra work and it spreads the load unevenly. If that machine restarts, the session is lost and the client has to start over. Change notifications needed a long-lived stream tied to the session, too.
In plain words: a handshake makes the server remember you, and remembering is expensive once there's more than one server.
Send them all to machine 3
The handshake's results live on machine 3, so the others wouldn't know the session. That's what sticky sessions means, and if machine 3 restarts, the session is gone.
A letter is different from a phone call. It has to say everything the reader needs, because the reader remembers nothing. The 2026-07-28 revision made MCP stateless, the word for keeping nothing between messages. There's no handshake. Instead, every request carries what the server needs in params._meta:
{
"jsonrpc": "2.0",
"id": 1,
"method": "tools/list",
"params": {
"_meta": {
"io.modelcontextprotocol/protocolVersion": "2026-07-28",
"io.modelcontextprotocol/clientCapabilities": {},
"io.modelcontextprotocol/clientInfo": {"name": "shop-assistant",
"version": "0.3.0"}
}
}
}
protocolVersion (required): the revision this request uses.clientCapabilities (required): the optional features this client supports, such as elicitation. An empty object means "only the basics".clientInfo (recommended): the client's name and version, for logs and display only.The long names start with io.modelcontextprotocol/. That prefix marks them as the protocol's own, so other tools can add their own keys to _meta without clashing. Two of those fields are required in every modern request: io.modelcontextprotocol/protocolVersion and io.modelcontextprotocol/clientCapabilities. A request missing either is malformed. The server answers with error -32602 (Invalid params), and HTTP 400 over HTTP.
Results answer in kind. Each has resultType ("complete" for ordinary results), and a _meta holding io.modelcontextprotocol/serverInfo, the server's own name and version.
In plain words: the server no longer has to remember you, because each request introduces itself.
_meta. What does this print for a request that has it, and one that doesn't?KEY = "io.modelcontextprotocol/protocolVersion" def version_of(request): return request.get("params", {}).get("_meta", {}).get(KEY) with_meta = {"id": 1, "method": "tools/list", "params": {"_meta": {KEY: "2026-07-28"}}} print(version_of(with_meta), version_of({"id": 2, "method": "tools/list"}))
2026-07-28 None
The first request says which revision it speaks, so any machine can answer it without having seen an earlier message. The second leaves the field out. A server doesn't guess: a request with a required _meta field missing gets an Invalid params error, −32602.
Popular servers run several copies behind a load balancer, and each request may land on a different one. See what that does to a server that keeps sessions in memory.
PV = "io.modelcontextprotocol/protocolVersion" class LegacyServer: def __init__(self): self.sessions = {} def handle(self, session, method): if method == "initialize": self.sessions[session] = "2025-03-26" return "ok" if session not in self.sessions: return "error: unknown session" return f"{method} answered" class ModernServer: def handle(self, meta, method): if PV not in meta: return "error: missing _meta" return f"{method} answered" copies = [LegacyServer(), LegacyServer()] print(copies[0].handle("s1", "initialize")) print(copies[1].handle("s1", "tools/list")) # the next request lands on the other copy copies = [ModernServer(), ModernServer()] meta = {PV: "2026-07-28"} print(copies[0].handle(meta, "tools/list")) print(copies[1].handle(meta, "tools/list"))
The legacy server fails as soon as a request reaches a copy that didn't see the handshake. Fixing that needs sticky routing or shared storage. A modern request carries everything needed, so any copy can answer it.
From params._meta["io.modelcontextprotocol/protocolVersion"] in that same request
Modern MCP is stateless: each request carries its version and capabilities. Initialize and session ids belong to legacy revisions.
Write a sentence or two, then compare it with a good answer in the lesson.
This part writes the client's side of the agreement and then the server's answer to the question "where do I put state now?".
There's no up-front agreement, so negotiation happens per request. Picture asking for something in French abroad, and the clerk answering "sorry, I speak English and German". You pick one of those and ask again.
If the server doesn't support the version a request names, it answers with UnsupportedProtocolVersion, error code -32022, and lists what it does support:
{"jsonrpc": "2.0", "id": 1, "error": {"code": -32022,
"message": "Unsupported protocol version",
"data": {"supported": ["2026-07-28", "2025-11-25"], "requested": "2027-01-15"}}}
The client picks a version both sides support, preferring the newest it knows, and retries. If there's none in common, it reports a clear error to the user, with both lists, instead of failing mysteriously. A client remembers the answer for that server, so it doesn't pay for the extra round trip on every call.
In plain words: ask in your newest language. If the server says no, pick the newest one you both speak.
A client prefers the newest version it knows. Given the server's supported list from an error, pick the best version both sides speak.
CLIENT_VERSIONS = ["2026-07-28", "2025-11-25", "2025-06-18"] # newest first def choose_version(server_supported): for v in CLIENT_VERSIONS: if v in server_supported: return v return None print(choose_version(["2025-11-25", "2025-06-18"])) print(choose_version(["2026-07-28"])) print(choose_version(["2024-11-05"]))
The client walks its own list from newest to oldest and takes the first one the server also lists. When there's nothing in common, it returns None, and a well-behaved client shows the user a clear message (with both lists) instead of failing mysteriously.
The same retry, against the practice server. The first request names a version the server doesn't speak, the error lists what it does, and the client retries with the best common version:
import requests URL = "https://api.gradient.test/mcp" PV = "io.modelcontextprotocol/protocolVersion" CAPS = "io.modelcontextprotocol/clientCapabilities" CLIENT = ["2026-07-28", "2025-06-18"] def send(method, version): body = {"jsonrpc": "2.0", "id": 1, "method": method, "params": {"_meta": {PV: version, CAPS: {}}}} headers = {"MCP-Protocol-Version": version, "Mcp-Method": method} return requests.post(URL, json=body, headers=headers) first = send("tools/list", "2027-01-15") error = first.json()["error"] print(first.status_code, error["code"], error["data"]["supported"]) best = next(v for v in CLIENT if v in error["data"]["supported"]) second = send("tools/list", best) print(second.status_code, best, len(second.json()["result"]["tools"]), "tools")
Write missing_keys(request): return the list of required _meta keys the request leaves out, in this order: protocolVersion, then clientCapabilities. A request with no params or no _meta is missing both.
PV = "io.modelcontextprotocol/protocolVersion" CAPS = "io.modelcontextprotocol/clientCapabilities" def missing_keys(request): return [] print(missing_keys({"method": "tools/list"}))
ours = ["2026-07-28", "2025-11-25", "2025-06-18"] theirs = ["2025-11-25", "2025-06-18"] print(next((v for v in ours if v in theirs), None))
2025-11-25
2026-07-28 isn't in theirs, so next moves on, and 2025-11-25 is the first that both lists have. The newest version the client knows that the server also supports wins. None would mean there's no common version.
Write negotiate(client_prefers, server_supports): return the first version in the client's preference order that the server also supports, or None if they share none. Versions are date strings like "2026-07-28".
def negotiate(client_prefers, server_supports): return max(server_supports) print( negotiate(["2026-07-28", "2025-11-25", "2025-06-18"], ["2025-06-18", "2025-11-25"]))
Some things really do have to last across calls. A shopping basket is the classic case: add an item now, pay for it three calls later. In stateless MCP the server still can't keep that in connection memory, because the next call may reach a different copy.
The answer is a handle: the server stores the basket somewhere all its copies can read, such as a database, and returns a short id like "bsk_0001". The client sends that id back as an ordinary tool argument on later calls. It works like a coat check. You hand over the coat, you get a ticket, and any attendant can fetch the coat from the ticket.
The server should also check that the handle belongs to the caller, so one customer can't open another's basket by guessing an id.
In plain words: state that must last gets a ticket, and the ticket travels with every request.
Two copies of a modern server share one store. The first call opens a basket and gets a handle; later calls pass the handle back, whichever copy they reach.
STORE = {} # stands in for a database every copy can reach
class ModernServer:
def open_basket(self):
handle = f"bsk_{len(STORE) + 1:04d}"
STORE[handle] = []
return handle
def add_item(self, handle, item):
if handle not in STORE:
return "error: unknown basket"
STORE[handle].append(item)
return f"{len(STORE[handle])} item(s) in {handle}"
copies = [ModernServer(), ModernServer()]
handle = copies[0].open_basket() # the first call lands on copy 0
print(copies[1].add_item(handle, "socks")) # the next lands on copy 1
print(copies[0].add_item(handle, "hat"))
print(copies[1].add_item("bsk_9999", "scarf"))The handle is the only thing the client holds. Both copies read the same store, so it doesn't matter which one gets the call: socks lands as item 1 and the hat as item 2 in the same basket. A made-up handle is refused.
Write a sentence or two, then compare it with a good answer in the lesson.
Purpose. Know what a real server and a real host will do with the versions you just handled by hand.
A real server also answers server/discover, the one request a client may send before any other. Here is the practice server's reply, copied from a real call:
The official Python SDK does this for you: version 2 of the mcp package implements the 2026-07-28 revision and still serves older clients, so one server can answer both eras, as the practice server does. Version 1.x keeps getting fixes for code that hasn't moved yet.
In an engineer's week. An upgrade week looks like this. A host updates and starts sending _meta on every request. A server that still expects initialize answers with an error, and tools vanish from the host. The fix is to read the error, look at data.supported, and either upgrade the server or keep a dual-era client that probes first. What goes wrong is a version check written as an exact match on one date, so the next revision breaks it.
Write a sentence or two, then compare it with a good answer in the lesson.
Start with a short baseline or choose guided, independent, or AI-assisted practice.
Which two _meta fields must every modern MCP request include?
575 lessons in thirteen phases. 176 are free without an account, and a free account opens the rest. See every lesson.
Build a Model Context Protocol client and server, then verify the integration with a compatible host.
Never trained an AI? Teach a small one to write, right in your browser. It takes about 20 seconds and needs no account. Teach it to write