fix(gateway): refresh cached agent tools on /reload-mcp#32815
Merged
Conversation
When the gateway processes /reload-mcp, it reconnects MCP servers and updates the global _servers registry, but cached AIAgent instances in _agent_cache keep the tools list they were built with. The user had to also run /new (discarding conversation history) before the agent could see the new tools — even though /reload-mcp had succeeded. This patch refreshes each cached agent's .tools and .valid_tool_names in _execute_mcp_reload after discovery returns, so existing sessions pick up new MCP tools on their next turn. The slash-confirm gate in _handle_reload_mcp_command already obtains user consent for the implied prompt-cache invalidation before this code runs. Mirrors the equivalent behaviour the CLI already does in cli.py _reload_mcp. Per-agent enabled_toolsets and disabled_toolsets are preserved so an agent that was scoped to a subset of toolsets does not silently gain disabled tools after the reload. Original diagnosis + initial implementation in #23812 from @fujinice. The auto-reload watcher half of that PR is intentionally dropped — users want /reload-mcp to remain explicit. Co-authored-by: fujinice <45688690+fujinice@users.noreply.github.com>
Contributor
🔎 Lint report:
|
| Rule | Count |
|---|---|
invalid-argument-type |
1 |
unresolved-import |
1 |
First entries
gateway/run.py:13363: [invalid-argument-type] invalid-argument-type: Argument to function `get_tool_definitions` is incorrect: Expected `list[str]`, found `Any | None`
tests/gateway/test_mcp_reload_refreshes_cached_agents.py:20: [unresolved-import] unresolved-import: Cannot resolve imported module `pytest`
✅ Fixed issues: none
Unchanged: 4967 pre-existing issues carried over.
Diagnostics are surfaced as warnings — this check never fails the build.
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
/reload-mcpnow updates the running session's tool list — no/newrequired.Previously the gateway reconnected MCP servers but kept the stale tool list on every cached AIAgent. Users had to also start a new session to actually call the newly-available tools, losing their conversation history.
Changes
gateway/run.py::_execute_mcp_reload— afterdiscover_mcp_tools()returns, walk_agent_cacheand overwrite each agent's.tools+.valid_tool_namesviaget_tool_definitions(). Per-agentenabled_toolsets/disabled_toolsetsare preserved.tests/gateway/test_mcp_reload_refreshes_cached_agents.py— 3 new tests covering: cached agents are refreshed, empty cache is a no-op, and per-agent toolset scopes survive the reload.scripts/release.py— AUTHOR_MAP entry for @fujinice.What did NOT change
The slash-confirm gate (
Approve Once/Always Approve/Cancel) stays exactly as it was —/reload-mcpstill prompts before invalidating the prompt cache. The cache-refresh is just the missing piece of what "yes" already meant.The auto-reload-on-config-change watcher from #23812 is intentionally not included —
/reload-mcpremains explicit.Validation
/new)/reload-mcptests/gateway/(5923 tests)Credit
Original diagnosis + initial cache-refresh implementation by @fujinice in #23812 (Co-Authored-By preserved). Closes #23812.
Infographic