feat(client): add long-lived multi-server client that reuses sessions…#419
Open
kulikrch wants to merge 2 commits intolangchain-ai:mainfrom
Open
feat(client): add long-lived multi-server client that reuses sessions…#419kulikrch wants to merge 2 commits intolangchain-ai:mainfrom
kulikrch wants to merge 2 commits intolangchain-ai:mainfrom
Conversation
… for tools/prompts/resources - add `LongLivedMultiServerMCPClient` inheriting `MultiServerMCPClient` - support async lifecycle with `start()` / `stop()` and `__aenter__` / `__aexit__` - override `get_tools()` to pass persistent `ClientSession` into `load_mcp_tools` - override `get_prompt()` and `get_resources()` to prefer persistent sessions - export new client from package `__init__` - add tests validating persistent-session reuse paths Refs: langchain-ai#362, langchain-ai#189
- refactor LongLivedMultiServerMCPClient session lifecycle to use AsyncExitStack - ensure partial startup failures close already-opened sessions safely - centralize server validation via _validate_server_name() - add _session_for_server() helper to unify persistent/ephemeral session resolution - keep get_tools() connection-based fallback when no persistent session exists to avoid tools bound to closed ephemeral sessions - update get_prompt() and get_resources() to use shared session-resolution helper - add tests for persistent-session reuse and partial-start cleanup Refs: langchain-ai#362, langchain-ai#189
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds a new client class,
LongLivedMultiServerMCPClient, to enable long-lived MCP sessions and ensure those sessions are actually reused by high-level APIs.Problem
Even when a long-lived session was opened,
get_tools()could still load tools throughload_mcp_tools(session=None, connection=...), which caused tool execution to create fresh sessions instead of using the persistent one.Changes
LongLivedMultiServerMCPClientinlangchain_mcp_adapters/client.py.start()stop()__aenter__()__aexit__()get_tools()to useself.sessions[server_name]when available.get_prompt()to use persistent sessions when available.get_resources()to use persistent sessions when available (single server and all servers modes).LongLivedMultiServerMCPClientvialangchain_mcp_adapters/__init__.py.tests/test_client.pyto verify persistent-session reuse for:get_tools()get_prompt()get_resources()Why this fixes it
When a persistent session exists, all three APIs now call loaders with that active
ClientSessiondirectly. This avoids per-call session creation and preserves server-side state in long-lived workflows.Related issues
Testing
Added targeted unit tests for the new behavior.