python-sdk icon indicating copy to clipboard operation
python-sdk copied to clipboard

Update _client_info on the ClientSession upon initialization with serverInfo

Open mconflitti-pbc opened this issue 1 year ago • 1 comments

Description

Currently, when a ClientSession is created and no client_info arg is passed, it uses the default client info object. My expectation would be that upon session initialization, the clientinfo data is updated with the value returned from the MCP server it connected to. This information is available within the InitializationResult but not used by the SDK so the only way to get that info is to grab it during initialization manually.

Example of grabbing info like the name defined by the MCP server when the client connects:

            transport = await self.exit_stack.enter_async_context(
                streamablehttp_client(server_url, headers=headers)
            )
            self.read, self.write, _ = transport
            self.session = await self.exit_stack.enter_async_context(
                ClientSession(self.read, self.write)
            )

            server = await self.session.initialize()
            self.name = server.serverInfo.name  # <====

ClientSession does the following:

class ClientSession(
    BaseSession[
        types.ClientRequest,
        types.ClientNotification,
        types.ClientResult,
        types.ServerRequest,
        types.ServerNotification,
    ]
):
    def __init__(
        self,
        read_stream: MemoryObjectReceiveStream[SessionMessage | Exception],
        write_stream: MemoryObjectSendStream[SessionMessage],
        # ...
        client_info: types.Implementation | None = None,
    ) -> None:
        super().__init__(...)
        self._client_info = client_info or DEFAULT_CLIENT_INFO # <<<<<<
       # ...

    async def initialize(self) -> types.InitializeResult:
        # ...
        result = await self.send_request(
            types.ClientRequest(
                types.InitializeRequest(
                    method="initialize",
                    params=types.InitializeRequestParams(
                        protocolVersion=types.LATEST_PROTOCOL_VERSION,
                        capabilities=types.ClientCapabilities(
                            sampling=sampling,
                            experimental=None,
                            roots=roots,
                        ),
                        clientInfo=self._client_info,
                    ),
                )
            ),
            types.InitializeResult,
        )
        # ...
        return result

The result is returned but ClientSession._client_info is not updated with the values coming from the result. The simplest thing would just be to set _client_info to serverInfo.

References

No response

mconflitti-pbc avatar Jun 24 '25 17:06 mconflitti-pbc

I'd like to pick this up. Previous PRs:

  • #1565 (open, stale ~4 months, changes requested) — replaced _client_info with server info, which conflates client and server identity
  • #1935, #1954 (both self-closed by author, no maintainer feedback) — stored server_info separately, which is the correct approach

I've submitted #2211 following the same pattern as get_server_capabilities(): store server_info from InitializeResult on the session and expose it via get_server_info(). Includes unit tests.

shivama205 avatar Mar 04 '26 19:03 shivama205