Lesson 35.6 · 6 of 6 · 8 min read · Reviewed 24 Nov 2026
Capability negotiation and the connection lifecycle
By the end, you can
- Trace the MCP connection lifecycle: initialize request and response, the initialized notification, normal operation and shutdown.
- Explain how the protocol version and capabilities are negotiated, and how the 2026-07-28 revision moves them onto every request.
- Spot lifecycle bugs, such as using a feature the other side never declared.
Before a client and a server exchange any tool calls, they must agree on two things: which protocol version they speak, and which optional capabilities each side supports. MCPModel Context Protocol (MCP): An open protocol that standardises how AI applications connect to tools, data and prompts through servers. revisions are named by date. Up to 2025-11-25, that agreement happens once, in a handshake at the start of each connection. The current revision, 2026-07-28, moves it onto every request. You will meet both, because the spec tells new clients how to fall back to the handshake for older servers.
The handshake lifecycle (2025-11-25 and earlier)
A connection has three phases: initialization, operation and shutdown.
1. The initialize request
The client must speak first. It sends its latest supported version, its capabilities and who it is:
{ "jsonrpc": "2.0", "id": 1, "method": "initialize",
"params": {
"protocolVersion": "2025-11-25",
"capabilities": { "elicitation": {}, "roots": { "listChanged": true } },
"clientInfo": { "name": "demo-host", "version": "0.3.0" } } }
2. The initialize response
The server answers with the version it will use, its own capabilities and its identity:
{ "jsonrpc": "2.0", "id": 1,
"result": {
"protocolVersion": "2025-11-25",
"capabilities": { "tools": { "listChanged": true }, "resources": { "subscribe": true } },
"serverInfo": { "name": "tickets-server", "version": "2.1.0" } } }
Version negotiation. If the server supports the requested version, it must reply with the same one. If not, it replies with another version it supports, normally its latest. If the client cannot speak that version, it should disconnect.
Capability negotiation. Capabilities switch optional features on. This server offers tools, says it will notify when its tool list changes, and lets clients subscribe to resource changes. It did not declare prompts, so the client must not call prompts/list. The client declared elicitation, so the server may ask the user for input mid-task; it did not declare sampling, so the server must not ask the client’s model to generate text.
3. The initialized notification
The client confirms it is ready:
{ "jsonrpc": "2.0", "method": "notifications/initialized" }
It has no id because it is a notification: nothing replies. Until the server has answered initialize, the client should send nothing except pings. Until the server receives initialized, it should send nothing except pings and log messages.
4. Operation
Now the normal traffic flows: tools/list, tools/call, resources/read and the rest, all within the agreed version and capabilities. Set a timeout on every request; if one expires, send a cancellation notification and stop waiting.
5. Shutdown
There is no shutdown message. The transport signals the end. On stdiostdio transport: Running an MCP server as a local process that talks to the client over standard input and output. the client closes the server’s input streamStreaming: Receiving a response as a series of server-sent events while it is generated, instead of waiting for the whole reply., waits for it to exit, then sends SIGTERM and finally SIGKILL if it hangs. On HTTP, closing the connection ends it.
The stateless revision (2026-07-28)
The current spec removes initialize and notifications/initialized. There is no session to set up. Instead:
- Every request carries its version and the client’s capabilities in
_meta, under keys such asio.modelcontextprotocol/protocolVersionandio.modelcontextprotocol/clientCapabilities. - If the server does not support that version, it returns an UnsupportedProtocolVersionError (code
-32022) listing the versions it does support. The client picks one and retries. - Servers must implement
server/discover, which returnssupportedVersions,capabilitiesand the server’s identity in one call. Calling it first is optional. - A dual-era client talking over stdio should send
server/discoverfirst. A recognised modern reply means a modern server; any other error, or a timeout, means a legacy server, so it falls back toinitialize.
The idea is the same in both eras: agree on the version, then only use what both sides declared. What changed is where the agreement lives.
Common mistakes
| Bug | Era | What you see | Fix |
|---|---|---|---|
| Calling a capability the server never declared | Both | Errors from the server, or a host menu offering features that never work | Check the server’s capabilities before showing or calling a feature |
| Requests before the handshake finishes | 2025-11-25 and earlier | The first calls are ignored or rejected | Send notifications/initialized and only then start operation |
| Ignoring the server’s chosen version | 2025-11-25 and earlier | Odd field errors later in the session | Compare the response’s version with yours; disconnect if unsupported |
Leaving out the version or capabilities in _meta |
2026-07-28 | The server can’t tell what you support and rejects or downgrades the call | Send them on every request, not just the first |
Treating -32022 as a fatal error |
2026-07-28 | Calls fail against a server on another version | Read its supported versions, pick one you share, and retry |
| Relying on a session that no longer exists | 2026-07-28 | State from one call is missing in the next | Pass an explicit handle, such as an id the tool returned |
| Hard-coding one version | Both | Breaks when either side upgrades | Support a list, and negotiate or retry |
| No request timeouts | Both | A hung server freezes the host | Time out, cancel, and report the failure |
Key points
- In handshake-based revisions (2025-11-25 and earlier), every connection starts with initialize, its response, then notifications/initialized.
- The client offers its latest version; the server echoes it or offers its own latest; the client disconnects if it cannot speak that version.
- Each side may only use capabilities the other declared, such as tools, resources with subscribe, or listChanged notifications.
- Revision 2026-07-28 drops the handshake: each request carries its version and client capabilities in _meta, and server/discover reports what a server supports.
Negotiate a version and capabilities
Run the initialize handshake against a toy server, respect the capabilities it declared, and handle a version mismatch in the stateless revision.
You will have: A toy client and server that negotiate a version, send notifications/initialized, skip undeclared features and retry after an unsupported-version error.
Steps
- Make the server echo the requested version if it supports it, and otherwise offer its newest one.
- Make the client disconnect when it cannot speak the offered version.
- Send notifications/initialized after a successful response, then list tools.
- Skip any call whose capability the server did not declare.
- In the stateless part, retry once with a version from the error's supported list.
Done when
- The client and server agree on 2025-06-18.
- resources/list is skipped because the server never declared resources.
- The stateless call retries with 2026-07-28 after error -32022 and completes.
Open in the Playground No API key needed.
Starter code (Python)
import json
class LegacyServer:
"""A toy server for handshake-based revisions (2025-11-25 and earlier)."""
SUPPORTED = ["2025-06-18", "2025-03-26"] # newest first
CAPABILITIES = {"tools": {"listChanged": True}} # no resources, no prompts
def __init__(self):
self.state = "new"
def handle(self, msg):
method = msg["method"]
if method == "initialize":
asked = msg["params"]["protocolVersion"]
version = asked # TODO: echo asked if supported, otherwise offer SUPPORTED[0]
self.state = "initializing"
return {"jsonrpc": "2.0", "id": msg["id"], "result": {
"protocolVersion": version, "capabilities": self.CAPABILITIES,
"serverInfo": {"name": "tickets-server", "version": "2.1.0"}}}
if method == "notifications/initialized":
self.state = "ready"
return None # notifications get no reply
if self.state != "ready":
return {"jsonrpc": "2.0", "id": msg["id"], "error": {"code": -32600, "message": "not initialized"}}
if method == "tools/list":
return {"jsonrpc": "2.0", "id": msg["id"], "result": {"tools": [{"name": "create_ticket"}]}}
return {"jsonrpc": "2.0", "id": msg["id"], "error": {"code": -32601, "message": "method not found"}}
class Client:
def __init__(self, server, versions):
self.server, self.versions, self.next_id = server, versions, 1
self.server_caps, self.version = {}, None
def request(self, method, params=None):
msg = {"jsonrpc": "2.0", "id": self.next_id, "method": method, "params": params or {}}
self.next_id += 1
return self.server.handle(msg)
def notify(self, method):
self.server.handle({"jsonrpc": "2.0", "method": method})
def connect(self):
reply = self.request("initialize", {"protocolVersion": self.versions[0], "capabilities": {},
"clientInfo": {"name": "demo-host", "version": "0.3.0"}})
offered = reply["result"]["protocolVersion"]
# TODO: if offered is not in self.versions, print "disconnect: ..." and return False
self.version, self.server_caps = offered, reply["result"]["capabilities"]
# TODO: send the notifications/initialized notification
print("agreed version:", self.version, "| server capabilities:", json.dumps(self.server_caps))
return True
def call_if_declared(self, capability, method):
# TODO: if capability is not in self.server_caps, print "skip ..." and return None
return self.request(method)
# 1. Too early: a request before the handshake is rejected.
early = Client(LegacyServer(), ["2025-11-25", "2025-06-18"])
print("before handshake:", early.request("tools/list")["error"]["message"])
# 2. Negotiation that works: the server offers an older version the client also speaks.
client = Client(LegacyServer(), ["2025-11-25", "2025-06-18"])
if client.connect():
print("tools/list:", client.call_if_declared("tools", "tools/list"))
print("resources/list:", client.call_if_declared("resources", "resources/list"))
# 3. Negotiation that fails: the client cannot speak the offered version.
Client(LegacyServer(), ["2025-11-25"]).connect()
# 4. The stateless revision: the version travels on every request, and a mismatch means retry.
def modern_handle(req, supported=("2026-07-28",)):
asked = req["params"]["_meta"]["io.modelcontextprotocol/protocolVersion"]
if asked not in supported:
return {"error": {"code": -32022, "message": "Unsupported protocol version",
"data": {"supported": list(supported), "requested": asked}}}
return {"result": {"resultType": "complete", "tools": [{"name": "create_ticket"}]}}
def modern_call(method, version):
req = {"jsonrpc": "2.0", "id": 1, "method": method,
"params": {"_meta": {"io.modelcontextprotocol/protocolVersion": version}}}
reply = modern_handle(req)
if "error" in reply:
# TODO: on code -32022, pick a version from reply["error"]["data"]["supported"] and retry once
print(f"stateless {method} with {version}: error {reply['error']['code']}")
return reply
print(f"stateless {method} with {version}: {reply['result']['resultType']}")
return reply
modern_call("tools/list", "2099-01-01") # a made-up future revision the server does not know
Show a solution
import json
class LegacyServer:
"""A toy server for handshake-based revisions (2025-11-25 and earlier)."""
SUPPORTED = ["2025-06-18", "2025-03-26"] # newest first
CAPABILITIES = {"tools": {"listChanged": True}} # no resources, no prompts
def __init__(self):
self.state = "new"
def handle(self, msg):
method = msg["method"]
if method == "initialize":
asked = msg["params"]["protocolVersion"]
version = asked if asked in self.SUPPORTED else self.SUPPORTED[0]
self.state = "initializing"
return {"jsonrpc": "2.0", "id": msg["id"], "result": {
"protocolVersion": version, "capabilities": self.CAPABILITIES,
"serverInfo": {"name": "tickets-server", "version": "2.1.0"}}}
if method == "notifications/initialized":
self.state = "ready"
return None # notifications get no reply
if self.state != "ready":
return {"jsonrpc": "2.0", "id": msg["id"], "error": {"code": -32600, "message": "not initialized"}}
if method == "tools/list":
return {"jsonrpc": "2.0", "id": msg["id"], "result": {"tools": [{"name": "create_ticket"}]}}
return {"jsonrpc": "2.0", "id": msg["id"], "error": {"code": -32601, "message": "method not found"}}
class Client:
def __init__(self, server, versions):
self.server, self.versions, self.next_id = server, versions, 1
self.server_caps, self.version = {}, None
def request(self, method, params=None):
msg = {"jsonrpc": "2.0", "id": self.next_id, "method": method, "params": params or {}}
self.next_id += 1
return self.server.handle(msg)
def notify(self, method):
self.server.handle({"jsonrpc": "2.0", "method": method})
def connect(self):
reply = self.request("initialize", {"protocolVersion": self.versions[0], "capabilities": {},
"clientInfo": {"name": "demo-host", "version": "0.3.0"}})
offered = reply["result"]["protocolVersion"]
if offered not in self.versions:
print(f"disconnect: server offered {offered}, client speaks {self.versions}")
return False
self.version, self.server_caps = offered, reply["result"]["capabilities"]
self.notify("notifications/initialized")
print("agreed version:", self.version, "| server capabilities:", json.dumps(self.server_caps))
return True
def call_if_declared(self, capability, method):
if capability not in self.server_caps:
print(f"skip {method}: server did not declare {capability}")
return None
return self.request(method)
# 1. Too early: a request before the handshake is rejected.
early = Client(LegacyServer(), ["2025-11-25", "2025-06-18"])
print("before handshake:", early.request("tools/list")["error"]["message"])
# 2. Negotiation that works: the server offers an older version the client also speaks.
client = Client(LegacyServer(), ["2025-11-25", "2025-06-18"])
if client.connect():
print("tools:", [t["name"] for t in client.call_if_declared("tools", "tools/list")["result"]["tools"]])
client.call_if_declared("resources", "resources/list")
# 3. Negotiation that fails: the client cannot speak the offered version.
Client(LegacyServer(), ["2025-11-25"]).connect()
# 4. The stateless revision: the version travels on every request, and a mismatch means retry.
def modern_handle(req, supported=("2026-07-28",)):
asked = req["params"]["_meta"]["io.modelcontextprotocol/protocolVersion"]
if asked not in supported:
return {"error": {"code": -32022, "message": "Unsupported protocol version",
"data": {"supported": list(supported), "requested": asked}}}
return {"result": {"resultType": "complete", "tools": [{"name": "create_ticket"}]}}
def modern_call(method, version):
req = {"jsonrpc": "2.0", "id": 1, "method": method,
"params": {"_meta": {"io.modelcontextprotocol/protocolVersion": version}}}
reply = modern_handle(req)
if "error" in reply and reply["error"]["code"] == -32022:
retry = reply["error"]["data"]["supported"][0]
print(f"{version} unsupported (code -32022); retry with {retry}")
return modern_call(method, retry)
print(f"stateless {method} with {version}: {reply['result']['resultType']}")
return reply
modern_call("tools/list", "2099-01-01") # a made-up future revision the server does not know
Finished the chapter? Take the chapter check.