Sub topic 3
How a session starts: messages and capability negotiation
This chapter assumes you know what hosts, clients, and servers are, and that you can read JSON. Before your server can offer a single tool, the client and server must agree on a protocol version and on which features each side supports. That agreement happens in the first few messages of every session.
Messages are JSON-RPC
MCP sends JSON-RPC 2.0 messages. There are three kinds:
- A request has an
id, amethod, and optionalparams. The receiver must answer it. - A response carries the same
idas its request and holds either aresultor anerror. - A notification has a
methodbut noid. The receiver does not reply.
The id is how a client matches a response to the request that caused it. You rarely handle this yourself, because the SDK does it, but you will see these messages in logs and in the Inspector.
The initialization handshake
Every session begins with three steps, in this order:
- The client sends an
initializerequest. It states the protocol version it wants, its own capabilities, and its name and version. - The server answers with the version it will use, its own capabilities, and its name and version.
- The client sends a
notifications/initializednotification. Normal traffic can start after this.
Here is a trimmed initialize request and its response for the server from the previous chapter. The version string is an example. The one you see depends on the SDK and client versions.
{
"jsonrpc": "2.0",
"id": 1,
"method": "initialize",
"params": {
"protocolVersion": "2025-06-18",
"capabilities": {},
"clientInfo": { "name": "example-host", "version": "1.0.0" }
}
}{
"jsonrpc": "2.0",
"id": 1,
"result": {
"protocolVersion": "2025-06-18",
"capabilities": {
"tools": { "listChanged": false },
"resources": {},
"prompts": {}
},
"serverInfo": { "name": "team-tools", "version": "1.0.0" }
}
}Version negotiation
The client proposes a protocol version. If the server supports it, the server replies with the same version. If not, the server replies with a version it does support. The client then decides whether it can work with that version. If it cannot, it should disconnect. A version mismatch therefore ends the session at startup instead of causing odd failures later.
Capability negotiation
Capabilities are the features each side declares. A server declares tools, resources, or prompts for the features it implements. A client may declare features such as sampling or roots. Each side must only use features the other side declared.
The practical effect is that the client will only call tools/list if the server advertised tools. The FastMCP helper builds this declaration from what you register. Registering a function with @mcp.tool() is what makes the server advertise tools.
Some capabilities have flags. listChanged tells the client that the server will send a notification when its list of tools changes, so the client can fetch the list again.
Common errors
- Sending any request before
initializecompletes. A server may reject it. The SDK prevents this, but a hand-written test client can hit it. - Forgetting the
notifications/initializedmessage in a hand-written client. The server may then ignore later requests. - Printing text to stdout in a stdio server. The client reads stdout as protocol messages, so a stray
printcan break the handshake with a parse error. Send logs to stderr. - Assuming a feature exists without checking. If your client code calls
resources/listagainst a server that did not advertiseresources, expect a method-not-found error.
Exercise
A client connects to your server, and tools/list works. Another client reports "method not found" when it calls prompts/list. Which part of the handshake explains why this can happen, and what would you check first?
Show answer
Capability negotiation. A client should call prompts/list only if the server's initialize result declared prompts. Check the capabilities object in the server's initialize response, for example in the Inspector. If prompts is missing, either the server registers no prompts or the client ignored the declaration.