From 5ab0b2836b60b5c08221b6cefb79de60ff1e0498 Mon Sep 17 00:00:00 2001 From: cnYui Date: Sat, 21 Nov 2026 06:19:36 +0900 Subject: [PATCH 1/2] fix(mcp): report a missing agent CLI instead of crashing `secfoo mcp sync` called `gemini` or `agent` through subprocess without checking that the binary exists, so a missing CLI raised FileNotFoundError and printed a traceback. Check PATH first and return one failed SyncResult that names the missing CLI. For `agent`, this happens before the Cursor config is written. Co-Authored-By: Claude Haiku 5.5 --- src/secfoo/mcp.py | 5 +++++ tests/test_mcp.py | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/src/secfoo/mcp.py b/src/secfoo/mcp.py index 36b4f25..7bcd996 100644 --- a/src/secfoo/mcp.py +++ b/src/secfoo/mcp.py @@ -20,6 +20,7 @@ import json import re +import shutil import subprocess from dataclasses import dataclass from pathlib import Path @@ -117,6 +118,8 @@ def _gemini_add_command(server: MCPServerConfig) -> list[str]: def sync_gemini(servers: list[MCPServerConfig]) -> list[SyncResult]: + if shutil.which("gemini") is None: + return [SyncResult("*", False, "gemini isn't installed (not found on PATH)")] existing = _gemini_existing_names() results = [] for server in servers: @@ -135,6 +138,8 @@ def sync_gemini(servers: list[MCPServerConfig]) -> list[SyncResult]: def sync_cursor(servers: list[MCPServerConfig]) -> list[SyncResult]: + if shutil.which("agent") is None: + return [SyncResult("*", False, "agent isn't installed (not found on PATH)")] CURSOR_MCP_CONFIG_PATH.parent.mkdir(parents=True, exist_ok=True) existing: dict = {} if CURSOR_MCP_CONFIG_PATH.exists(): diff --git a/tests/test_mcp.py b/tests/test_mcp.py index 6ec8ccc..07d46f9 100644 --- a/tests/test_mcp.py +++ b/tests/test_mcp.py @@ -8,6 +8,13 @@ from secfoo import mcp from secfoo.settings import MCPServerConfig + +@pytest.fixture(autouse=True) +def _agent_clis_on_path(monkeypatch): + # Existing sync tests assume the gemini/agent CLIs are installed. + monkeypatch.setattr(mcp.shutil, "which", lambda name: f"/usr/bin/{name}") + + STDIO_SERVER = MCPServerConfig( name="Atlassian-Rovo-MCP", command="npx", @@ -178,3 +185,34 @@ def test_sync_unsupported_agent_raises(): mcp.sync("agy", []) with pytest.raises(ValueError): mcp.sync("claude", []) + + +def test_sync_gemini_reports_missing_cli_instead_of_crashing(monkeypatch): + monkeypatch.setattr(mcp.shutil, "which", lambda name: None) + + def fail_if_called(cmd, **kwargs): + raise AssertionError("should not run gemini when the CLI is missing") + + monkeypatch.setattr(mcp.subprocess, "run", fail_if_called) + results = mcp.sync_gemini([STDIO_SERVER]) + + assert len(results) == 1 + assert results[0].ok is False + assert "gemini" in results[0].message + + +def test_sync_cursor_reports_missing_cli_before_writing_config(tmp_path, monkeypatch): + cursor_path = tmp_path / "mcp.json" + monkeypatch.setattr(mcp, "CURSOR_MCP_CONFIG_PATH", cursor_path) + monkeypatch.setattr(mcp.shutil, "which", lambda name: None) + + def fail_if_called(cmd, **kwargs): + raise AssertionError("should not run agent when the CLI is missing") + + monkeypatch.setattr(mcp.subprocess, "run", fail_if_called) + results = mcp.sync_cursor([STDIO_SERVER]) + + assert len(results) == 1 + assert results[0].ok is False + assert "agent" in results[0].message + assert not cursor_path.exists() From 2c43589a429232721731e133d8c391438582b2c9 Mon Sep 17 00:00:00 2001 From: Satyanarayana Vuppala Date: Fri, 20 Nov 2026 23:09:30 +0000 Subject: [PATCH 2/2] fix(mcp): launch the CLI path shutil.which found On Windows npm installs gemini/agent as a .cmd shim. shutil.which finds it via PATHEXT, but subprocess.run with a bare "gemini" does not, so the new installed-check passed and sync still raised FileNotFoundError. Route the three CLI calls through _run_cli(), which launches the resolved path (same approach as BaseAdapter.run). Co-Authored-By: Claude Opus 5.5 --- src/secfoo/mcp.py | 15 ++++++++++++--- tests/test_mcp.py | 19 ++++++++++++++++++- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/secfoo/mcp.py b/src/secfoo/mcp.py index 7bcd996..624d576 100644 --- a/src/secfoo/mcp.py +++ b/src/secfoo/mcp.py @@ -93,8 +93,17 @@ def write_claude_mcp_config(servers: list[MCPServerConfig]) -> Path | None: _GEMINI_LIST_NAME_RE = re.compile(r"^[^\sA-Za-z0-9]*\s*([^\s:]+):") +def _run_cli(cmd: list[str]) -> subprocess.CompletedProcess[str]: + """subprocess.run, launching exactly the binary shutil.which found. + Popen ignores PATHEXT on Windows, so a bare "gemini" never finds npm's + `gemini.cmd` shim and raises FileNotFoundError even when the CLI is + installed (same fix as BaseAdapter.run).""" + resolved = shutil.which(cmd[0]) or cmd[0] + return subprocess.run([resolved, *cmd[1:]], capture_output=True, text=True, timeout=30, check=False) + + def _gemini_existing_names() -> set[str]: - proc = subprocess.run(["gemini", "mcp", "list"], capture_output=True, text=True, timeout=30) + proc = _run_cli(["gemini", "mcp", "list"]) names = set() for line in proc.stdout.splitlines(): match = _GEMINI_LIST_NAME_RE.match(line.strip()) @@ -126,7 +135,7 @@ def sync_gemini(servers: list[MCPServerConfig]) -> list[SyncResult]: if server.name in existing: results.append(SyncResult(server.name, True, "already configured, skipped")) continue - proc = subprocess.run(_gemini_add_command(server), capture_output=True, text=True, timeout=30) + proc = _run_cli(_gemini_add_command(server)) if proc.returncode == 0: results.append(SyncResult(server.name, True, "added")) else: @@ -161,7 +170,7 @@ def sync_cursor(servers: list[MCPServerConfig]) -> list[SyncResult]: if newly_added: CURSOR_MCP_CONFIG_PATH.write_text(json.dumps({"mcpServers": existing}, indent=2), encoding="utf-8") for name in newly_added: - proc = subprocess.run(["agent", "mcp", "enable", name], capture_output=True, text=True, timeout=30) + proc = _run_cli(["agent", "mcp", "enable", name]) if proc.returncode != 0: for result in results: if result.server_name == name: diff --git a/tests/test_mcp.py b/tests/test_mcp.py index 07d46f9..93f692b 100644 --- a/tests/test_mcp.py +++ b/tests/test_mcp.py @@ -105,7 +105,7 @@ def fake_run(cmd, **kwargs): results = mcp.sync_gemini([STDIO_SERVER]) assert results[0].ok is True assert results[0].message == "added" - assert captured["cmd"][:4] == ["gemini", "mcp", "add", "--scope"] + assert captured["cmd"][:4] == ["/usr/bin/gemini", "mcp", "add", "--scope"] assert "Atlassian-Rovo-MCP" in captured["cmd"] @@ -216,3 +216,20 @@ def fail_if_called(cmd, **kwargs): assert results[0].ok is False assert "agent" in results[0].message assert not cursor_path.exists() + + +def test_sync_launches_the_resolved_cli_path(tmp_path, monkeypatch): + # On Windows npm installs `gemini.cmd`; shutil.which finds it but a bare + # "gemini" passed to subprocess raises FileNotFoundError. + monkeypatch.setattr(mcp.shutil, "which", lambda name: rf"C:\npm\{name}.CMD") + monkeypatch.setattr(mcp, "CURSOR_MCP_CONFIG_PATH", tmp_path / "mcp.json") + launched = [] + + def fake_run(cmd, **kwargs): + launched.append(cmd[0]) + return subprocess.CompletedProcess(cmd, 0, stdout="", stderr="") + + monkeypatch.setattr(mcp.subprocess, "run", fake_run) + mcp.sync_gemini([STDIO_SERVER]) + mcp.sync_cursor([STDIO_SERVER]) + assert launched == [r"C:\npm\gemini.CMD", r"C:\npm\gemini.CMD", r"C:\npm\agent.CMD"]