"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(mcp): apply the tool deny-list to every MCP tool
Haozhe Wu45750af8c670
diff --git a/agent/mcp_server.py b/agent/mcp_server.pyindex 64a16f0b1..166ae0680 100644--- a/agent/mcp_server.py+++ b/agent/mcp_server.py@@ -70,6 +70,8 @@ if str(AGENT_DIR) not in sys.path:sys.path.insert(0, str(AGENT_DIR))from fastmcp import Context, FastMCP+from fastmcp.exceptions import ToolError+from fastmcp.server.middleware import Middlewarefrom pydantic import BeforeValidatorfrom cli._version import __version__ as APP_VERSION@@ -86,6 +88,33 @@ mcp = FastMCP("Vibe-Trading", version=APP_VERSION)logger = logging.getLogger(__name__)+class _ToolPolicyMiddleware(Middleware):+ """Apply the tool deny-list to every MCP tool.++ The registry refuses denied names, but many MCP tools call their code+ directly; screening here covers those and keeps denied tools out of the+ advertised list. Like the registry check, a convenience filter, not a+ security boundary.+ """++ async def on_list_tools(self, context: Any, call_next: Any) -> Any:+ from src.providers.tool_policy import _deny_prefixes++ prefixes = _deny_prefixes()+ return [tool for tool in await call_next(context) if not tool.name.startswith(prefixes)]++ async def on_call_tool(self, context: Any, call_next: Any) -> Any:+ from src.providers.tool_policy import _deny_prefixes++ name = context.message.name+ if name.startswith(_deny_prefixes()):+ raise ToolError(f"{name} is disabled by the active tool policy")+ return await call_next(context)+++mcp.add_middleware(_ToolPolicyMiddleware())++# ---------------------------------------------------------------------------# Lazy-loaded singletons# ---------------------------------------------------------------------------diff --git a/agent/tests/test_provider_agnostic_tool_policy.py b/agent/tests/test_provider_agnostic_tool_policy.pyindex b52dd97fb..a6540f8fc 100644--- a/agent/tests/test_provider_agnostic_tool_policy.py+++ b/agent/tests/test_provider_agnostic_tool_policy.py@@ -280,3 +280,33 @@ def test_windows_python_paths_use_cmd_quoting(monkeypatch):command, reason = tool_policy.check_bash_command('python -c "print(1)"')assert reason is Noneassert command.startswith('"C:\\Program Files\\Python\\python.exe" ')+++def test_mcp_tools_that_bypass_the_registry_still_honour_the_deny_list(monkeypatch):+ """`backtest` and `web_search` are MCP tools that call their code directly."""+ import asyncio++ from fastmcp import Client+ from fastmcp.exceptions import ToolError++ import mcp_server++ async def listed() -> set[str]:+ async with Client(mcp_server.mcp) as client:+ return {tool.name for tool in await client.list_tools()}++ unfiltered = asyncio.run(listed())+ assert {"backtest", "web_search"} <= unfiltered++ monkeypatch.setenv("VIBE_TRADING_DENY_TOOL_PREFIXES", "backtest,web_")+ reset_env_config()+ filtered = asyncio.run(listed())+ assert not {name for name in filtered if name.startswith(("backtest", "web_"))}+ assert filtered == {name for name in unfiltered if not name.startswith(("backtest", "web_"))}++ async def call_denied() -> None:+ async with Client(mcp_server.mcp) as client:+ await client.call_tool("backtest", {"run_dir": "unused"})++ with pytest.raises(ToolError, match="disabled by the active tool policy"):+ asyncio.run(call_denied())