Skip to content

client: store server_info on ClientSession after initialization#2293

Open
Bortlesboat wants to merge 1 commit intomodelcontextprotocol:mainfrom
Bortlesboat:fix/store-server-info-on-client-session
Open

client: store server_info on ClientSession after initialization#2293
Bortlesboat wants to merge 1 commit intomodelcontextprotocol:mainfrom
Bortlesboat:fix/store-server-info-on-client-session

Conversation

@Bortlesboat
Copy link

Problem

When a ClientSession completes the MCP initialize handshake, the server returns serverInfo (name, version) inside InitializeResult. This data was returned from initialize() but not retained on the session object — callers had no way to inspect which server they were connected to without capturing it at call time.

Fixes #1018

Solution

Add a _server_info: types.Implementation | None attribute to ClientSession, populated during initialize() from result.server_info. Expose it via a get_server_info() method, mirroring the existing get_server_capabilities() pattern:

  • Returns None before initialization
  • Returns the Implementation (name + version) after a successful handshake

Example

session = await exit_stack.enter_async_context(ClientSession(read, write))
await session.initialize()

server_info = session.get_server_info()
print(server_info.name)     # e.g. "my-mcp-server"
print(server_info.version)  # e.g. "1.0.0"

Changes

  • src/mcp/client/session.py: added _server_info field in __init__, assigned in initialize(), exposed via get_server_info()
  • tests/client/test_session.py: added test_get_server_info verifying pre-init returns None and post-init returns the correct Implementation

After the initialize handshake, the InitializeResult contains serverInfo
describing the connected server. Previously this data was returned from
initialize() but not retained on the session, requiring callers to store
it themselves.

Add _server_info attribute set during initialize() and expose it via
get_server_info(), mirroring the existing get_server_capabilities() pattern.
Returns None before initialization, the Implementation object after.

Github-Issue: modelcontextprotocol#1018
Reported-by: mconflitti-pbc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Update _client_info on the ClientSession upon initialization with serverInfo

1 participant