Sub topic 4
The message format underneath: JSON-RPC 2.0
Every MCP exchange between a client and a server is a JSON-RPC 2.0 message. This chapter assumes you can read JSON and know the host, client and server roles from the architecture chapter. You don't need to write any JSON-RPC code. You need to read messages well enough to follow a trace or debug a server.
What JSON-RPC 2.0 is
JSON-RPC 2.0 is a small protocol for calling a named method on a remote process and getting a result back. Every message is a JSON object with the field "jsonrpc": "2.0". MCP uses it so that tools, resources and prompts all share one envelope, whatever transport carries it.
There are three kinds of message:
| Kind | Has `id` | Has `method` | Expects a reply |
|---|---|---|---|
| Request | Yes | Yes | Yes |
| Response | Yes (matches the request) | No | No |
| Notification | No | Yes | No |
Either side can send requests and notifications. In MCP the client usually asks and the server answers, but servers can also send requests, for example to ask the client for a model completion.
Requests
A request names a method and carries parameters. The id ties the reply to the request, because several requests can be in flight at once. This is the request a client sends to run the add tool from the server-writing chapter:
{"jsonrpc": "2.0", "id": 1, "method": "tools/call", "params": {"name": "add", "arguments": {"a": 2, "b": 3}}}The method tools/call is defined by MCP. The params object holds the tool name and its arguments, which must match the input schema the server published through tools/list.
Responses
A response repeats the request's id and contains either a result or an error, never both. A successful reply to the call above looks like this:
{"jsonrpc": "2.0", "id": 1, "result": {"content": [{"type": "text", "text": "5"}], "isError": false}}The result shape belongs to MCP. Tool output arrives as a list of content items, and here there is one text item.
Notifications
A notification has a method but no id, so nothing replies to it. MCP uses notifications for one-way events. The client sends this one after initialization finishes:
{"jsonrpc": "2.0", "method": "notifications/initialized"}Two kinds of failure
A call can fail at the protocol level or inside the tool, and the two look different.
A protocol error means the request itself was bad. The method doesn't exist, the parameters are malformed, or the server crashed while handling it. The server returns an error object with a numeric code and a message:
{"jsonrpc": "2.0", "id": 2, "error": {"code": -32601, "message": "Method not found"}}Standard codes include -32700 (parse error), -32600 (invalid request), -32601 (method not found), -32602 (invalid params) and -32603 (internal error).
A tool execution error means the request was valid but the tool failed, for example a file wasn't found. The server sends a normal result with "isError": true and an explanation in content. This lets the model read the message and try something else.
Common errors
- Reusing or ignoring ids. If two in-flight requests share an id, replies can't be matched. Each id must be unique within a session.
- Replying to a notification. A notification has no
id, so there is nothing to answer. A reply to it is a protocol violation. - Missing
"jsonrpc": "2.0". Strict parsers reject the message. - Confusing the two error types. An unknown tool name is a protocol error. A tool that runs and fails is a result with
isError: true. - Printing to stdout on a stdio server. On the stdio transport, stdout carries the JSON-RPC stream. Any stray
printcorrupts it. Write logs to stderr instead.
Exercise
A client sends {"jsonrpc": "2.0", "id": 7, "method": "tools/call", "params": {"name": "readFile", "arguments": {"path": "missing.txt"}}}. The server knows the tool, but the file doesn't exist. Which response shape should it send, and what id should it carry?
Show answer
It should send a normal response with "id": 7 and a result containing "isError": true and a text content item explaining that the file was not found. This is a tool execution error, not a protocol error, so it does not use the "error" field. The model can then read the message and adjust.