"""Local MCP stdio server: extra gating, protocol round-trip, stream hygiene.""" from __future__ import annotations import asyncio import json import os import subprocess import sys from pathlib import Path import pytest import obliteratus.mcp_server as mcp_server_module from obliteratus import __version__ from obliteratus.mcp_policy import READ_TOOLS from obliteratus.mcp_server import ( EXIT_MISSING_EXTRA, EXIT_POLICY_ERROR, MissingMcpExtraError, build_arg_parser, create_server, main, serve, serve_from_namespace, ) REPO_ROOT = Path(__file__).resolve().parent.parent SERVER_CMD = [sys.executable, "-m", "obliteratus.mcp_server"] def _namespace(tmp_path, **overrides): args = build_arg_parser().parse_args( ["--archive-root", str(tmp_path), *overrides.pop("extra", [])] ) for key, value in overrides.items(): setattr(args, key, value) return args def _assert_source_install_hint(text: str) -> None: """The missing-extra hint names only the source-checkout install (#603). The PyPI project ``obliteratus`` is quarantined with no installable files, so the hint must never send users to ``pip install "obliteratus[mcp]"``. """ assert "uv sync --locked --extra mcp" in text assert "python -m pip install '.[mcp]'" in text assert "docs/MCP_LOCAL_SETUP.md" in text assert "obliteratus[mcp]" not in text @pytest.fixture def _block_mcp_import(monkeypatch): """Simulate a plain install without the optional ``obliteratus[mcp]`` extra.""" removed = { name: module for name, module in sys.modules.items() if name == "mcp" or name.startswith("mcp.") } for name in removed: monkeypatch.delitem(sys.modules, name, raising=False) # A None entry makes ``import mcp`` raise ImportError immediately. monkeypatch.setitem(sys.modules, "mcp", None) yield # monkeypatch restores sys.modules automatically def test_create_server_raises_missing_extra_error(tmp_path, _block_mcp_import): from obliteratus.mcp_policy import policy_from_args policy = policy_from_args(_namespace(tmp_path)) with pytest.raises(MissingMcpExtraError) as exc_info: create_server(policy) _assert_source_install_hint(str(exc_info.value)) def test_missing_extra_gives_clean_stderr_error_without_traceback( tmp_path, capsys, _block_mcp_import ): from obliteratus.mcp_policy import policy_from_args policy = policy_from_args(_namespace(tmp_path)) code = serve(policy) assert code == EXIT_MISSING_EXTRA captured = capsys.readouterr() assert "Traceback" not in captured.err _assert_source_install_hint(captured.err) def test_serve_from_namespace_rejects_invalid_policy_before_binding( tmp_path, capsys, _block_mcp_import ): args = _namespace(tmp_path) args.archive_root = "relative/path" code = serve_from_namespace(args) assert code == EXIT_POLICY_ERROR captured = capsys.readouterr() assert "invalid MCP policy" in captured.err assert "Traceback" not in captured.err def test_cli_mcp_serve_degrades_gracefully_without_extra( tmp_path, capsys, _block_mcp_import ): from obliteratus import cli with pytest.raises(SystemExit) as exc_info: cli.main(["mcp", "serve", "--archive-root", str(tmp_path)]) assert exc_info.value.code == EXIT_MISSING_EXTRA captured = capsys.readouterr() _assert_source_install_hint(captured.err) assert "Traceback" not in captured.err # No CLI banner may leak onto stdout: the serve path owns the stream. assert "OBLITERATUS" not in captured.out def test_cli_mcp_serve_help_lists_policy_flags(capsys): from obliteratus import cli with pytest.raises(SystemExit) as exc_info: cli.main(["mcp", "serve", "--help"]) assert exc_info.value.code == 0 captured = capsys.readouterr() assert "--archive-root" in captured.out assert "--allow-network" in captured.out def test_top_level_help_still_works_without_extra(capsys): from obliteratus import cli with pytest.raises(SystemExit) as exc_info: cli.main(["--help"]) assert exc_info.value.code == 0 assert "mcp" in capsys.readouterr().out def test_module_help_exits_zero_without_extra(): proc = subprocess.run( [*SERVER_CMD, "--help"], cwd=REPO_ROOT, capture_output=True, text=True, timeout=30, env={**os.environ, "PYTHONDONTWRITEBYTECODE": "1"}, ) assert proc.returncode == 0 assert "--archive-root" in proc.stdout def test_server_module_imports_no_heavy_runtimes(): code = ( "import sys, obliteratus.mcp_server; " "bad = sorted(m for m in sys.modules " "if m in ('torch', 'transformers', 'mcp') or m.startswith(('torch.', 'transformers.', 'mcp.'))); " "assert not bad, bad" ) proc = subprocess.run( [sys.executable, "-c", code], cwd=REPO_ROOT, capture_output=True, text=True, timeout=30, env={**os.environ, "PYTHONDONTWRITEBYTECODE": "1"}, ) assert proc.returncode == 0, proc.stderr def _sdk_client_helpers(): """Import the official SDK client pieces; skip when the extra is absent.""" pytest.importorskip("mcp") from mcp.client.session import ClientSession # noqa: E402 from mcp.client.stdio import StdioServerParameters, stdio_client # noqa: E402 return ClientSession, StdioServerParameters, stdio_client def _server_params(archive_dir, *, env=None): _, StdioServerParameters, _ = _sdk_client_helpers() base_env = {"PYTHONDONTWRITEBYTECODE": "1", "PATH": os.environ["PATH"]} if env: base_env.update(env) return StdioServerParameters( command=sys.executable, args=["-m", "obliteratus.mcp_server", "--archive-root", str(archive_dir)], cwd=str(REPO_ROOT), env=base_env, ) def _run(coro): return asyncio.run(coro) def test_sdk_client_round_trip_reports_version_and_capabilities(tmp_path): ClientSession, _, stdio_client = _sdk_client_helpers() async def scenario(): async with stdio_client(_server_params(tmp_path)) as (read, write): async with ClientSession(read, write) as session: init = await session.initialize() # Negotiated version is derived from the pinned SDK, never # hard-coded: the current modern version plus the supported # legacy handshake path. from mcp.client.session import HANDSHAKE_PROTOCOL_VERSIONS # noqa: E402 from mcp_types.version import MODERN_PROTOCOL_VERSIONS # noqa: E402 known = set(HANDSHAKE_PROTOCOL_VERSIONS) | set(MODERN_PROTOCOL_VERSIONS) assert init.protocol_version in known tools = await session.list_tools() names = [tool.name for tool in tools.tools] assert sorted(names) == sorted(READ_TOOLS) # deterministic listing result = await session.call_tool("server_status", {}) assert result.is_error is False payload = json.loads(result.content[0].text) assert set(payload) == {"version", "capabilities"} assert payload["version"] == __version__ assert set(payload["capabilities"]) == {"tools", "network", "execution"} assert payload["capabilities"]["tools"] == sorted(READ_TOOLS) assert payload["capabilities"]["network"] is False assert payload["capabilities"]["execution"] is False raw = json.dumps(payload).lower() for leaked in ("token", "secret", "password", "api_key", "home", os.environ.get("USER", "\0")): assert leaked not in raw, f"possible leak of {leaked!r}" _run(scenario()) def test_unknown_tool_errors_without_corrupting_stream(tmp_path): ClientSession, _, stdio_client = _sdk_client_helpers() async def scenario(): async with stdio_client(_server_params(tmp_path)) as (read, write): async with ClientSession(read, write) as session: await session.initialize() bad = await session.call_tool("no-such-tool", {}) assert bad.is_error is True # The stream stays usable for a subsequent valid request. good = await session.call_tool("server_status", {}) assert good.is_error is False assert json.loads(good.content[0].text)["version"] == __version__ _run(scenario()) def test_stdout_carries_only_protocol_bytes_and_survives_malformed_input(tmp_path): pytest.importorskip("mcp") env ={**os.environ, "PYTHONDONTWRITEBYTECODE": "1"} proc = subprocess.Popen( [*SERVER_CMD, "--archive-root", str(tmp_path)], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=REPO_ROOT, env=env, ) try: assert proc.stdin and proc.stdout init = { "jsonrpc": "2.0", "id": 1, "method": "initialize", "params": { "protocolVersion": "2025-11-25", "capabilities": {}, "clientInfo": {"name": "rawtest", "version": "0"}, }, } proc.stdin.write((json.dumps(init) + "\n").encode()) proc.stdin.flush() response = json.loads(proc.stdout.readline()) assert response["id"] == 1 and "result" in response # Malformed lines must not kill the server or pollute stdout. proc.stdin.write(b"this is not json\n") proc.stdin.write(b'{"jsonrpc": "2.0", "id": 9, "method": "ping",\n') proc.stdin.flush() proc.stdin.write( (json.dumps({"jsonrpc": "2.0", "method": "notifications/initialized"}) + "\n").encode() ) proc.stdin.write( (json.dumps({"jsonrpc": "2.0", "id": 2, "method": "tools/list", "params": {}}) + "\n").encode() ) proc.stdin.flush() tools_response = None for _ in range(20): raw = proc.stdout.readline() assert raw, "server went silent after malformed input" parsed = json.loads(raw) # every stdout line is pure JSON-RPC assert parsed.get("jsonrpc") == "2.0", f"non-protocol bytes: {raw!r}" if parsed.get("id") == 2: tools_response = parsed break assert tools_response is not None names = [t["name"] for t in tools_response["result"]["tools"]] assert sorted(names) == sorted(READ_TOOLS) finally: if proc.stdin: proc.stdin.close() proc.terminate() try: proc.wait(timeout=15) finally: proc.stdout.close() proc.stderr.close() def test_offline_server_opens_no_connections_and_creates_no_artifacts(tmp_path): _sdk_client_helpers() # skip when the extra is absent ClientSession, _, stdio_client = _sdk_client_helpers() guard_dir = tmp_path / "guard" guard_dir.mkdir() (guard_dir / "sitecustomize.py").write_text( "import socket\n" "_real_connect = socket.socket.connect\n" "def _blocked_connect(self, address, *args, **kwargs):\n" " raise RuntimeError('network disabled in offline MCP test')\n" "socket.socket.connect = _blocked_connect\n" ) home_dir = tmp_path / "home" home_dir.mkdir() work_dir = tmp_path / "work" work_dir.mkdir() before = set(work_dir.rglob("*")) async def scenario(): env = {"HOME": str(home_dir), "PYTHONPATH": str(guard_dir)} async with stdio_client(_server_params(tmp_path, env=env)) as (read, write): async with ClientSession(read, write) as session: await session.initialize() tools = await session.list_tools() names = [tool.name for tool in tools.tools] assert sorted(names) == sorted(READ_TOOLS) assert not {"launch_run", "cancel_run", "plan_run"}.intersection(names) result = await session.call_tool("server_status", {}) capabilities = json.loads(result.content[0].text)["capabilities"] assert capabilities["network"] is False assert capabilities["execution"] is False _run(scenario()) assert set(work_dir.rglob("*")) == before # no run artifacts assert not any(home_dir.iterdir()) # no model downloads / cache writes def test_main_entry_point_rejects_bad_policy(tmp_path, capsys): code = main(["--archive-root", "relative/path"]) assert code == EXIT_POLICY_ERROR assert "invalid MCP policy" in capsys.readouterr().err def test_main_entry_point_missing_extra_without_traceback(tmp_path, capsys, _block_mcp_import): code = main(["--archive-root", str(tmp_path)]) assert code == EXIT_MISSING_EXTRA captured = capsys.readouterr() assert "Traceback" not in captured.err _assert_source_install_hint(captured.err) def test_main_accepts_explicit_positive_limits(tmp_path, monkeypatch): import obliteratus.mcp_server as server_module seen = {} def fake_serve(policy): seen["policy"] = policy return 0 monkeypatch.setattr(server_module, "serve", fake_serve) code = main( [ "--archive-root", str(tmp_path), "--max-input-bytes", "4096", "--max-output-bytes", "8192", ] ) assert code == 0 assert seen["policy"].max_input_bytes == 4096 assert seen["policy"].max_output_bytes == 8192 @pytest.mark.parametrize("bad_value", ["0", "-10", "not-a-number"]) def test_main_rejects_non_positive_limits(tmp_path, bad_value): with pytest.raises(SystemExit) as exc_info: main(["--archive-root", str(tmp_path), "--max-input-bytes", bad_value]) assert exc_info.value.code == 2 def test_load_sdk_returns_server_class_when_extra_present(): pytest.importorskip("mcp") server_class = mcp_server_module._load_sdk() assert server_class.__name__ == "MCPServer" def test_serve_registers_status_tool_and_runs_stdio(tmp_path, monkeypatch): from obliteratus.mcp_policy import policy_from_args calls: dict = {} class FakeMCPServer: def __init__(self, name, version=""): calls["name"] = name calls["version"] = version calls["server"] = self self.tools = {} def tool(self, name=None, description=None, **kwargs): def decorator(fn): self.tools[name or fn.__name__] = fn return fn return decorator def run(self, transport="stdio"): calls["transport"] = transport monkeypatch.setattr(mcp_server_module, "_load_sdk", lambda: FakeMCPServer) policy = policy_from_args(_namespace(tmp_path)) assert serve(policy) == 0 assert calls["name"] == "obliteratus" assert calls["version"] == __version__ assert calls["transport"] == "stdio" status_fn = calls["server"].tools["server_status"] assert status_fn() == {"version": __version__, "capabilities": policy.capabilities()} def test_sdk_stdio_read_tools_accept_valid_and_reject_untrusted_inputs(tmp_path): ClientSession, _, stdio_client = _sdk_client_helpers() run_id = 'run-' + 'a' * 32 run = tmp_path / run_id run.mkdir() (run / 'manifest.json').write_text(json.dumps({ 'run_id': run_id, 'status': 'succeeded', 'result': {'checkpoint': '/secret/path', 'metrics': {'accuracy': .5, 'secret': 'secret'}}, })) async def scenario(): params = _server_params(tmp_path) params.args.extend(['--allowed-root', str(tmp_path)]) async with stdio_client(params) as (read, write): async with ClientSession(read, write) as session: await session.initialize() for name, arguments in [ ('list_catalog', {'kind': 'strategy'}), ('estimate_gpu', {'parameters': 1000, 'bits': 4, 'copies': 2}), ('inspect_checkpoint', {'path': str(run)}), ('list_runs', {}), ('get_run_status', {'run_id': run_id}), ('get_run_result', {'run_id': run_id}), ]: result = await session.call_tool(name, arguments) assert not result.is_error, (name, result) assert 'secret' not in result.model_dump_json() bad = await session.call_tool('get_run_status', {'run_id': '../secret'}) assert bad.is_error assert 'secret' not in bad.model_dump_json() good = await session.call_tool('get_run_status', {'run_id': run_id}) assert not good.is_error _run(scenario()) def test_sdk_stdio_execution_tokens_failure_reconnect_and_cancel(tmp_path): """Protocol lifecycle with injected spawn failure; no runtime/sandbox launch.""" ClientSession, _, stdio_client = _sdk_client_helpers() archive, model, guard = tmp_path / 'archive', tmp_path / 'model', tmp_path / 'guard' for directory in (archive, model, guard): directory.mkdir(mode=0o700) (model / 'config.json').write_text('{}') (model / 'model.safetensors').write_bytes(b'synthetic') (guard / 'sitecustomize.py').write_text( 'import obliteratus.mcp_execution as execution\n' 'from obliteratus.run_archive import RunArchive\n' 'execution.sandbox_identity = lambda: (1, 2, 3, 4)\n' '_original = RunArchive.launch\n' 'def failed_spawn(*args, **kwargs):\n' " raise OSError('sensitive-probe-secret')\n" 'def guarded(self, arguments, **kwargs):\n' ' return _original(self, arguments, popen=failed_spawn, **kwargs)\n' 'RunArchive.launch = guarded\n' ) def parameters(): params = _server_params(archive, env={'PYTHONPATH': str(guard) + os.pathsep + str(REPO_ROOT)}) params.args.extend(['--allow-exec', '--allowed-root', str(model)]) return params async def scenario(): async with stdio_client(parameters()) as (read, write): async with ClientSession(read, write) as session: await session.initialize() request = {'model': {'kind': 'local', 'identifier': str(model)}, 'method': 'basic', 'output_root': str(archive), 'resources': {'parameters': 1000}} planned = await session.call_tool('plan_run', request) assert not planned.is_error token, run_id = planned.structured_content['token'], planned.structured_content['run_id'] launched = await session.call_tool('launch_run', {'token': token}) assert not launched.is_error replay = await session.call_tool('launch_run', {'token': token}) assert replay.is_error and token not in replay.model_dump_json() async with stdio_client(parameters()) as (read, write): async with ClientSession(read, write) as session: await session.initialize() result = await session.call_tool('get_run_status', {'run_id': run_id}) assert result.structured_content['status'] == 'failed' assert 'sensitive-probe-secret' not in result.model_dump_json() terminal = await session.call_tool('get_run_result', {'run_id': run_id}) assert not terminal.is_error cancelled = await session.call_tool('cancel_run', {'run_id': run_id}) assert cancelled.structured_content['status'] == 'failed' assert (await session.call_tool('launch_run', {'token': token})).is_error _run(scenario())