Skip to content

Expose streamablehttp_client session id callback to users #2485

@mathewjhan

Description

@mathewjhan

Enhancement

Currently, the StreamableHTTPTransport doesn't expose the session id callback from MCP's streamablehttp_client.

        async with streamablehttp_client(
            self.url,
            auth=self.auth,
            **client_kwargs,
        ) as transport:
            read_stream, write_stream, _ = transport
            async with ClientSession(
                read_stream, write_stream, **session_kwargs
            ) as session:
                yield session

For users wanting to manage some state on the client side related to sessions, it is more complicated to access mcp-session-id without working with lower level code.

Proposal:

        async with streamablehttp_client(
            self.url,
            auth=self.auth,
            **client_kwargs,
        ) as transport:
            # _ is replaced with get_session_id and attached to the transport
            read_stream, write_stream, get_session_id = transport
            self.get_session_id_cb = get_session_id
            
            async with ClientSession(
                read_stream, write_stream, **session_kwargs
            ) as session:
                yield session

    def get_session_id(self) -> str | None:
        if self.get_session_id_cb:
            return self.get_session_id_cb()
        return None

Metadata

Metadata

Assignees

No one assigned

    Labels

    clientRelated to the FastMCP client SDK or client-side functionality.enhancementImprovement to existing functionality. For issues and smaller PR improvements.httpRelated to HTTP transport, networking, or web server functionality.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions