diff --git a/cli/mcp_cli.py b/cli/mcp_cli.py index af8c802bd..6845b78fc 100644 --- a/cli/mcp_cli.py +++ b/cli/mcp_cli.py @@ -15,6 +15,7 @@ mcp_inventory_view, mcp_oauth_flow_view, mcp_preset_inventory_view, + mcp_probe_all_view, mcp_probe_view, ) from core.domain.project import TrustState @@ -38,8 +39,13 @@ def _parser() -> argparse.ArgumentParser: presets.add_argument("--json", action="store_true") probe = commands.add_parser("test", help="Perform a real MCP handshake.") - probe.add_argument("name") + probe.add_argument("name", nargs="?") _workspace_arguments(probe, allow_trust=False) + probe.add_argument( + "--all", + action="store_true", + help="Test every enabled MCP server instead of one named server.", + ) probe.add_argument("--json", action="store_true") login = commands.add_parser("login", help="Authorize an OAuth MCP server.") @@ -179,9 +185,21 @@ def run(argv: list[str] | None = None) -> int: ) ) elif args.command == "test": - result = mcp_probe_view( - application.mcp.probe(args.name, project_id=project_id) - ) + if args.all == bool(args.name): + raise ValueError("pass exactly one of a server name or --all") + if args.all: + inventory = application.mcp.list(project_id) + result = mcp_probe_all_view( + [ + application.mcp.probe(info.name, project_id=project_id) + for info in inventory.servers + if info.enabled + ] + ) + else: + result = mcp_probe_view( + application.mcp.probe(args.name, project_id=project_id) + ) exit_code = 0 if result["ok"] else 1 elif args.command == "login": flow = application.mcp.oauth_start( @@ -241,7 +259,7 @@ def run(argv: list[str] | None = None) -> int: elif args.command == "presets": _print_presets(result) elif args.command == "test": - _print_probe(result) + _print_probe_all(result) if args.all else _print_probe(result) elif args.command == "login": _print_oauth(result) elif args.command == "logout": @@ -560,6 +578,23 @@ def _print_probe(result: dict[str, Any]) -> None: print(f"Connection failed for {result['name']}: {result['error']}") +def _print_probe_all(result: dict[str, Any]) -> None: + for server in result["servers"]: + if server["ok"]: + print( + f"PASS {server['name']}: {server['toolCount']} tools, " + f"{server['resourceCount']} resources, {server['promptCount']} prompts " + f"in {server['elapsedSeconds']:.3f}s" + ) + else: + print(f"FAIL {server['name']}: {server['error']}") + passed = result["total"] - result["failed"] + print( + f"{passed} passed, {result['failed']} failed " + f"of {result['total']} enabled MCP servers." + ) + + def _print_oauth(result: dict[str, Any]) -> None: if result["status"] == "authenticated": print(f"Authorized {result['name']}.") diff --git a/core/application/views.py b/core/application/views.py index 6a6a8b50b..03dfd346d 100644 --- a/core/application/views.py +++ b/core/application/views.py @@ -339,6 +339,16 @@ def mcp_probe_view(result) -> dict[str, Any]: } +def mcp_probe_all_view(results) -> dict[str, Any]: + probed = [mcp_probe_view(result) for result in results] + return { + "ok": all(item["ok"] for item in probed), + "total": len(probed), + "failed": sum(1 for item in probed if not item["ok"]), + "servers": probed, + } + + def mcp_oauth_flow_view(flow) -> dict[str, Any]: return { "flowId": flow.flow_id, diff --git a/tests/test_mcp_cli.py b/tests/test_mcp_cli.py index bf1f37919..890df2917 100644 --- a/tests/test_mcp_cli.py +++ b/tests/test_mcp_cli.py @@ -202,6 +202,76 @@ def test_mcp_cli_returns_failure_for_a_failed_real_probe( assert _json_output(capsys)["ok"] is False +def test_mcp_cli_tests_every_enabled_server( + tmp_path: Path, + monkeypatch, + capsys, +) -> None: + monkeypatch.setenv("DEEPCODE_HOME", str(tmp_path / "home")) + fixture = Path(__file__).parent / "fixtures" / "mcp_runtime_server.py" + assert ( + run( + [ + "add", + "fixture", + "--enable", + "--command", + sys.executable, + str(fixture), + ] + ) + == 0 + ) + assert run(["add", "broken", "--enable", "--command", "not-a-command"]) == 0 + capsys.readouterr() + + assert run(["test", "--all", "--json"]) == 1 + report = _json_output(capsys) + assert report["ok"] is False + assert (report["total"], report["failed"]) == (2, 1) + status = {server["name"]: server["ok"] for server in report["servers"]} + assert status == {"fixture": True, "broken": False} + + +def test_mcp_cli_test_all_skips_disabled_servers( + tmp_path: Path, + monkeypatch, + capsys, +) -> None: + monkeypatch.setenv("DEEPCODE_HOME", str(tmp_path / "home")) + fixture = Path(__file__).parent / "fixtures" / "mcp_runtime_server.py" + assert ( + run( + [ + "add", + "fixture", + "--enable", + "--command", + sys.executable, + str(fixture), + ] + ) + == 0 + ) + assert run(["add", "broken", "--command", "not-a-command"]) == 0 + capsys.readouterr() + + assert run(["test", "--all", "--json"]) == 0 + report = _json_output(capsys) + assert (report["total"], report["failed"]) == (1, 0) + assert [server["name"] for server in report["servers"]] == ["fixture"] + + +def test_mcp_cli_test_requires_a_name_or_all( + tmp_path: Path, + monkeypatch, + capsys, +) -> None: + monkeypatch.setenv("DEEPCODE_HOME", str(tmp_path / "home")) + assert run(["test"]) == 2 + assert "exactly one" in capsys.readouterr().err + + def test_mcp_list_names_concerns_and_states_default_posture_once( tmp_path: Path, monkeypatch,