Repository navigation
fix(agent): apply filter_tools_regex to runtime tools - #4186
Merged
VascoSch92 merged 2 commits intoJul 22, 2026
Merged
VascoSch92 merged 2 commits into
VascoSch92 merged 2 commits into
Conversation
Since OpenHands#3964 moved MCP tool materialization from AgentBase._initialize() into LocalConversation._ensure_agent_ready(), runtime MCP tools are registered via add_runtime_tools(), which skipped filter_tools_regex. Tools excluded by the regex were still sent to the LLM and executable. Apply the regex in add_runtime_tools(), keeping built-in default tools exempt to match _initialize(). Fixes OpenHands#4184
VascoSch92
commented
Jul 22, 2026
VascoSch92
commented
Jul 22, 2026
VascoSch92
commented
Jul 22, 2026
… tests Address review: drop the kept/dropped bookkeeping in add_runtime_tools and log before/after counts like _initialize(); remove the issue link from the test docstring; add edge-case tests for mixed batches, all-filtered batches, prefix-match anchoring, built-in subclasses, and duplicate handling around the filter.
VascoSch92
marked this pull request as ready for review
July 22, 2026 08:23
5 tasks
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.
HUMAN:
Fixing the filter_tools_regext for runtime tools.
AGENT:
Why
Agent.filter_tools_regexis documented as being "applied after any tools provided intoolsand any MCP tools are added", but runtime-materialized MCP tools bypass it entirely. Tools excluded by the regex still land inagent.tools_map, are advertised to the LLM (agent.py,tools=list(self.tools_map.values())), and are executable — so applications using the regex as a tool boundary silently expose filtered-out MCP tools.This is a regression: until #3964,
AgentBase._initialize()materialized MCP tools itself and applied the regex afterwards (seecreate_mcp_toolsfollowed by thefilter_tools_regexblock atfe924f705^). #3964 moved materialization intoLocalConversation._ensure_agent_ready(), which registers the tools viaadd_runtime_tools()— a method introduced in #3826 that never applied the filter. The subsequentinit_state()call does not re-filter because_initialize()returns immediately once_initializedis set.Summary
filter_tools_regexinAgentBase.add_runtime_tools(), so the single choke point covers both runtime-tool paths (conversation-start MCP tools and plugin-added tools) as well as any future dynamic-tool source.InvokeSkillTool, which the plugin path routes through the same method) are exempt, matching the existing exemption in_initialize().filter_tools_regexpreviously had zero test coverage anywhere in the repo, which is how this slipped through.Issue Number
Fixes #4184
How to Test
End-to-end through the real production path (not just unit tests): build a
LocalConversationwhose injectedmcp_tool_provideradvertises tools namedallowedandblocked, with the agent configured withfilter_tools_regex=r"^allowed$", then trigger_ensure_agent_ready()and inspecttools_map.Before this fix (at
cae92b879):After this fix:
(
inspect_image_with_visionis an auto-attached built-in default tool, documented as exempt from the filter.)That scenario is captured as
test_runtime_mcp_tools_apply_filter_tools_regexin the new test file, so reviewers can reproduce with:Verification performed:
uv run pytest tests/sdk/agent/test_filter_tools_regex.py -q→ 12 passed. Withbase.pyreverted toorigin/main, the 6 regression tests fail and the 6 behavior-preservation tests still pass — confirming the tests catch the bug and guard existing behavior.uv run pytest tests/sdk -q(single process) → 5443 passed, 9 skipped, 13 xfailed; matches the pre-change baseline plus the 12 new tests.uv run pre-commit run --files openhands-sdk/openhands/sdk/agent/base.py tests/sdk/agent/test_filter_tools_regex.py→ all hooks pass (ruff format/lint, pycodestyle, pyright, import rules, tool registration).Video/Screenshots
Terminal logs above (before/after output of the end-to-end reproduction and test runs).
Type
Notes
isinstanceagainstBUILT_IN_TOOL_CLASSES, mirroring_initialize()'s exemption; without it, a restrictive regex would silently dropInvokeSkillToolinjected by the plugin path and break skill invocation.pattern.match(prefix semantics) is deliberately kept consistent with_initialize()._initialize()also discarded filtered MCP tools without cleanup. If a server's tools are all filtered its client connection lingers; flagging as a possible follow-up rather than expanding this fix.mcp_configpath); this fix covers both.