Sub topic 4
How messages and capabilities work
This chapter assumes you know basic JSON, can read Python, and have seen LLM tool calling. It explains the message format MCP uses on the wire and how a client and server agree on what each side supports.
JSON-RPC as the message format
MCP messages are JSON-RPC 2.0. There are three kinds:
- A request has an
id, amethod, and optionalparams. The receiver must answer it. - A response carries the same
idas the request, with either aresultor anerror. - A notification has a
methodbut noid. The receiver sends no reply.
Here is a request that calls a tool, followed by its response:
{"jsonrpc": "2.0", "id": 7, "method": "tools/call",
"params": {"name": "add", "arguments": {"a": 2, "b": 3}}}{"jsonrpc": "2.0", "id": 7,
"result": {"content": [{"type": "text", "text": "5"}], "isError": false}}The id lets many requests be in flight at once. You match each answer to its question by id, not by arrival order. The SDK does this for you, so await session.call_tool(...) simply waits for the response with the right id.
Two kinds of failure
JSON-RPC errors and tool errors are different, and mixing them up is a common mistake.
| Kind | Where it appears | Typical cause |
|---|---|---|
| Protocol error | The `error` field of the response | Unknown method, malformed params, unknown tool name |
| Tool execution error | A normal `result` with `isError: true` | The tool ran and failed, for example a bad file path |
A tool execution error is data for the model. Pass its text back as a tool_result so the model can retry or explain. A protocol error usually means your code has a bug.
Capabilities
Both sides announce what they support during initialization. The client sends its capabilities in the initialize request. The server returns its own in the response. A server that exposes tools declares a tools capability. Others may declare resources, prompts, or logging.
{"capabilities": {"tools": {"listChanged": true}, "resources": {}}}Here the server says it has tools and resources. listChanged: true means it will send a notifications/tools/list_changed notification when its tool list changes. Without that flag, do not expect such notifications.
Capabilities decide which methods you may call. Call tools/list only if the server declared tools. Calling an undeclared feature typically returns a "method not found" protocol error.
The client declares capabilities too. For example, a client that supports sampling lets the server ask the host's model for a completion. If you do not declare a capability, the server must not use it.
Reading what the server declared
After session.initialize(), the SDK returns the server's declared capabilities:
init = await session.initialize()
caps = init.capabilities
print("server:", init.serverInfo.name)
print("tools:", caps.tools is not None)
print("resources:", caps.resources is not None)
print("prompts:", caps.prompts is not None)Check these before calling list_tools(), list_resources(), or list_prompts(). This keeps one host working with servers that offer only some features.
Exercise
A server replies to your tools/call with a normal result containing "isError": true. Is this a JSON-RPC error? What should your agent loop do with it?
Show answer
No. A JSON-RPC error appears in the response's error field. This is a tool execution error inside a normal result. The loop should send its text back to the model as a tool_result (with is_error set) so the model can recover or explain the failure.
Exercise
Why should you check caps.prompts before calling list_prompts()?
Show answer
The server may not have declared the prompts capability. Calling an undeclared feature typically gives a "method not found" protocol error, so checking first lets your host work with servers that offer only some features.