Sub topic 3
The wire format: JSON-RPC requests, responses, and notifications
Every message in the Model Context Protocol (MCP) is a JSON-RPC 2.0 message. This chapter covers the three message shapes, how requests are matched to responses, how errors are reported, and the mistakes that most often break a first implementation.
This chapter assumes you can read JSON, know the basics of client-server communication, and know that MCP has hosts, clients, and servers. You do not need to know how a session starts. The next chapter covers that.
Why a small wire format matters
JSON-RPC 2.0 is a lightweight convention for calling methods on a remote peer. It specifies only the shape of messages. It says nothing about how they travel. MCP uses it for this reason. The same messages can move over a subprocess pipe (stdio) or over HTTP without any change to their content.
Three message kinds exist:
| Kind | Has `id` | Has `method` | Gets a reply |
|---|---|---|---|
| Request | yes | yes | yes, exactly one |
| Response | yes (copied from the request) | no | no |
| Notification | no | yes | no |
A response contains either result or error, never both.
Requests
A request asks the other side to do something and return an answer. It has four members:
jsonrpc: always the string"2.0".id: a string or number that identifies this request.method: the name of the operation, such asinitializeortools/call.params: an object with the arguments. It may be left out when a method takes none.
MCP adds two rules on top of base JSON-RPC. The id must not be null, and a sender must not reuse an id within the same session. Both rules make it safe to match replies to requests by id alone.
Here is the request that opens a session. The client states the protocol version it supports, the capabilities it offers, and its own name and version.
{"jsonrpc": "2.0", "id": 1, "method": "initialize", "params": {"protocolVersion": "2025-06-18", "capabilities": {"sampling": {}}, "clientInfo": {"name": "example-client", "version": "1.0.0"}}}Method names use a namespace/action style. Examples are tools/list, tools/call, resources/read, and prompts/get. Names that start with notifications/ are reserved for notifications.
Responses
A response answers exactly one request. It carries the same id as the request, so the sender can match them. This matters because messages may overlap. A client can send request 5 and request 6 before either has an answer, and the replies can arrive in any order.
A successful response has a result member. Its contents depend on the method. Below is a tools/call request followed by its reply:
{"jsonrpc": "2.0", "id": 2, "method": "tools/call", "params": {"name": "add", "arguments": {"a": 2, "b": 3}}}
{"jsonrpc": "2.0", "id": 2, "result": {"content": [{"type": "text", "text": "5"}], "isError": false}}The reply has the same id (2). The result holds a list of content items, here one text item with the value "5". The isError flag is part of the tool result, not of JSON-RPC. The section on two kinds of failure explains why that difference matters.
Notifications
A notification is a message that expects no answer. It has a method and optional params, but no id. Because there is no id, the receiver has nothing to reply to and must not send a response.
Notifications report events. A client sends one after the handshake to say it is ready:
{"jsonrpc": "2.0", "method": "notifications/initialized"}Other examples are notifications/progress, notifications/cancelled, and notifications/tools/list_changed. Later chapters cover them.
The lack of a reply has a cost. A sender cannot tell whether a notification arrived or was understood. Use a notification only when losing it is acceptable, or when a later request will reveal the problem.
Messages go both ways
In MCP, either side can send requests. Most traffic runs from client to server, as with tools/call. A server can also send requests to the client, for example sampling/createMessage (to ask the host's model for a completion) or roots/list. Each direction keeps its own set of id values. A client's request 1 and a server's request 1 do not collide, because each side matches responses only against the requests it sent.
Errors
When a request fails at the protocol level, the response has an error member in place of result. The error object has a numeric code, a short message, and an optional data value for details.
{"jsonrpc": "2.0", "id": 3, "error": {"code": -32602, "message": "Unknown tool: multiply"}}The standard codes come from JSON-RPC:
| Code | Name | Typical cause |
|---|---|---|
| -32700 | Parse error | The text is not valid JSON |
| -32600 | Invalid request | The JSON does not match the request shape |
| -32601 | Method not found | The peer does not support the method |
| -32602 | Invalid params | Wrong or missing arguments, or an unknown name |
| -32603 | Internal error | The handler crashed |
Codes from -32768 to -32000 are reserved for the standard. Servers may define their own codes outside that range.
Two kinds of failure
A tool call can fail in two different ways, and they use different channels.
- Protocol error. The request itself was wrong: the method does not exist, the tool name is unknown, or the arguments do not match the schema. The server returns a JSON-RPC
error. - Tool execution error. The request was fine, but the tool ran and failed, for example a file was missing or an upstream API returned 500. The server returns a normal
resultwith"isError": trueand a message incontent.
{"jsonrpc": "2.0", "id": 4, "result": {"content": [{"type": "text", "text": "File not found: report.csv"}], "isError": true}}The split is deliberate. The model reads the content of a result, so an isError message can tell it what went wrong and let it try again. A protocol error is usually handled by the client code and may never reach the model.
Batching and ordering
Base JSON-RPC allows a batch, which is an array of messages sent together. The MCP revision 2025-06-18 removed batching support, so send one message per JSON object. Do not rely on messages arriving in the same order they were sent for matching purposes. Match by id.
Framing: where one message ends
JSON-RPC defines the content of a message, not its boundaries. The transport has to mark them. Over stdio, MCP sends one JSON message per line, ending in a newline, so a message must not contain a raw newline. Compact JSON, or JSON with newlines escaped inside strings, satisfies this. Over Streamable HTTP, each message travels in an HTTP request or response body, or as an event in a server-sent event stream. The chapter on transports covers this.
Common mistakes
- Replying to a notification. A response to a message with no
idhas nothing to match and is a protocol violation. - Missing
idon a request. Without anid, the message is a notification, and no answer will come. A client waiting on it will hang. - Using
nullor a repeatedid. MCP forbids both. A repeatedidmakes responses ambiguous. - Putting both
resultanderrorin one response. Only one is allowed. - Treating
isError: trueas a protocol failure. It is a successful response that carries a failure message. - Writing logs to stdout on a stdio server. Stdout is the message channel. Any stray
printcorrupts the stream. Write logs to stderr. - Pretty-printing JSON on stdio. Embedded newlines split one message into several lines and break framing.
- Treating
idas always a number. Strings are valid. Echo the value back with the same type.
A small parser
The function below sorts an incoming message into the three kinds. It is a useful model of the rules above.
import json
def classify(line: str) -> str:
msg = json.loads(line)
if msg.get("jsonrpc") != "2.0":
raise ValueError("not a JSON-RPC 2.0 message")
has_id = "id" in msg
if "method" in msg:
return "request" if has_id else "notification"
if has_id and ("result" in msg) != ("error" in msg):
return "response"
raise ValueError("invalid message shape")
print(classify('{"jsonrpc": "2.0", "id": 2, "method": "tools/call"}'))
print(classify('{"jsonrpc": "2.0", "method": "notifications/initialized"}'))
print(classify('{"jsonrpc": "2.0", "id": 2, "result": {}}'))Running it prints request, notification, and response. The check ("result" in msg) != ("error" in msg) enforces that a response holds exactly one of the two members.
Exercise
A client sends {"jsonrpc": "2.0", "method": "tools/call", "params": {"name": "add", "arguments": {"a": 1, "b": 2}}} and waits for the answer, but nothing ever arrives. What is wrong, and how do you fix it?
Show answer
The message has no id, so it is a notification, and the server must not reply to it. Add a unique id, such as "id": 7, so the message becomes a request. The server will then answer with a response carrying "id": 7.
Exercise
A tool tries to read a file that does not exist. Should the server return a JSON-RPC error or a result? Give the reason.
Show answer
It should return a result with "isError": true and a message in content. The request was valid and the tool ran, so this is a tool execution error. Returning it in content lets the model read the message and try a different action. A JSON-RPC error is for protocol problems such as an unknown tool name or invalid arguments.