Skip to content

fix(agent): apply filter_tools_regex to runtime tools - #4186

Merged
VascoSch92 merged 2 commits into
OpenHands:mainfrom
VascoSch92:vasco/fix-filter-tools-regex-runtime-tools
Jul 22, 2026
Merged

VascoSch92 merged 2 commits into
OpenHands:mainfrom
VascoSch92:vasco/fix-filter-tools-regex-runtime-tools

Conversation

@VascoSch92

@VascoSch92 VascoSch92 commented Jul 22, 2026 •

Copy link
Copy Markdown
Member

HUMAN:

Fixing the filter_tools_regext for runtime tools.


AGENT:

Why

Agent.filter_tools_regex is documented as being "applied after any tools provided in tools and any MCP tools are added", but runtime-materialized MCP tools bypass it entirely. Tools excluded by the regex still land in agent.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 (see create_mcp_tools followed by the filter_tools_regex block at fe924f705^). #3964 moved materialization into LocalConversation._ensure_agent_ready(), which registers the tools via add_runtime_tools() — a method introduced in #3826 that never applied the filter. The subsequent init_state() call does not re-filter because _initialize() returns immediately once _initialized is set.

Summary

  • Apply filter_tools_regex in AgentBase.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.
  • Built-in default tools (e.g. InvokeSkillTool, which the plugin path routes through the same method) are exempt, matching the existing exemption in _initialize().
  • Add regression tests (12) covering both paths plus edge cases — mixed batches, all-filtered batches, prefix-match anchoring, built-in subclasses, and duplicate handling around the filter; filter_tools_regex previously 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 LocalConversation whose injected mcp_tool_provider advertises tools named allowed and blocked, with the agent configured with filter_tools_regex=r"^allowed$", then trigger _ensure_agent_ready() and inspect tools_map.

Before this fix (at cae92b879):

filter_tools_regex = '^allowed$'
agent.tools_map    = ['allowed', 'blocked', 'inspect_image_with_vision']
AssertionError: BUG REPRODUCED: 'blocked' does not match ^allowed$ but is exposed to the agent/LLM

After this fix:

INFO  Filtered runtime tools from 2 to 1 after applying regex filter
filter_tools_regex = '^allowed$'
agent.tools_map    = ['allowed', 'inspect_image_with_vision']
OK: filter applied to runtime MCP tools

(inspect_image_with_vision is 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_regex in the new test file, so reviewers can reproduce with:

uv run pytest tests/sdk/agent/test_filter_tools_regex.py -q

Verification performed:

  • uv run pytest tests/sdk/agent/test_filter_tools_regex.py -q → 12 passed. With base.py reverted to origin/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

  • Bug fix
  • Feature
  • Refactor
  • Breaking change
  • Docs / chore

Notes

  • Filtering exempts built-ins via isinstance against BUILT_IN_TOOL_CLASSES, mirroring _initialize()'s exemption; without it, a restrictive regex would silently drop InvokeSkillTool injected by the plugin path and break skill invocation.
  • pattern.match (prefix semantics) is deliberately kept consistent with _initialize().
  • Filtered-out MCP tools are dropped without closing their executors — identical to the pre-feat(mcp): add settings-backed MCP auth credentials #3964 behavior, where _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.
  • The plugin path has bypassed the filter since feat(marketplace): support runtime local plugin loading #3826 (two weeks before feat(mcp): add settings-backed MCP auth credentials #3964 regressed the main mcp_config path); this fix covers both.

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
… 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
VascoSch92 marked this pull request as ready for review July 22, 2026 08:23

@neubig neubig left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks!

@VascoSch92
VascoSch92 merged commit eed2d63 into OpenHands:main Jul 22, 2026
34 of 36 checks passed
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.

[Bug]: Runtime MCP tools bypass Agent.filter_tools_regex

2 participants