Skip to content

Create ClientSessionGroup for managing multiple session connections. #639

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 13, 2025

Conversation

mkeid
Copy link
Contributor

@mkeid mkeid commented May 6, 2025

This abstraction concurrently manages multiple MCP session connections.

Tools, resources, and prompts are aggregated across servers. Servers may be connected to or disconnected from at any point after initialization.

This abstractions can handle naming collisions using a custom user-provided hook.

Motivation and Context

This change alleviates the need for users to re-implement a multi-session "manager" anytime more than 1 session needs to be established.

Testing

Code branches are covered via pytest unit tests.

Breaking Changes

This is a no-op as it introduces a new class without modifying existing dependencies.

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

mkeid added 2 commits May 6, 2025 16:05
This abstraction concurrently manages multiple MCP session connections.
Tools, resources, and prompts are aggregated across servers. Servers may
be connected to or disconnected from at any point after initialization.

This abstractions can handle naming collisions using a custom
user-provided hook.
@ihrpr ihrpr added this to the 2025-03-26 spec release milestone May 7, 2025
Copy link
Contributor

@ihrpr ihrpr left a comment

Choose a reason for hiding this comment

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

Thank you for working on this!

We need to make sure that this ClientSessionGroup/Host abstraction supports all current transports in SDK

mkeid added 2 commits May 9, 2025 01:23
…onGroup

This change introduces additional support for the other MCP transports
and includes respective server parameter types to align with the stdio
client implementation.
@mkeid mkeid requested a review from ihrpr May 9, 2025 15:36
Copy link
Contributor

@ihrpr ihrpr left a comment

Choose a reason for hiding this comment

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

thank you so much for the quick turnaround!

there a few things we need to add:

  • ClientSessionGroup to implement __aenter__/__aexit__
  • Ability to use async with ClientSessionGroup():
  • disconnect_from_server removes sessions from tracking but doesn't remove them from _exit_stack
  • add documentation on how to use

@mkeid mkeid force-pushed the client-session-group branch 3 times, most recently from 215d0bc to fcbdd79 Compare May 10, 2025 18:51
@mkeid mkeid requested a review from ihrpr May 10, 2025 18:52
@mkeid mkeid force-pushed the client-session-group branch 3 times, most recently from cca631c to d4a28ca Compare May 12, 2025 18:13
Copy link
Contributor

@ihrpr ihrpr left a comment

Choose a reason for hiding this comment

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

Some small changes and we are good to merge! Thank you

@mkeid mkeid force-pushed the client-session-group branch from d4a28ca to a42f953 Compare May 12, 2025 23:42
This changes enables context management for setting up and tearing down
async exit stacks durring server connection and disconnection respectively.

Documentation has been added to show an example use case that demonstrates
how `ClientSessionGroup` can be used with `async with`.
@mkeid mkeid force-pushed the client-session-group branch from 595e777 to e798eeb Compare May 13, 2025 01:20
the client and can be accessed via the session.
Example Usage:
name_fn = lambda name, server_info: f"{(server_info.name)}-{name}"
Copy link

Choose a reason for hiding this comment

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

Is it possible that both a tool name and a server_info name would conflict? would there be enough information for the hook to disambiguate?

@mkeid mkeid requested a review from ihrpr May 13, 2025 04:55
@ihrpr
Copy link
Contributor

ihrpr commented May 13, 2025

related to #9

Copy link
Contributor

@ihrpr ihrpr left a comment

Choose a reason for hiding this comment

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

Thank you!

@ihrpr ihrpr merged commit 7b6a903 into modelcontextprotocol:main May 13, 2025
6 checks passed
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