From 8998697b4ee963643cec6d54591033ac1b344064 Mon Sep 17 00:00:00 2001 From: Marius Ciepluch <11855163+norandom@users.noreply.github.com> Date: Wed, 25 Nov 2026 18:14:06 +0100 Subject: [PATCH] fix(contributor-scan): the MCP server answers a tool that stops the CLI and stays up (3.1, follow-up) By the maintainer's decision of 2026-11-25, recorded in the handover: "McpServer.handle catches Exception only, so a missing scan path raises SystemExit through it and ends the stdio loop; openultrasast.scan and openultrasast.benchmark both take that path. One bad call would become an error result for that call and the server would stay up." `McpServer.handle` in src/openultrasast/mcp.py catches `SystemExit` beside `Exception` and answers with the error it already builds for any exception a tool raises: `-32603` with `SystemExit: scan path is not a directory: `. The stdio loop reads on, answers the next request and exits 0 at EOF; before, the process exited 1 with that message on stderr and nothing on stdout. `openultrasast.benchmark` takes the same clause when the manifest names a source path that does not exist. No other path changes: the notification return comes before the `try`, a line that does not parse is answered in `serve`, and `cli.py:588` is the only `SystemExit` a tool handler can reach (the independent reviewer's trace), raised before the configuration is loaded and before a run directory is created. "Error result" is read as that JSON-RPC error: the decision names `handle`, and the handover keeps this channel apart from `isError: true`, which `_call_tool` builds for `McpToolError`. A missing path therefore travels a different channel from a missing `path` argument or an unsupported mode; that is left for the maintainer's menu, with one pre-existing gap the reviewer found: a line that is valid JSON but not an object still ends the loop. Tests: through `serve`, a tools/call for `openultrasast.scan` with a missing path followed by a ping gives the error object and then the ping result, and `serve` returns 0; through `handle`, the same call gives that error object. Both failed on the code as it was, each with the `SystemExit` raised through the function it calls (the first in my run, the second in the reviewer's, on a copy of the code before the change); the second pins that the catch sits in `handle`, the public dispatch, where a catch in `serve` alone passes the first. The 23 test files around the scan path and the MCP server pass (242 passed before the second test was added, and `tests/test_mcp.py` 18 passed after); ruff, mypy, the three gates and compileall pass, and the gates print what they printed before the change. The real `ousast mcp` process, fed the same two lines, answered both and exited 0 with an empty stderr. An independent reviewer approved the change in one round and proposed the second test. --- src/openultrasast/mcp.py | 3 ++- tests/test_mcp.py | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) 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.