Skip to content

add session-persistent tools context manager #211

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

zndqs
Copy link

@zndqs zndqs commented Jun 16, 2025

Description

This PR introduces a new tools() async context manager method to MultiServerMCPClient, providing persistent session support when working with MCP tools. The implementation:

Adds @asynccontextmanager tools() method that maintains active connections

Supports both single-server and all-servers tool loading patterns

Includes comprehensive documentation with usage examples

Maintains backward compatibility with existing get_tools()

Motivation

Currently, get_tools() creates a new session for each invocation, which can be inefficient for stateful tool usage. The new context manager approach:

Keeps connections alive during tool operations

Enables better resource management

Provides more intuitive session lifecycle control

Matches patterns used elsewhere in the codebase

Usage Examples

# Single server usage
async with client.tools(server_name="math") as tools:
    # Tools remain connected
    result = await tools[0].arun(...)

# All servers usage 
async with client.tools() as all_tools:
    # Work with combined tools from all servers

Related Issues

Closes #207 (if applicable)

@l2leonardo
Copy link

Does this keep the session alive outside the with block?

@zndqs
Copy link
Author

zndqs commented Jun 18, 2025

Does this keep the session alive outside the with block?

no, you can try use xxx.tool().aenter and aexit separatly

@l2leonardo
Copy link

@zndqs I don't think this actually fix the issue. Session is not persistent on the agent as a whole, only if you hit a Tool Calling node and have a stream of tools already prepared for calling. I think the current version of adapters already allows you to do something like this if you add a with block in your tool calling node...

Copy link
Collaborator

@eyurtsev eyurtsev left a comment

Choose a reason for hiding this comment

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

stateful connections in MCP do not only involve tools but can invole resources etc.

A context manager that opens a stateful connection to a server makes sense, but it shouldn't be limited to only tools.

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.

Question: Why start a new session on each tool call in MultiServerMCPClient? (Causes issues with stateful MCP servers in stdio mode)
3 participants