Sub topic 3
The wire format: JSON-RPC requests, responses, and notifications
This chapter assumes you can read JSON and know the host, client, and server roles from the previous chapter. It covers the three message shapes every MCP exchange uses.
Why JSON-RPC
MCP does not invent its own message format. It uses JSON-RPC 2.0, a small specification for calling named methods with JSON. Every MCP message is a JSON object with "jsonrpc": "2.0", and it is one of three kinds: a request, a response, or a notification.
Either side can send requests. A client calls tools/call on a server, and a server can call sampling/createMessage on a client. The protocol is symmetric at the message level, even though the roles are not.
Requests
A request asks the other side to do something and expects an answer. It has an id, a method, and usually params.
{"jsonrpc": "2.0", "id": 2, "method": "tools/call", "params": {"name": "add", "arguments": {"a": 2, "b": 3}}}MCP adds two rules to plain JSON-RPC. The id must be a string or a number, never null. The sender must also not reuse an id within the same session.
Responses
A response carries the same id as the request it answers. That is how a sender matches replies to requests when several are in flight. A response holds either a result or an error, never both.
{"jsonrpc": "2.0", "id": 2, "result": {"content": [{"type": "text", "text": "5"}], "isError": false}}Notifications
A notification has a method but no id. The receiver must not reply, so the sender never learns whether it was handled. MCP uses notifications for one-way facts, such as the client finishing setup:
{"jsonrpc": "2.0", "method": "notifications/initialized"}Two kinds of failure
An error response reports a protocol-level problem. It has an integer code and a message.
{"jsonrpc": "2.0", "id": 3, "error": {"code": -32601, "message": "Method not found"}}The standard codes are:
| Code | Meaning |
|---|---|
| -32700 | Parse error: the text was not valid JSON |
| -32600 | Invalid request: the JSON was not a valid message |
| -32601 | Method not found |
| -32602 | Invalid params |
| -32603 | Internal error |
A tool that runs but fails is different. For example, an API the tool called may return a 500. That is a successful JSON-RPC exchange, so the server returns a normal result with "isError": true and puts the explanation in content. This lets the model read the failure and react to it.
Framing on the wire
JSON-RPC defines message content, not how messages are separated in a byte stream. Each transport handles that. On stdio, messages are UTF-8 JSON separated by newlines, so a message must not contain a raw newline. The next chapters cover transports in detail.
The 2025-06-18 revision of MCP does not support JSON-RPC batching, where several messages are sent as one array. Send each message on its own.
Common mistakes
- Adding an
idto a notification. The receiver then treats it as a request and may wait to answer it. - Replying to a request with a different
id, which leaves the sender waiting forever. - Returning both
resultanderrorin one response. - Writing log lines to stdout in a stdio server. They corrupt the message stream. Log to stderr instead.
Exercise
A client sends {"jsonrpc": "2.0", "id": 7, "method": "tools/call", "params": {"name": "fetch", "arguments": {"url": "http://example.invalid"}}}. The fetch tool runs, but the network call fails. Should the server reply with an "error" object or a "result"? Give the key fields of the reply.
Show answer
It should reply with a "result". The request was valid and the tool ran, so the failure belongs to the tool. The reply is {"jsonrpc": "2.0", "id": 7, "result": {"content": [{"type": "text", "text": "Network request failed"}], "isError": true}}. It must reuse id 7. An "error" object with a code such as -32602 is for protocol problems, such as a missing "name" parameter.