feat: ClientConnectionManager to manage multiple sessions without async with#1133
feat: ClientConnectionManager to manage multiple sessions without async with#1133hanhan1123 wants to merge 2 commits intomodelcontextprotocol:mainfrom
Conversation
…ut requiring async with
|
Thank you for your contribution. The issue mentioned is related to ClientSessionGroup. If there is a specific concern around it, we need to address it in ClientSessionGroup. Adding an additional, very similar abstraction creates API fragmentation and maintenance burden. |
|
@ihrpr Regarding ClientSessionGroup, I’ve evaluated it before proposing this PR. However, it doesn’t fundamentally resolve the session management problem. ClientSessionGroup still relies on the async with lifecycle, which makes it tricky when you need sessions to persist across a wider application scope. More importantly, it can still hit this error: And with the current architecture, there’s really no clean way to solve that, because of how async context managers and cancellation scopes work across tasks. That’s why I explored a different approach in this PR. I’m happy to further discuss how this could evolve to better align with the project’s direction while effectively addressing these session lifecycle constraints. |
|
nice, this pr is what i need. |
This PR introduces a
ClientConnectionManagerthat allows users to manage multiple sessions without needing to explicitly useasync with. This provides more flexibility in long-running or dynamic session use cases.This PR also resolves #922 by allowing users to manage multiple sessions without async with
Motivation and Context
In developing AI agents, the MCP client is often just one part of a larger system. However, the requirement to use
async withwhen initiating sessions makes it difficult to integrate MCP into more complex workflows, especially when other asynchronous components are involved.To address this, I implemented a
ClientConnectionManagerthat completely removes the need forasync withduring usage. This provides much greater flexibility in designing and structuring other components, particularly in long-running or modular systems.How Has This Been Tested?
Code branches are covered via pytest unit tests.
Breaking Changes
No
Types of changes
Checklist
Additional context