Sub topic 6
Initialization and the connection lifecycle
This chapter assumes you can read async Python, know basic JSON-RPC ideas (requests, responses, notifications), and have seen the host, client and server roles from earlier chapters. It covers what happens between starting a connection and making the first tool call, and what happens when the connection ends.
Three phases of a connection
Every MCP connection moves through the same phases:
- Initialization. The client and server agree on a protocol version and exchange capabilities.
- Operation. Both sides send requests and notifications, such as
tools/listandtools/call. - Shutdown. The transport closes. MCP defines no special shutdown message. For stdio, the client closes the server's input stream and waits for it to exit. For HTTP, the client closes the connection.
The initialize handshake
The client always speaks first. Its initialize request carries three things:
- The protocol version it prefers.
- Its capabilities, which are the optional features it supports.
- Its name and version.
The server replies with a protocol version it will use, its own capabilities and its own name and version. If the server cannot support the client's version, it answers with a different version it does support. The client then decides whether to continue or disconnect.
After the reply, the client sends a notifications/initialized notification. A notification has no id and gets no response. It tells the server that normal operation can begin.
What the SDK does for you
In the Python SDK, session.initialize() performs all three steps. You saw it in the client example:
import asyncio
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
server_params = StdioServerParameters(command="python", args=["server.py"])
async def main():
async with stdio_client(server_params) as (read, write):
async with ClientSession(read, write) as session:
init = await session.initialize()
print(init.serverInfo.name, init.protocolVersion)
print("tools supported:", init.capabilities.tools is not None)
asyncio.run(main())The return value of initialize() is the server's reply. Two parts are worth keeping in your host:
capabilitiestells you which primitives the server offers. Ifcapabilities.toolsisNone, callinglist_tools()is an error.protocolVersiontells you which version was negotiated.
Lifecycle through context managers
The two async with blocks tie the connection to your code's scope.
stdio_clientstarts the server as a subprocess. When the block exits, it closes the pipes and ends the process.ClientSessionruns a background reader that matches responses to requests.
A session must be initialized before any other request. Forgetting await session.initialize() is the most common mistake. The server rejects or ignores requests it receives before the handshake finishes, and the failure often looks like a hang or a protocol error instead of a clear message.
Common errors
| Symptom | Likely cause |
|---|---|
| Call hangs after connecting | `initialize()` was never awaited, or the server printed non-protocol text to stdout |
| Error about an unsupported method | The server did not declare that capability |
| Session closes at once | The server process crashed on startup, so check its stderr |
| Works once, fails later | The session was used after the context manager exited |
For stdio servers, remember that stdout carries protocol messages. Log to stderr only.
Exercise
A client connects to a server and calls list_tools() without calling initialize() first. Why is this wrong, and what does initialize() return that helps you decide whether list_tools() is safe to call?
Show answer
The protocol requires the initialization handshake before other requests, so the server may reject or ignore the call. initialize() returns the server's capabilities. If capabilities.tools is None, the server does not offer tools and list_tools() should not be called.