Sub topic 4
Starting a session: initialization and capability negotiation
This chapter assumes you can read JSON, know the host, client, and server roles from the earlier chapters, and understand the JSON-RPC request, response, and notification shapes. Here you will see how an MCP connection begins and how both sides agree on what they can do.
Why a handshake is needed
An MCP server and client are written by different people at different times. They may support different protocol versions and different features. Before either side sends a real request, they must agree on a protocol version and learn what the other side supports. This happens in the initialization phase, and nothing else may come first, apart from simple pings.
The three-step handshake
Initialization has three steps.
- The client sends an
initializerequest. - The server replies with its own version, capabilities, and identity.
- The client sends a
notifications/initializednotification to say it is ready.
The initialize request
The client opens the session with a request like this one.
{"jsonrpc": "2.0", "id": 1, "method": "initialize", "params": {"protocolVersion": "2025-06-18", "capabilities": {"sampling": {}}, "clientInfo": {"name": "example-client", "version": "1.0.0"}}}It has three parts:
protocolVersionis the newest protocol version the client supports.capabilitieslists the optional features the client offers. Here the client offerssampling, which lets the server ask the client to run a model call.clientInfogives the client's name and version, which helps with logs and debugging.
Version negotiation
The server looks at the requested version. If it supports that version, it answers with the same one. If it does not, it answers with another version it does support, normally its newest. The client then checks the reply. If it cannot work with that version, it should disconnect.
Capability negotiation
The server's result lists what it offers. For example, it might declare tools, resources, and prompts, plus serverInfo with its name and version.
Capabilities can carry sub-flags. A server that declares "tools": {"listChanged": true} promises to send a notification when its tool list changes. A client should use only the features that both sides declared. If a server did not declare resources, the client must not send resources/list.
| Side | Example capabilities | Meaning |
|---|---|---|
| Client | `sampling`, `roots`, `elicitation` | Features the server may ask the client to perform |
| Server | `tools`, `resources`, `prompts`, `logging` | Features the client may request from the server |
The initialized notification
After reading the server's reply, the client sends a notification. It has no id because it expects no response.
{"jsonrpc": "2.0", "method": "notifications/initialized"}Only after this does normal operation begin. A client must not send tool calls before it, and a server should not send requests other than pings or logging before it receives it.
Seeing it in code
The Python SDK performs the whole handshake in one call.
import asyncio
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
async def main():
params = StdioServerParameters(command="python", args=["server.py"])
async with stdio_client(params) as (read, write):
async with ClientSession(read, write) as session:
init = await session.initialize()
print(init.protocolVersion)
print(init.serverInfo.name)
print(init.capabilities)
asyncio.run(main())session.initialize() sends the request, receives the result, and sends the initialized notification. The returned object shows what the server declared. This assumes a server.py like the one built later in this tutorial.
Common errors
- Calling
list_tools()orcall_tool()beforeinitialize(). The server will reject the request. - Assuming a feature exists without checking the declared capabilities.
- Hard-coding a protocol version and ignoring the version in the server's reply.
- Forgetting the initialized notification when writing a raw client. The server may then ignore later requests.
Exercise
A server's initialize result does not include a prompts capability. What should the client do about prompts, and why?
Show answer
The client should not send prompts/list or prompts/get. Capabilities declared during initialization define what each side may use in the session, so the client should rely only on features the server declared.
Exercise
Why does the notifications/initialized message have no id?
Show answer
It is a JSON-RPC notification. Notifications expect no response, so they carry no id.