Skip to content

[Bug]: Runtime MCP tools bypass Agent.filter_tools_regex #4184

Closed

Description

@s0214lx

Is there an existing issue for the same bug?

  • I have searched existing issues and this is not a duplicate.

Bug Description

Agent.filter_tools_regex is not applied to MCP tools materialized at conversation runtime.

Since MCP tool materialization was moved from AgentBase._initialize() to LocalConversation._ensure_agent_ready(), the current initialization flow is:

  1. AgentBase._initialize() resolves and filters only statically configured Agent.tools.
  2. LocalConversation materializes MCP tools.
  3. The MCP tools are passed directly to AgentBase.add_runtime_tools().
  4. add_runtime_tools() inserts them into _tools without applying filter_tools_regex.

Although Agent.init_state() later calls _initialize() again, _initialize() returns immediately because _initialized is already True.

This is inconsistent with the filter_tools_regex field description, which states that the filter is applied after both explicitly configured tools and MCP tools are added.

This is more than a prompt-size issue: applications using the regex as a sub-agent tool boundary may expose filtered-out MCP tools to the model and allow those tools to be executed.

Expected Behavior

filter_tools_regex should apply consistently to all non-default tools exposed to an Agent, including:

  • Statically configured tools;
  • MCP tools materialized when the conversation starts;
  • MCP tools added by plugins at runtime;
  • Any MCP tools dynamically advertised in the future.

An MCP tool whose name does not match the Agent's filter_tools_regex should:

  • Not be added to tools_map;
  • Not be sent to the LLM;
  • Not be executable through that Agent.

Built-in default tools may continue to follow their existing documented behavior and remain outside this filter.

Actual Behavior

The regex is applied only to tools resolved from Agent.tools during the first call to AgentBase._initialize().

MCP tools are materialized afterward and passed to add_runtime_tools(), which adds every supplied tool directly to _tools without applying the regex.

As a result, MCP tools excluded by filter_tools_regex:

  • Still appear in agent.tools_map;
  • Are sent to the LLM;
  • Can be executed by the Agent.

Steps to Reproduce

  1. Install openhands-sdk==1.36.1.
  2. Create an Agent with filter_tools_regex=r"^allowed$".
  3. Initialize the Agent.
  4. Add two runtime tools named allowed and blocked, reproducing the same add_runtime_tools() path used by LocalConversation for MCP tools.
  5. Inspect agent.tools_map.
  6. Observe that both allowed and blocked are present.

The production MCP path exhibiting the same behavior is:

self.agent._initialize(self._state)
runtime_mcp_tools = self._runtime_mcp_tools_for_agent()
self.agent.add_runtime_tools(runtime_mcp_tools)
self.agent.init_state(...)

The final init_state() call does not reapply filtering because _initialize() immediately returns when _initialized is already True.

Installation Method

Installed from PyPI using uv with openhands-sdk==1.36.1 pinned in pyproject.toml.

If you selected "Other", please specify

No response

SDK Version

1.36.1

Version Confirmation

  • I have confirmed this bug exists on the LATEST version of OpenHands SDK

Python Version

3.12.3

Model Name (if applicable)

Not model-specific. The issue occurs before the LLM request is made.

Operating System

Linux

Logs and Error Messages

There is no exception. The failure is observable in the resulting tool map.

With a filter that only allows allowed, the runtime result is:

Loaded 0 tools from spec
Filtered to 0 tools after applying regex filter
['allowed', 'blocked']

Relevant SDK flow:

AgentBase._initialize()
-> applies filter_tools_regex to static tools
-> sets _initialized = True

LocalConversation._ensure_agent_ready()
-> materializes MCP tools
-> add_runtime_tools(runtime_mcp_tools)
-> init_state()
-> _initialize()
-> returns immediately because _initialized is True

Minimal Code Sample

from unittest.mock import Mock, patch

from openhands.sdk import Agent, LLM
from openhands.sdk.conversation.state import ConversationState
from openhands.sdk.tool import ToolDefinition


llm = LLM(
    model="openai/test",
    api_key="test",
)

agent = Agent(
    llm=llm,
    tools=[],
    include_default_tools=[],
    filter_tools_regex=r"^allowed$",
)

state = Mock(spec=ConversationState)

# Avoid unrelated local vision-profile discovery during this minimal test.
with patch(
    "openhands.sdk.agent.base.has_vision_profile_available",
    return_value=False,
):
    agent._initialize(state)

allowed = Mock(spec=ToolDefinition)
allowed.name = "allowed"

blocked = Mock(spec=ToolDefinition)
blocked.name = "blocked"

# This is the same Agent API used by LocalConversation to inject
# runtime-materialized MCP tools.
agent.add_runtime_tools([allowed, blocked])

print(sorted(agent.tools_map))
assert "allowed" in agent.tools_map
assert "blocked" not in agent.tools_map

Actual output:

['allowed', 'blocked']

The final assertion fails.

Screenshots and Additional Context

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions