feat(client): store InitializeResult on ClientSession, decompose on Client#2300
Draft
feat(client): store InitializeResult on ClientSession, decompose on Client#2300
Conversation
ClientSession now stores the complete InitializeResult via a server_params property, mirroring ServerSession.client_params. This provides access to server_info, capabilities, instructions, and the negotiated protocol_version through a single, future-proof property. Previously, only capabilities were stored (via _server_capabilities) and users had no access to server_info, instructions, or protocol_version after initialize() returned — they had to manually capture the return value. get_server_capabilities() and Client.server_capabilities have been removed. Github-Issue: #1018
…rties - ClientSession.server_params -> initialize_result (avoids collision with StdioServerParameters idiom; matches Go SDK and FastMCP) - Client: replace server_params proxy with non-nullable server_capabilities, server_info, server_instructions (init is guaranteed inside the context manager, so | None was unreachable) - Client.server_capabilities is preserved from v1 with a better type Github-Issue: #1018
915a21c to
3455447
Compare
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.
ClientSessionnow stores the fullInitializeResultvia aninitialize_resultproperty. The high-levelClientexposes it as three decomposed, non-nullable properties.Closes #1018. Supersedes #1565 and #2211.
Motivation and Context
Previously,
ClientSessiondestructured onlyresult.capabilitiesand discardedserver_info,instructions, andprotocol_version— users had to manually capture the return value ofinitialize(), whichClient.__aenter__throws away.ClientSession.initialize_resultStores the whole
InitializeResult, returnsInitializeResult | None. The| Noneis honest here — users who manageClientSessiondirectly can observe the pre-init state.The name matches the type and the method that populates it (
initialize()→initialize_result). Go's SDK calls thisInitializeResult(); FastMCP calls itinitialize_result. The earlier candidate nameserver_paramscollided with the establishedserver_params = StdioServerParameters(...)idiom used throughout README, examples, tests, andClientSessionGroup'sServerParameterstype alias.Client.server_capabilities/server_info/server_instructionsInside
async with Client(...) as client, initialization is guaranteed —__aenter__callsinitialize()before returning. SoClientcaptures the result and exposes it as three non-nullable properties:server_capabilities: ServerCapabilities— same name as v1, now non-nullable instead of| Noneserver_info: Implementation— the server's name/versionserver_instructions: str | None—Nonemeans the server didn't provide any (spec marksinstructionsoptional)Each raises
RuntimeErrorif accessed outside the context manager, matching the existingsessionproperty.The negotiated
protocol_versionis not exposed onClient— it's transport plumbing. Power users can reach it viaclient.session.initialize_result.protocol_version.How Has This Been Tested?
test_initialize_resultassertsNonebefore init and all four fields aftertest_client_is_initializedasserts all threeClientpropertiestest_client_server_properties_before_entercovers theRuntimeErrorpaths./scripts/test— 100% coverage, strict-no-cover cleanpyright— 0 errorsBreaking Changes
ClientSession.get_server_capabilities()removed → usesession.initialize_result.capabilitiesClient.server_capabilitiesreturn type narrowed fromServerCapabilities | NonetoServerCapabilities(raisesRuntimeErroroutside context instead of returningNone)Types of changes
Checklist
AI Disclaimer