Bug fixes: - agent/redact.py: catastrophic regex backtracking in _ENV_ASSIGN_RE — removed re.IGNORECASE and changed [A-Z_]* to [A-Z0-9_]* to restrict matching to actual env var name chars. Without this, the pattern backtracks exponentially on large strings (e.g. 100K tool output), causing test_file_read_guards to time out. - tools/file_operations.py: over-escaped newline in find -printf format string produced literal backslash-n instead of a real newline, breaking file search result parsing (total_count always 1, paths concatenated). Test fixes: - Remove stale pytestmark.skip from 4 test modules that were blanket-skipped as 'Hangs in non-interactive environments' but actually run fine: - test_413_compression.py (12 tests, 25s) - test_file_tools_live.py (71 tests, 24s) - test_code_execution.py (61 tests, 99s) - test_agent_loop_tool_calling.py (has proper OPENROUTER_API_KEY skip already) - test_413_compression.py: fix threshold values in 2 preflight compression tests where context_length was too small for the compressed output to fit in one pass. - test_mcp_probe.py: add missing _MCP_AVAILABLE mock so tests work without MCP SDK. - test_mcp_tool_issue_948.py: inject MCP symbols (StdioServerParameters etc.) when SDK is not installed so patch() targets exist. - test_approve_deny_commands.py: replace time.sleep(0.3) with deterministic polling of _gateway_queues — fixes race condition where resolve fires before threads register their approval entries, causing the test to hang indefinitely. Net effect: +256 tests recovered from skip, 8 real failures fixed.
216 lines
8.2 KiB
Python
216 lines
8.2 KiB
Python
"""Tests for probe_mcp_server_tools() in tools.mcp_tool."""
|
|
|
|
import asyncio
|
|
from types import SimpleNamespace
|
|
from unittest.mock import AsyncMock, MagicMock, patch
|
|
|
|
import pytest
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def _reset_mcp_state():
|
|
"""Ensure clean MCP module state before/after each test."""
|
|
import tools.mcp_tool as mcp
|
|
old_loop = mcp._mcp_loop
|
|
old_thread = mcp._mcp_thread
|
|
old_servers = dict(mcp._servers)
|
|
yield
|
|
mcp._servers.clear()
|
|
mcp._servers.update(old_servers)
|
|
mcp._mcp_loop = old_loop
|
|
mcp._mcp_thread = old_thread
|
|
|
|
|
|
class TestProbeMcpServerTools:
|
|
"""Tests for the lightweight probe_mcp_server_tools function."""
|
|
|
|
def test_returns_empty_when_mcp_not_available(self):
|
|
with patch("tools.mcp_tool._MCP_AVAILABLE", False):
|
|
from tools.mcp_tool import probe_mcp_server_tools
|
|
result = probe_mcp_server_tools()
|
|
assert result == {}
|
|
|
|
def test_returns_empty_when_no_config(self):
|
|
with patch("tools.mcp_tool._load_mcp_config", return_value={}):
|
|
from tools.mcp_tool import probe_mcp_server_tools
|
|
result = probe_mcp_server_tools()
|
|
assert result == {}
|
|
|
|
def test_returns_empty_when_all_servers_disabled(self):
|
|
config = {
|
|
"github": {"command": "npx", "enabled": False},
|
|
"slack": {"command": "npx", "enabled": "off"},
|
|
}
|
|
with patch("tools.mcp_tool._load_mcp_config", return_value=config):
|
|
from tools.mcp_tool import probe_mcp_server_tools
|
|
result = probe_mcp_server_tools()
|
|
assert result == {}
|
|
|
|
def test_returns_tools_from_successful_server(self):
|
|
"""Successfully probed server returns its tools list."""
|
|
config = {
|
|
"github": {"command": "npx", "connect_timeout": 5},
|
|
}
|
|
mock_tool_1 = SimpleNamespace(name="create_issue", description="Create a new issue")
|
|
mock_tool_2 = SimpleNamespace(name="search_repos", description="Search repositories")
|
|
|
|
mock_server = MagicMock()
|
|
mock_server._tools = [mock_tool_1, mock_tool_2]
|
|
mock_server.shutdown = AsyncMock()
|
|
|
|
async def fake_connect(name, cfg):
|
|
return mock_server
|
|
|
|
with patch("tools.mcp_tool._MCP_AVAILABLE", True), \
|
|
patch("tools.mcp_tool._load_mcp_config", return_value=config), \
|
|
patch("tools.mcp_tool._connect_server", side_effect=fake_connect), \
|
|
patch("tools.mcp_tool._ensure_mcp_loop"), \
|
|
patch("tools.mcp_tool._run_on_mcp_loop") as mock_run, \
|
|
patch("tools.mcp_tool._stop_mcp_loop"):
|
|
|
|
# Simulate running the async probe
|
|
def run_coro(coro, timeout=120):
|
|
loop = asyncio.new_event_loop()
|
|
try:
|
|
return loop.run_until_complete(coro)
|
|
finally:
|
|
loop.close()
|
|
|
|
mock_run.side_effect = run_coro
|
|
|
|
from tools.mcp_tool import probe_mcp_server_tools
|
|
result = probe_mcp_server_tools()
|
|
|
|
assert "github" in result
|
|
assert len(result["github"]) == 2
|
|
assert result["github"][0] == ("create_issue", "Create a new issue")
|
|
assert result["github"][1] == ("search_repos", "Search repositories")
|
|
mock_server.shutdown.assert_awaited_once()
|
|
|
|
def test_failed_server_omitted_from_results(self):
|
|
"""Servers that fail to connect are silently skipped."""
|
|
config = {
|
|
"github": {"command": "npx", "connect_timeout": 5},
|
|
"broken": {"command": "nonexistent", "connect_timeout": 5},
|
|
}
|
|
mock_tool = SimpleNamespace(name="create_issue", description="Create")
|
|
mock_server = MagicMock()
|
|
mock_server._tools = [mock_tool]
|
|
mock_server.shutdown = AsyncMock()
|
|
|
|
async def fake_connect(name, cfg):
|
|
if name == "broken":
|
|
raise ConnectionError("Server not found")
|
|
return mock_server
|
|
|
|
with patch("tools.mcp_tool._MCP_AVAILABLE", True), \
|
|
patch("tools.mcp_tool._load_mcp_config", return_value=config), \
|
|
patch("tools.mcp_tool._connect_server", side_effect=fake_connect), \
|
|
patch("tools.mcp_tool._ensure_mcp_loop"), \
|
|
patch("tools.mcp_tool._run_on_mcp_loop") as mock_run, \
|
|
patch("tools.mcp_tool._stop_mcp_loop"):
|
|
|
|
def run_coro(coro, timeout=120):
|
|
loop = asyncio.new_event_loop()
|
|
try:
|
|
return loop.run_until_complete(coro)
|
|
finally:
|
|
loop.close()
|
|
|
|
mock_run.side_effect = run_coro
|
|
|
|
from tools.mcp_tool import probe_mcp_server_tools
|
|
result = probe_mcp_server_tools()
|
|
|
|
assert "github" in result
|
|
assert "broken" not in result
|
|
|
|
def test_handles_tool_without_description(self):
|
|
"""Tools without descriptions get empty string."""
|
|
config = {"github": {"command": "npx", "connect_timeout": 5}}
|
|
mock_tool = SimpleNamespace(name="my_tool") # no description attribute
|
|
|
|
mock_server = MagicMock()
|
|
mock_server._tools = [mock_tool]
|
|
mock_server.shutdown = AsyncMock()
|
|
|
|
async def fake_connect(name, cfg):
|
|
return mock_server
|
|
|
|
with patch("tools.mcp_tool._MCP_AVAILABLE", True), \
|
|
patch("tools.mcp_tool._load_mcp_config", return_value=config), \
|
|
patch("tools.mcp_tool._connect_server", side_effect=fake_connect), \
|
|
patch("tools.mcp_tool._ensure_mcp_loop"), \
|
|
patch("tools.mcp_tool._run_on_mcp_loop") as mock_run, \
|
|
patch("tools.mcp_tool._stop_mcp_loop"):
|
|
|
|
def run_coro(coro, timeout=120):
|
|
loop = asyncio.new_event_loop()
|
|
try:
|
|
return loop.run_until_complete(coro)
|
|
finally:
|
|
loop.close()
|
|
|
|
mock_run.side_effect = run_coro
|
|
|
|
from tools.mcp_tool import probe_mcp_server_tools
|
|
result = probe_mcp_server_tools()
|
|
|
|
assert result["github"][0] == ("my_tool", "")
|
|
|
|
def test_cleanup_called_even_on_failure(self):
|
|
"""_stop_mcp_loop is called even when probe fails."""
|
|
config = {"github": {"command": "npx", "connect_timeout": 5}}
|
|
|
|
with patch("tools.mcp_tool._MCP_AVAILABLE", True), \
|
|
patch("tools.mcp_tool._load_mcp_config", return_value=config), \
|
|
patch("tools.mcp_tool._ensure_mcp_loop"), \
|
|
patch("tools.mcp_tool._run_on_mcp_loop", side_effect=RuntimeError("boom")), \
|
|
patch("tools.mcp_tool._stop_mcp_loop") as mock_stop:
|
|
|
|
from tools.mcp_tool import probe_mcp_server_tools
|
|
result = probe_mcp_server_tools()
|
|
|
|
assert result == {}
|
|
mock_stop.assert_called_once()
|
|
|
|
def test_skips_disabled_servers(self):
|
|
"""Disabled servers are not probed."""
|
|
config = {
|
|
"github": {"command": "npx", "connect_timeout": 5},
|
|
"disabled_one": {"command": "npx", "enabled": False},
|
|
}
|
|
mock_tool = SimpleNamespace(name="create_issue", description="Create")
|
|
mock_server = MagicMock()
|
|
mock_server._tools = [mock_tool]
|
|
mock_server.shutdown = AsyncMock()
|
|
|
|
connect_calls = []
|
|
|
|
async def fake_connect(name, cfg):
|
|
connect_calls.append(name)
|
|
return mock_server
|
|
|
|
with patch("tools.mcp_tool._MCP_AVAILABLE", True), \
|
|
patch("tools.mcp_tool._load_mcp_config", return_value=config), \
|
|
patch("tools.mcp_tool._connect_server", side_effect=fake_connect), \
|
|
patch("tools.mcp_tool._ensure_mcp_loop"), \
|
|
patch("tools.mcp_tool._run_on_mcp_loop") as mock_run, \
|
|
patch("tools.mcp_tool._stop_mcp_loop"):
|
|
|
|
def run_coro(coro, timeout=120):
|
|
loop = asyncio.new_event_loop()
|
|
try:
|
|
return loop.run_until_complete(coro)
|
|
finally:
|
|
loop.close()
|
|
|
|
mock_run.side_effect = run_coro
|
|
|
|
from tools.mcp_tool import probe_mcp_server_tools
|
|
result = probe_mcp_server_tools()
|
|
|
|
assert "github" in result
|
|
assert "disabled_one" not in result
|
|
assert "disabled_one" not in connect_calls
|