client: store server_info on ClientSession after initialization#2293
Open
Bortlesboat wants to merge 1 commit intomodelcontextprotocol:mainfrom
Open
client: store server_info on ClientSession after initialization#2293Bortlesboat wants to merge 1 commit intomodelcontextprotocol:mainfrom
Bortlesboat wants to merge 1 commit intomodelcontextprotocol:mainfrom
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When a
ClientSessioncompletes the MCP initialize handshake, the server returnsserverInfo(name, version) insideInitializeResult. This data was returned frominitialize()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 | Noneattribute toClientSession, populated duringinitialize()fromresult.server_info. Expose it via aget_server_info()method, mirroring the existingget_server_capabilities()pattern:Nonebefore initializationImplementation(name + version) after a successful handshakeExample
Changes
src/mcp/client/session.py: added_server_infofield in__init__, assigned ininitialize(), exposed viaget_server_info()tests/client/test_session.py: addedtest_get_server_infoverifying pre-init returnsNoneand post-init returns the correctImplementation