diff --git a/src/openultrasast/mcp.py b/src/openultrasast/mcp.py index e7abb68d..3cc7afe1 100644 --- a/src/openultrasast/mcp.py +++ b/src/openultrasast/mcp.py @@ -328,7 +328,8 @@ class McpServer: result = self._dispatch(method, request.get("params") or {}) except McpToolError as exc: return _error(request_id, -32602, str(exc)) - except Exception as exc: # noqa: BLE001 — surface as a JSON-RPC error, never crash the loop + # SystemExit included: a tool that stops the CLI (a missing scan path) stops one call, not the loop + except (Exception, SystemExit) as exc: # noqa: BLE001 — surface as a JSON-RPC error, never crash the loop return _error(request_id, -32603, f"{type(exc).__name__}: {exc}") return {"jsonrpc": "2.0", "id": request_id, "result": result} diff --git a/tests/test_mcp.py b/tests/test_mcp.py index 48aba506..59e7b4d2 100644 --- a/tests/test_mcp.py +++ b/tests/test_mcp.py @@ -204,6 +204,41 @@ def test_serve_stdio_loop_handles_tools_list() -> None: assert {tool["name"] for tool in response["result"]["tools"]} == EXPECTED_TOOLS +def test_serve_stdio_loop_survives_a_tool_that_stops_the_cli(tmp_path: Path) -> None: + """A tool can stop the CLI (`_run_scan` on a missing scan path); the loop answers that call and reads on.""" + scan = {"name": "openultrasast.scan", "arguments": {"path": str(tmp_path / "missing")}} + requests = [ + {"jsonrpc": "2.0", "id": 1, "method": "tools/call", "params": scan}, + {"jsonrpc": "2.0", "id": 2, "method": "ping"}, + ] + stdin = io.StringIO("".join(json.dumps(request) + "\n" for request in requests)) + stdout = io.StringIO() + + assert serve(stdin, stdout) == 0 + + failed, pong = (json.loads(line) for line in stdout.getvalue().splitlines()) + assert failed == { + "jsonrpc": "2.0", + "id": 1, + "error": {"code": -32603, "message": f"SystemExit: scan path is not a directory: {tmp_path / 'missing'}"}, + } + assert pong == {"jsonrpc": "2.0", "id": 2, "result": {}} + + +def test_handle_answers_a_tool_that_stops_the_cli_with_an_error(tmp_path: Path) -> None: + """The catch lives in `handle`, the public dispatch; `serve` is not its only possible caller.""" + scan = {"name": "openultrasast.scan", "arguments": {"path": str(tmp_path / "missing")}} + server = McpServer() + + response = server.handle({"jsonrpc": "2.0", "id": 1, "method": "tools/call", "params": scan}) + + assert response == { + "jsonrpc": "2.0", + "id": 1, + "error": {"code": -32603, "message": f"SystemExit: scan path is not a directory: {tmp_path / 'missing'}"}, + } + + def test_the_findings_list_carries_the_rung_and_leads_with_what_was_decided(tmp_path: Path) -> None: """The final objective is an LLM diving in without re-deriving the reasoning.