Skip to content

Conversation

@crondinini-ant
Copy link
Contributor

Motivation and Context

Currently, there's no way to retrieve the session ID from the SSE client. The session ID is embedded in the
endpoint URL sent by the server, but it's not exposed to callers. Users who need the session ID (e.g., for
reconnection with mcp-session-id header) would have to resort to intercepting raw HTTP bytes via custom transports.

This change adds a simple callback mechanism that's invoked when the endpoint event is received, passing the
extracted session ID.

How Has This Been Tested?

  • Added unit test test_sse_client_on_session_created that verifies the callback is invoked with a valid session
    ID

Breaking Changes

None. The new on_session_created parameter is optional and defaults to None.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

Usage example:

session_id = None

def capture_session(sid: str):
    nonlocal session_id
    session_id = sid

async with sse_client(url, on_session_created=capture_session) as (r, w):
    print(session_id)  # available after connection established

@crondinini-ant crondinini-ant marked this pull request as ready for review December 1, 2025 15:02
@felixweinberger
Copy link
Contributor

felixweinberger commented Dec 1, 2025

What do you think about this alternative approach? #1608

Seems potentially more intuitive to have this available on the request context than to pass it back via a callback somehow?

Edit: not relevant, as this is on client, that makes it available on server tools

Copy link
Contributor

@felixweinberger felixweinberger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discussed alternative approaches offline and this tbh seems like the cleanest way to achieve this.

We discussed adding it to the return tuple (read, write, session_id) to mirror streamable_http, but that would be a breaking change here because of the tuple now returning a tuple of 3 instead of 2.

Given SSE is deprecated anyway and we're adding something here for backwards compatibility, I'm fine with taking on this minor debt to unblock the use case.

@felixweinberger felixweinberger merged commit 2cd178a into main Dec 1, 2025
21 checks passed
@felixweinberger felixweinberger deleted the crondinini/allow-sse-session-id-to-be-retrieved branch December 1, 2025 17:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants