Module 6 · Lesson 4 · 18 min read
JSON-RPC messages and the request lifecycle in MCP
JSON-RPC Messages and the MCP Request Lifecycle
Introduction
Everything that passes between the client and the server in MCP is messages in JSON-RPC 2.0 format. This lesson explains the types of these messages and the error codes, then compares the stateless design of the 2026-07-28 specification with the old flow based on the initialize handshake, and ends by building a small request handler in Python.
Learning objectives
- Distinguish between a request, a response and a notification in JSON-RPC 2.0
- Explain the stateless design and the role of _meta and server/discover in the 2026-07-28 specification
- Tell the protocol error -32602 apart from the tool execution error isError
- Recognize the old initialize and notifications/initialized flow as history
- Build a request handler that returns error -32022 for an unsupported version
JSON-RPC 2.0 basics
JSON-RPC 2.0 is a lightweight protocol for remote procedure calls, and its messages are of three types:
- Request: carries
jsonrpcwith the value"2.0", a uniqueid, amethod, and optionalparams. It expects a response. - Response: carries the same request
id, together with eitherresulton success orerroron failure, never both. - Notification: like a request but without an
id, and it expects no response.
The error object carries code and message and may carry data. Standard codes include: -32700 for a JSON parse error, -32600 for an invalid request, -32601 for a method that does not exist, -32602 for invalid arguments, and -32603 for an internal error.
The 2026-07-28 specification: every request is self-contained
The most important change in the current specification is that it is stateless. There is no initialization handshake, and the server does not need to remember anything about the client between requests. Instead:
- Every request carries the protocol version in
params._metaunder the keyio.modelcontextprotocol/protocolVersion, and the client's capabilities are also sent within_meta. - When HTTP is used, the version is also sent in the
MCP-Protocol-Versionheader. - Every server must provide the
server/discovermethod so the client can learn about the server and its capabilities whenever it needs to. - If the version is not supported, the server returns error
-32022, and puts the versions it supports indata.supported. - Every result carries the
resultTypefield: the valuecompletewhen it is complete, and the valueinput_requiredwhen the server needs additional input from the client to complete the request. This pattern replaces the requests the server used to send to the client in earlier versions.
A server discovery request:
{
"jsonrpc": "2.0",
"id": 1,
"method": "server/discover",
"params": {"_meta": {"io.modelcontextprotocol/protocolVersion": "2026-07-28"}}
}And the server's response if it is sent a version it does not support:
{
"jsonrpc": "2.0",
"id": 2,
"error": {
"code": -32022,
"message": "Unsupported protocol version",
"data": {"supported": ["2026-07-28"]}
}
}Protocol error or execution error?
This is a distinction many people get wrong:
| Status | How it is returned | Example |
|---|---|---|
| Protocol error | A JSON-RPC response with an error field | An unknown tool: code -32602 |
| Tool execution error | A normal result containing isError: true | The weather API did not respond |
The reason is that an execution error is useful information for the model: it reads it in content and retries or chooses another route. A protocol error, on the other hand, means the request itself is invalid.
The old flow: initialize
In those versions the connection started with an initialize request in which the client sent its version and capabilities, the server replied with its version and capabilities, then the client sent the notifications/initialized notification, and only after that did normal requests begin:
{
"jsonrpc": "2.0",
"id": 1,
"method": "initialize",
"params": {
"protocolVersion": "2025-11-25",
"capabilities": {},
"clientInfo": {"name": "study-helper", "version": "0.1.0"}
}
}In the 2026-07-28 specification this step has disappeared, because every request carries what it needs in _meta.
A practical example: a stateless request handler
We build in Python (with no libraries) a handler that receives JSON text and returns JSON text, and keeps no state between requests:
import json
SUPPORTED = ["2026-07-28"]
TOOLS = {"add": lambda a, b: a + b, "divide": lambda a, b: a / b}
def ok(rid, result):
return {"jsonrpc": "2.0", "id": rid, "result": {**result, "resultType": "complete"}}
def err(rid, code, message, data=None):
e = {"code": code, "message": message}
if data is not None:
e["data"] = data
return {"jsonrpc": "2.0", "id": rid, "error": e}
def handle(raw):
try:
req = json.loads(raw)
except json.JSONDecodeError:
return json.dumps(err(None, -32700, "Parse error"))
rid, params = req.get("id"), req.get("params", {})
version = params.get("_meta", {}).get("io.modelcontextprotocol/protocolVersion")
if version not in SUPPORTED: # a simplification: we treat a missing version as unsupported
return json.dumps(err(rid, -32022, "Unsupported protocol version", {"supported": SUPPORTED}))
method = req.get("method")
if method == "server/discover":
return json.dumps(ok(rid, {"serverInfo": {"name": "calc-demo"}}), ensure_ascii=False)
if method == "tools/call":
name = params.get("name")
if name not in TOOLS:
return json.dumps(err(rid, -32602, f"Unknown tool: {name}"))
try:
value = TOOLS[name](**params.get("arguments", {}))
return json.dumps(ok(rid, {"content": [{"type": "text", "text": str(value)}], "isError": False}))
except Exception as e:
return json.dumps(ok(rid, {"content": [{"type": "text", "text": f"Execution failed: {e}"}], "isError": True}), ensure_ascii=False)
return json.dumps(err(rid, -32601, "Method not found"))
meta = {"io.modelcontextprotocol/protocolVersion": "2026-07-28"}
tests = [
{"jsonrpc": "2.0", "id": 1, "method": "server/discover", "params": {"_meta": meta}},
{"jsonrpc": "2.0", "id": 2, "method": "tools/call", "params": {"name": "add", "arguments": {"a": 2, "b": 3}, "_meta": meta}},
{"jsonrpc": "2.0", "id": 3, "method": "tools/call", "params": {"name": "divide", "arguments": {"a": 1, "b": 0}, "_meta": meta}},
{"jsonrpc": "2.0", "id": 4, "method": "tools/call", "params": {"name": "power", "arguments": {}, "_meta": meta}},
{"jsonrpc": "2.0", "id": 5, "method": "tools/list", "params": {"_meta": {"io.modelcontextprotocol/protocolVersion": "2024-01-01"}}},
]
for t in tests:
print(handle(json.dumps(t)))The division by zero returned a result containing isError: true, the unknown tool returned error -32602, and the unsupported version returned -32022 with the list of supported versions. The serverInfo field here is a teaching simplification, so refer to the specification for the full fields of the server/discover result.
Summary
MCP messages are requests, responses and notifications in JSON-RPC 2.0 format. In the 2026-07-28 specification there is no initialization handshake: every request carries the version in _meta, which is also sent in the MCP-Protocol-Version header over HTTP, the server provides server/discover, and it returns -32022 for an unsupported version. An unknown tool is protocol error -32602, while a tool execution failure is a result with the field isError: true. And the flow based on initialize and notifications/initialized belongs to versions up to 2025-11-25.
Terms
| Term | English | Definition |
|---|---|---|
| JSON-RPC 2.0 protocol | JSON-RPC 2.0 | A lightweight format for requests, responses and notifications encoded in JSON |
| Notification | Notification | A message without an id that expects no response |
| Metadata field | _meta | A field in params that carries the protocol version and the client's capabilities |
| Server discovery method | server/discover | A mandatory method through which the client learns about the server and its capabilities |
| Unsupported version error | Unsupported Protocol Version (-32022) | The server's response when it does not support the version sent |
| Execution error flag | isError | A field in a tool's result indicating that it failed to run |
Review questions
- What is the difference between a request and a notification in JSON-RPC 2.0?
- Why does the 2026-07-28 specification not need an initialize request?
- When does the server return code -32602, and when does it return a result containing isError: true?
- What does the server put in data when returning error -32022, and why is that useful to the client?
References
- MCP Specification 2026-07-28: Versioning modelcontextprotocol.io Reviewed on 2026-11-18
- MCP Specification 2026-07-28: Changelog modelcontextprotocol.io Reviewed on 2026-11-18
- MCP Specification 2026-07-28: Tools modelcontextprotocol.io Reviewed on 2026-11-18
