Sub topic 4
The message format underneath: JSON-RPC 2.0
This chapter assumes you can read JSON, know the host, client and server roles from the earlier chapters, and have run a command-line program that reads from standard input. It covers the wire format that every MCP message uses. You need it to read logs, write a client by hand, and tell protocol failures from tool failures.
Why MCP builds on JSON-RPC
MCP does not define its own envelope for messages. It uses JSON-RPC 2.0, a small, transport-independent specification for calling named methods with JSON. The spec fits on a few pages and defines three kinds of message:
- A request asks the other side to run a method and expects an answer.
- A response answers exactly one earlier request, with either a result or an error.
- A notification is a one-way message. It gets no answer.
MCP adds method names (initialize, tools/list, tools/call, resources/read and so on) and the shape of each method's parameters and results. The framing and error rules come from JSON-RPC. That is why the same messages travel unchanged over both stdio and Streamable HTTP.
Requests
A request has four members:
| Member | Meaning |
|---|---|
| `jsonrpc` | Always the string `"2.0"`. |
| `id` | A string or number that identifies this request. |
| `method` | The name of the operation, such as `tools/call`. |
| `params` | An object holding the arguments. Optional for methods that take none. |
This is the request from the spine's calculator example:
{"jsonrpc": "2.0", "id": 1, "method": "tools/call", "params": {"name": "add", "arguments": {"a": 2, "b": 3}}}The method says what to do, params.name picks the tool, and params.arguments holds the tool's own inputs. Two layers of naming are easy to confuse here. tools/call is a protocol method. add is a tool that this particular server chose to expose.
MCP is stricter than plain JSON-RPC about id. JSON-RPC allows null as an id. MCP does not, and the sender must not reuse an id within the same session. A counter that starts at 1 is enough.
Responses
A response repeats the request's id so the sender can match the two. It contains either result or error, never both.
A successful reply to the request above looks like this:
{"jsonrpc": "2.0", "id": 1, "result": {"content": [{"type": "text", "text": "5"}], "isError": false}}The exact result contents depend on the method and on the protocol version. Recent versions may also include a structuredContent member next to content. The id and the result member are the parts you can rely on.
A failed reply replaces result with an error object:
{"jsonrpc": "2.0", "id": 1, "error": {"code": -32602, "message": "Unknown tool: subtract"}}An error object has an integer code, a human-readable message, and an optional data member for extra detail.
Standard error codes
JSON-RPC reserves a block of codes for protocol-level problems:
| Code | Name | Typical cause |
|---|---|---|
| -32700 | Parse error | The bytes were not valid JSON. |
| -32600 | Invalid request | Valid JSON, but not a valid request object. |
| -32601 | Method not found | The server does not implement that method. |
| -32602 | Invalid params | Wrong or missing parameters, or an unknown tool name. |
| -32603 | Internal error | The server failed while handling the request. |
Codes from -32000 to -32099 are left for implementation-defined server errors. MCP also uses some codes of its own, for example for a resource that cannot be found. Check the spec revision your SDK follows before depending on one.
Notifications
A notification is a request with no id. Because there is no id, the receiver must not reply, and the sender never learns whether it was processed. MCP uses notifications for events that need no answer:
{"jsonrpc": "2.0", "method": "notifications/initialized"}Other common notifications are:
notifications/tools/list_changed, sent by a server when its tool list changes.notifications/progress, sent during a long-running request.notifications/cancelled, sent by the side that gave up waiting.
A message with an id but no method is a response. A message with a method but no id is a notification. A message with both is a request. When you read a raw log, this three-way test tells you what each line is.
Both sides can send requests
JSON-RPC is symmetric, and MCP uses that. The client usually sends requests to the server, but the server can also send requests to the client. Examples are ping, roots/list (asking which directories the client exposes) and sampling/createMessage (asking the host to run a model completion). Each side keeps its own id counter, so a client request with id: 1 and a server request with id: 1 can exist at the same time without conflict. Ids only need to be unique among the requests sent by one side.
A client must therefore do more than send a request and read one reply. It has to read every incoming message and dispatch it by shape: response, request or notification.
Matching responses and handling concurrency
Responses do not have to arrive in the order the requests were sent. A server may handle several requests at once, and a fast ping may return before a slow tools/call that was sent earlier. The id is what makes this safe. A correct client keeps a table of pending requests keyed by id, and resolves the matching entry when a response arrives.
If a client stops caring about a request, it can send notifications/cancelled with the requestId of the call. The server should stop work and send no response for it. The race is real, though. A response may already be in flight when the cancellation arrives, and the client must ignore it.
For long operations, the client can put a progressToken in the request's params._meta. The server then sends notifications/progress messages that carry the same token.
Framing on the wire
JSON-RPC says nothing about how one message ends and the next begins. The transport decides that.
- stdio. Each message is one line of JSON, ended by a newline. The JSON must not contain raw newlines, so the serializer must not pretty-print. The server reads from standard input and writes to standard output.
- Streamable HTTP. Each client message is the body of an HTTP POST. The server replies with either a single JSON body or an event stream that carries one or more messages.
The stdio rule has one important consequence. Standard output belongs to the protocol. A stray print("starting up") in your server writes a line that is not JSON-RPC, and the client may fail to parse it or drop the connection. Send logs to standard error instead.
Two kinds of failure
JSON-RPC errors report failures of the protocol exchange. A tool that runs but fails at its job is a different case. If a tools/call reaches a tool and the tool raises an exception, such as a file that does not exist, MCP expects a normal result with isError: true:
{"jsonrpc": "2.0", "id": 7, "result": {"content": [{"type": "text", "text": "File not found: notes.txt"}], "isError": true}}The split matters for the model. An error object is a protocol-level problem, and a host may treat it as a bug to report. A result with isError: true is information the model can read and act on, for example by trying another path. When you write a server, return isError: true for failures the model could fix. Reserve JSON-RPC errors for malformed calls, unknown methods and internal faults.
Driving a server by hand
The best way to learn the format is to speak it yourself. This script starts the calculator server from the earlier example, assumed to be saved as server.py. It performs the handshake and calls add. It needs the mcp package installed.
import json
import subprocess
import sys
proc = subprocess.Popen(
[sys.executable, "server.py"],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
text=True,
)
def send(message):
proc.stdin.write(json.dumps(message) + "\n")
proc.stdin.flush()
def receive():
return json.loads(proc.stdout.readline())
send({
"jsonrpc": "2.0",
"id": 1,
"method": "initialize",
"params": {
"protocolVersion": "2025-06-18",
"capabilities": {},
"clientInfo": {"name": "hand-client", "version": "0.1"},
},
})
print(receive())
send({"jsonrpc": "2.0", "method": "notifications/initialized"})
send({
"jsonrpc": "2.0",
"id": 2,
"method": "tools/call",
"params": {"name": "add", "arguments": {"a": 2, "b": 3}},
})
print(receive())
proc.stdin.close()
proc.wait()The script sends initialize as a request and waits for the reply. It then sends notifications/initialized as a notification, with no id and no reply, and only after that makes the tool call. The next chapter explains why that order matters. This script reads one line per request and assumes the next line is the matching response. That works for a quiet server. A real client must follow the dispatch rules above, because the server may interleave notifications or its own requests.
Try changing the name to "subtract". You should get an error response instead of a result. Then change "a": 2 to "a": "two" and compare the two failures.
Common mistakes
- Sending an id on a notification. The receiver treats it as a request and replies, or rejects it.
- Replying to a notification. Notifications never get a response, even an error.
- Pretty-printing JSON on stdio. Newlines inside a message break the framing.
- Assuming reply order. Always match by
id. - Reusing ids. Two in-flight requests with one id make the replies ambiguous.
- Relying on batching. JSON-RPC allows arrays of messages, but MCP revisions have differed on supporting them, and later ones dropped them. Send one message at a time.
- Reading
resultwithout checkingerror. Always test forerrorfirst, then checkisErrorinside tool results.
Exercise
You see these three lines in a stdio log. Classify each as request, response or notification, and say whether the receiver should reply. 1. {"jsonrpc":"2.0","method":"notifications/tools/list_changed"} 2. {"jsonrpc":"2.0","id":4,"method":"tools/list"} 3. {"jsonrpc":"2.0","id":4,"result":{"tools":[]}}
Show answer
Line 1 has a method and no id, so it is a notification, and the receiver must not reply. Line 2 has both an id and a method, so it is a request, and the receiver must reply with a result or error carrying id 4. Line 3 has an id and a result but no method, so it is a response to line 2, and nobody replies to it.
Exercise
A tool that reads a file is called with a path that does not exist. Should the server return a JSON-RPC error object or a normal result? Explain.
Show answer
It should return a normal result with isError set to true and a text message such as "File not found". The call was well formed and reached the tool, so this is a tool failure, not a protocol failure. A result lets the model read the message and try another path. A JSON-RPC error is for malformed requests, unknown methods or unknown tools, and internal faults.