Skip to content

Update _client_info on the ClientSession upon initialization with serverInfo #1018

@mconflitti-pbc

Description

@mconflitti-pbc

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3Nice to haves, rare edge casesPR welcomePRs for this issue are welcome and will be reviewed by maintainersenhancementNew feature or requestgood first issueGood for newcomersready for workEnough information for someone to start working on

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions