mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-06 19:13:55 +01:00
Integrate bounded admission, UI ownership and controls, opt-in durable MCP execution, rank-local TP2 groundwork, regression hardening and operator guides. Physical larger-memory and two-host qualification remain separate.
291 lines
12 KiB
Python
291 lines
12 KiB
Python
"""Thin MCP SDK registration + stdio startup for OBLITERATUS (issue #209).
|
|
|
|
Module scope imports only the light top-level ``obliteratus`` package,
|
|
:mod:`obliteratus.mcp_policy`, and the standard library. The MCP SDK itself
|
|
(the optional ``obliteratus[mcp]`` extra) is imported lazily inside
|
|
:func:`create_server`, so plain package installs never require it.
|
|
|
|
Stdout carries only JSON-RPC protocol bytes; every diagnostic — including the
|
|
missing-extra hint and policy rejections — goes to stderr.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import inspect
|
|
import json
|
|
import sys
|
|
from typing import Any
|
|
|
|
from obliteratus import __version__
|
|
from obliteratus.mcp_policy import (
|
|
DEFAULT_MAX_INPUT_BYTES,
|
|
DEFAULT_MAX_OUTPUT_BYTES,
|
|
McpPolicy,
|
|
McpPolicyError,
|
|
SERVER_STATUS_TOOL,
|
|
policy_from_args,
|
|
)
|
|
|
|
SERVER_NAME = "obliteratus"
|
|
|
|
EXIT_POLICY_ERROR = 2
|
|
EXIT_MISSING_EXTRA = 3
|
|
EXIT_SERVER_ERROR = 4
|
|
|
|
_MCP_EXTRA_SPEC = "mcp>=2.2,<2.3"
|
|
|
|
|
|
class MissingMcpExtraError(ImportError):
|
|
"""The optional ``obliteratus[mcp]`` extra is not installed."""
|
|
|
|
|
|
def _positive_int(value: str) -> int:
|
|
"""Parse a strictly positive integer for public MCP CLI boundaries."""
|
|
try:
|
|
parsed = int(value)
|
|
except ValueError as exc:
|
|
raise argparse.ArgumentTypeError("must be a positive integer") from exc
|
|
if parsed <= 0:
|
|
raise argparse.ArgumentTypeError("must be a positive integer")
|
|
return parsed
|
|
|
|
|
|
def add_serve_arguments(parser: argparse.ArgumentParser) -> argparse.ArgumentParser:
|
|
"""Attach the ``serve`` policy flags to ``parser`` (shared CLI surface)."""
|
|
parser.add_argument(
|
|
"--archive-root",
|
|
default=None,
|
|
metavar="DIR",
|
|
help=(
|
|
"Explicit archive root (required; must be an absolute path; "
|
|
"never auto-created)"
|
|
),
|
|
)
|
|
parser.add_argument(
|
|
"--allowed-root",
|
|
action="append",
|
|
default=None,
|
|
metavar="DIR",
|
|
help="Additional allowed filesystem root (absolute path; may repeat)",
|
|
)
|
|
parser.add_argument(
|
|
"--max-input-bytes",
|
|
type=_positive_int,
|
|
default=DEFAULT_MAX_INPUT_BYTES,
|
|
metavar="N",
|
|
help=f"Maximum per-request input bytes (default: {DEFAULT_MAX_INPUT_BYTES})",
|
|
)
|
|
parser.add_argument(
|
|
"--max-output-bytes",
|
|
type=_positive_int,
|
|
default=DEFAULT_MAX_OUTPUT_BYTES,
|
|
metavar="N",
|
|
help=f"Maximum per-request output bytes (default: {DEFAULT_MAX_OUTPUT_BYTES})",
|
|
)
|
|
parser.add_argument(
|
|
"--allow-network",
|
|
action="store_true",
|
|
help="Permit network for separately enabled remote-model downloads during execution",
|
|
)
|
|
parser.add_argument(
|
|
"--no-network",
|
|
action="store_true",
|
|
help="Explicitly confirm the offline posture",
|
|
)
|
|
parser.add_argument(
|
|
"--allow-exec",
|
|
action="store_true",
|
|
help="Register bounded plan/launch/cancel tools (Linux Bubblewrap required to launch)",
|
|
)
|
|
parser.add_argument(
|
|
"--no-exec",
|
|
action="store_true",
|
|
help="Explicitly confirm the no-execution posture",
|
|
)
|
|
parser.add_argument('--allow-remote-models', action='store_true',
|
|
help='Permit bundled remote model IDs; also requires network and downloads opt-in')
|
|
parser.add_argument('--allow-downloads', action='store_true',
|
|
help='Permit model downloads into isolated per-run cache')
|
|
for name, default in {
|
|
'max-concurrent-runs': 1, 'max-memory-bytes': 16 << 30,
|
|
'max-work-bytes': 512 << 20, 'max-runtime-seconds': 3600,
|
|
'max-threads': 4, 'max-sequence-length': 512,
|
|
'max-generation-tokens': 128, 'plan-ttl-seconds': 60,
|
|
}.items():
|
|
parser.add_argument('--' + name, type=_positive_int, default=default,
|
|
help=f'Immutable execution ceiling (default: {default})')
|
|
return parser
|
|
|
|
|
|
def build_arg_parser() -> argparse.ArgumentParser:
|
|
"""Argument parser for ``python -m obliteratus.mcp_server``."""
|
|
parser = argparse.ArgumentParser(
|
|
prog="obliteratus.mcp_server",
|
|
description="Serve the OBLITERATUS MCP interface over stdio",
|
|
)
|
|
return add_serve_arguments(parser)
|
|
|
|
|
|
def _load_sdk() -> type:
|
|
"""Import the MCP SDK server class lazily; fail with an install hint."""
|
|
try:
|
|
from mcp.server.mcpserver import MCPServer
|
|
except ImportError as exc:
|
|
raise MissingMcpExtraError(
|
|
"the 'mcp' extra is not installed; install it with: "
|
|
f'pip install "obliteratus[mcp]" (requires {_MCP_EXTRA_SPEC})'
|
|
) from exc
|
|
return MCPServer
|
|
|
|
|
|
def _server_status_payload(policy: McpPolicy) -> dict[str, Any]:
|
|
"""Allowlist-based ``server_status`` payload: version + capabilities only."""
|
|
return {
|
|
"version": __version__,
|
|
"capabilities": policy.capabilities(),
|
|
}
|
|
|
|
|
|
def create_server(policy: McpPolicy) -> Any:
|
|
"""Build the MCP server for ``policy`` (imports the SDK lazily)."""
|
|
MCPServer = _load_sdk()
|
|
from obliteratus.mcp_tools import ReadTools
|
|
|
|
class BoundedServer(MCPServer):
|
|
async def run_stdio_async(self):
|
|
from obliteratus.mcp_transport import run_stdio
|
|
await run_stdio(self, policy)
|
|
|
|
async def call_tool(self, name, arguments, context=None):
|
|
from mcp_types import CallToolResult, TextContent
|
|
|
|
try:
|
|
encoded = json.dumps({"name": name, "arguments": arguments}, allow_nan=False).encode()
|
|
if len(encoded) > policy.max_input_bytes or name not in signatures:
|
|
raise ValueError("request rejected")
|
|
signatures[name].bind(**arguments)
|
|
for key, value in arguments.items():
|
|
if key in {"offset", "limit", "bits", "copies"} and type(value) is not int:
|
|
raise ValueError("invalid integer")
|
|
if key in {'parameters', 'total_parameters', 'active_parameters', 'gpu_memory_bytes',
|
|
'overhead_fraction', 'reserve_bytes'} and type(value) not in {int, float}:
|
|
raise ValueError("invalid count")
|
|
if key in {"kind", "path", "run_id", "dtype"} and type(value) is not str:
|
|
raise ValueError("invalid string")
|
|
if key in {'token', 'method', 'output_root'} and type(value) is not str:
|
|
raise ValueError('invalid string')
|
|
if key in {'model', 'resources'}:
|
|
if type(value) is not dict:
|
|
raise ValueError('typed request required')
|
|
if key == 'model' and (set(value) != {'kind', 'identifier'} or
|
|
any(type(item) is not str for item in value.values())):
|
|
raise ValueError('invalid model')
|
|
if key == 'resources' and (
|
|
not set(value) <= {'parameters', 'memory_bytes', 'work_bytes', 'runtime_seconds',
|
|
'threads', 'sequence_length', 'generation_tokens'} or
|
|
any(type(item) is not int for item in value.values())
|
|
):
|
|
raise ValueError('invalid resources')
|
|
result = await super().call_tool(name, arguments, context)
|
|
payload = result.model_dump(mode="json", by_alias=True, exclude_none=True)
|
|
if len(json.dumps(payload, allow_nan=False).encode()) > policy.max_output_bytes:
|
|
raise ValueError("response exceeds limit")
|
|
return result
|
|
except Exception:
|
|
return CallToolResult(is_error=True, content=[TextContent(
|
|
type="text", text="MCP request rejected (invalid, unavailable, or exceeds limits).",
|
|
)])
|
|
|
|
server = BoundedServer(SERVER_NAME, version=__version__)
|
|
signatures = {}
|
|
hints = {"read_only_hint": True, "destructive_hint": False,
|
|
"idempotent_hint": True, "open_world_hint": False}
|
|
|
|
@server.tool(
|
|
name=SERVER_STATUS_TOOL,
|
|
description="Report the OBLITERATUS server version and enabled capabilities.",
|
|
annotations=hints,
|
|
)
|
|
def server_status() -> dict[str, Any]:
|
|
return _server_status_payload(policy)
|
|
|
|
signatures[SERVER_STATUS_TOOL] = inspect.signature(server_status)
|
|
if policy.execution_enabled:
|
|
from obliteratus.mcp_execution import ExecutionTools
|
|
tools = ExecutionTools(policy)
|
|
else:
|
|
tools = ReadTools(policy)
|
|
descriptions = {
|
|
"list_catalog": "List bundled model, study or strategy names; bounded deterministic pages.",
|
|
"estimate_gpu": "Arithmetic GPU budget from total/active counts, dtype, declared GPU bytes and assumptions; no probe. Fits is not a runtime guarantee.",
|
|
"inspect_checkpoint": "Bounded CheckpointService descriptor under allowed roots; recognized config fields and filename classification. No tensor headers/payloads, arbitrary contents or integrity claim.",
|
|
"list_runs": "List durable archived states; no worker probes or recovery writes.",
|
|
"get_run_status": "Read durable archived state by run ID; no process probing.",
|
|
"get_run_result": "Read terminal state and allowlisted numeric metrics; excludes paths, logs and errors.",
|
|
}
|
|
if policy.execution_enabled:
|
|
descriptions['get_run_status'] = 'Read durable status; may probe the verified worker and commit worker-loss recovery.'
|
|
descriptions['get_run_result'] += ' May probe the verified worker and commit worker-loss recovery.'
|
|
for name, description in descriptions.items():
|
|
function = getattr(tools, name)
|
|
signatures[name] = inspect.signature(function)
|
|
annotations = dict(hints)
|
|
if policy.execution_enabled and name in {'get_run_status', 'get_run_result'}:
|
|
annotations['read_only_hint'] = False
|
|
server.tool(name=name, description=description, annotations=annotations)(function)
|
|
if policy.execution_enabled:
|
|
for name, description in {
|
|
'plan_run': 'Validate bounded CPU resources and explicit roots; return destination, side effects and a short-lived one-use token. Does not launch.',
|
|
'launch_run': 'Consume a plan token once, revalidate and submit a durable bounded CPU run. Poll status by returned ID.',
|
|
'cancel_run': 'Request cancellation of an MCP-managed durable run; may signal its verified worker process group.',
|
|
}.items():
|
|
function = getattr(tools, name)
|
|
signatures[name] = inspect.signature(function)
|
|
annotations = {'read_only_hint': name == 'plan_run',
|
|
'destructive_hint': name == 'cancel_run',
|
|
'idempotent_hint': name == 'cancel_run',
|
|
'open_world_hint': policy.capabilities()['network'] and name == 'launch_run'}
|
|
server.tool(name=name, description=description, annotations=annotations)(function)
|
|
return server
|
|
|
|
|
|
def serve(policy: McpPolicy) -> int:
|
|
"""Run the MCP stdio server under a validated policy. Returns exit code."""
|
|
try:
|
|
server = create_server(policy)
|
|
except MissingMcpExtraError as exc:
|
|
print(f"obliteratus: {exc}", file=sys.stderr)
|
|
return EXIT_MISSING_EXTRA
|
|
except Exception:
|
|
print('obliteratus: MCP startup failed; diagnostics redacted', file=sys.stderr)
|
|
return EXIT_SERVER_ERROR
|
|
try:
|
|
server.run(transport="stdio")
|
|
except KeyboardInterrupt:
|
|
return 0
|
|
except Exception:
|
|
print('obliteratus: MCP server stopped; diagnostics redacted', file=sys.stderr)
|
|
return EXIT_SERVER_ERROR
|
|
return 0
|
|
|
|
|
|
def serve_from_namespace(args: argparse.Namespace) -> int:
|
|
"""Build the policy from CLI flags (fail-closed) and serve. Returns exit code."""
|
|
try:
|
|
policy = policy_from_args(args)
|
|
except McpPolicyError as exc:
|
|
print(f"obliteratus: invalid MCP policy: {exc}", file=sys.stderr)
|
|
return EXIT_POLICY_ERROR
|
|
return serve(policy)
|
|
|
|
|
|
def main(argv: list[str] | None = None) -> int:
|
|
"""Entry point for ``python -m obliteratus.mcp_server``."""
|
|
args = build_arg_parser().parse_args(argv)
|
|
return serve_from_namespace(args)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|