Skip to content

Always enter Toolset context when running agent #2361

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 2 commits into from
Jul 30, 2025

Conversation

strawgate
Copy link
Contributor

@strawgate strawgate commented Jul 29, 2025

Always enter the Agent's context in .iter -- this allows us to remove the need for the MCP Toolset to enter its context in get_tools -- which I believe was resulting in startup/shutdown for every get_tools call.

Using async with for MCP is still nice to share an instance of the mcp server across agents runs

Copy link
Contributor

@DouweM DouweM left a comment

Choose a reason for hiding this comment

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

@strawgate Thanks William!

@strawgate
Copy link
Contributor Author

It would be beneficial for my dynamic toolset pr if the agent always entered the toolset context instead of only the first time the context is activated and then we document that the toolset contexts must be reentrant / idempotent as discussed in the #2337

@strawgate strawgate force-pushed the enter_context_in_iter branch 2 times, most recently from 24acc2d to 707d4c3 Compare July 30, 2025 00:23
@strawgate
Copy link
Contributor Author

strawgate commented Jul 30, 2025

I really thought you could pop() context managers off an ExitStack but you can't, so any contexts accumulated into an exitstack can only be exited all at once.

So I think instead of entering the agent in .iter() we should just enter the toolset in .iter() (we've now come full circle 😅).

This results in the same behavior where it matters for this but is sad because entering the Agent's context felt like a pretty clean way to handle it.

Also sorry for the force push...

@strawgate strawgate changed the title Always enter Agent context Always enter Toolset context Jul 30, 2025
@DouweM DouweM changed the title Always enter Toolset context Always enter Toolset context when running agent Jul 30, 2025
@DouweM
Copy link
Contributor

DouweM commented Jul 30, 2025

@strawgate Thanks William!

@DouweM DouweM merged commit 29c72a6 into pydantic:main Jul 30, 2025
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Parallel MCP tool calls cause Runtime Error Tool Manager build() does not enter toolset context
2 participants