|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Read-only MCP tool plane for Sopify protocol state. |
| 3 | +
|
| 4 | +S1 intentionally exposes only deterministic read/check operations. Workflow |
| 5 | +decisions, checkpoint confirmation, installer setup, and all state writes stay |
| 6 | +with the host prompt, CLI, and sopify_writer. |
| 7 | +""" |
| 8 | + |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +import sys |
| 12 | +from pathlib import Path |
| 13 | +from typing import Any, Callable |
| 14 | + |
| 15 | +REPO_ROOT = Path(__file__).resolve().parents[1] |
| 16 | +if str(REPO_ROOT) not in sys.path: |
| 17 | + sys.path.insert(0, str(REPO_ROOT)) |
| 18 | + |
| 19 | +from scripts.sopify_protocol_check import run_protocol_check # noqa: E402 |
| 20 | +from sopify_writer import ProtocolStore # noqa: E402 |
| 21 | + |
| 22 | +MCP_DEPENDENCY = "mcp[cli]>=1.27,<2" |
| 23 | + |
| 24 | + |
| 25 | +def resolve_workspace_root(workspace_root: str | Path) -> Path: |
| 26 | + """Resolve and validate the caller-provided workspace root.""" |
| 27 | + candidate = Path(workspace_root).expanduser().resolve() |
| 28 | + if not candidate.exists(): |
| 29 | + raise ValueError(f"workspace_root does not exist: {candidate}") |
| 30 | + if not candidate.is_dir(): |
| 31 | + raise ValueError(f"workspace_root is not a directory: {candidate}") |
| 32 | + return candidate |
| 33 | + |
| 34 | + |
| 35 | +def sopify_root_for(workspace_root: str | Path) -> Path: |
| 36 | + return resolve_workspace_root(workspace_root) / ".sopify" |
| 37 | + |
| 38 | + |
| 39 | +def read_active_plan(workspace_root: str | Path) -> dict[str, Any] | None: |
| 40 | + """Return state/active_plan.json through ProtocolStore, or null.""" |
| 41 | + return ProtocolStore(sopify_root_for(workspace_root)).get_active_plan() |
| 42 | + |
| 43 | + |
| 44 | +def read_current_handoff(workspace_root: str | Path) -> dict[str, Any] | None: |
| 45 | + """Return state/current_handoff.json through ProtocolStore, or null.""" |
| 46 | + handoff = ProtocolStore(sopify_root_for(workspace_root)).get_current_handoff() |
| 47 | + return handoff.to_dict() if handoff is not None else None |
| 48 | + |
| 49 | + |
| 50 | +def workspace_status_lite(workspace_root: str | Path) -> dict[str, Any]: |
| 51 | + """Return a minimal, dependency-light Sopify workspace status.""" |
| 52 | + workspace = resolve_workspace_root(workspace_root) |
| 53 | + sopify_root = workspace / ".sopify" |
| 54 | + state_root = sopify_root / "state" |
| 55 | + active_plan = read_active_plan(workspace) if sopify_root.exists() else None |
| 56 | + active_plan_id = active_plan.get("plan_id") if isinstance(active_plan, dict) else None |
| 57 | + active_plan_dir = sopify_root / "plan" / str(active_plan_id) if active_plan_id else None |
| 58 | + |
| 59 | + return { |
| 60 | + "workspace_root": str(workspace), |
| 61 | + "sopify_exists": sopify_root.is_dir(), |
| 62 | + "paths": { |
| 63 | + "blueprint": (sopify_root / "blueprint").is_dir(), |
| 64 | + "plan": (sopify_root / "plan").is_dir(), |
| 65 | + "history": (sopify_root / "history").is_dir(), |
| 66 | + "state": state_root.is_dir(), |
| 67 | + }, |
| 68 | + "active_plan": active_plan, |
| 69 | + "active_plan_dir_exists": active_plan_dir.is_dir() if active_plan_dir else None, |
| 70 | + "handoff_exists": (state_root / "current_handoff.json").is_file(), |
| 71 | + } |
| 72 | + |
| 73 | + |
| 74 | +def protocol_check(workspace_root: str | Path, scenario: str) -> dict[str, Any]: |
| 75 | + return run_protocol_check(workspace_root, scenario) |
| 76 | + |
| 77 | + |
| 78 | +def _tool_error(exc: Exception) -> dict[str, str]: |
| 79 | + return { |
| 80 | + "code": type(exc).__name__, |
| 81 | + "message": str(exc), |
| 82 | + } |
| 83 | + |
| 84 | + |
| 85 | +def _safe_tool(key: str, fn: Callable[..., Any], *args: Any) -> dict[str, Any]: |
| 86 | + try: |
| 87 | + return {key: fn(*args), "error": None} |
| 88 | + except Exception as exc: |
| 89 | + return {key: None, "error": _tool_error(exc)} |
| 90 | + |
| 91 | + |
| 92 | +def get_mcp_dependency_hint() -> str: |
| 93 | + return f'Install the stable MCP Python SDK with: python3 -m pip install "{MCP_DEPENDENCY}"' |
| 94 | + |
| 95 | + |
| 96 | +def create_mcp_server() -> Any: |
| 97 | + """Create the FastMCP server lazily so tests can run without the SDK.""" |
| 98 | + try: |
| 99 | + from mcp.server.fastmcp import FastMCP |
| 100 | + except ModuleNotFoundError as exc: |
| 101 | + raise RuntimeError(get_mcp_dependency_hint()) from exc |
| 102 | + |
| 103 | + server = FastMCP("sopify", json_response=True) |
| 104 | + |
| 105 | + @server.tool(name="sopify.get_active_plan") |
| 106 | + def tool_get_active_plan(workspace_root: str) -> dict[str, Any]: |
| 107 | + """Read Sopify state/active_plan.json for a workspace.""" |
| 108 | + return _safe_tool("active_plan", read_active_plan, workspace_root) |
| 109 | + |
| 110 | + @server.tool(name="sopify.get_current_handoff") |
| 111 | + def tool_get_current_handoff(workspace_root: str) -> dict[str, Any]: |
| 112 | + """Read Sopify state/current_handoff.json for a workspace.""" |
| 113 | + return _safe_tool("current_handoff", read_current_handoff, workspace_root) |
| 114 | + |
| 115 | + @server.tool(name="sopify.workspace_status_lite") |
| 116 | + def tool_workspace_status_lite(workspace_root: str) -> dict[str, Any]: |
| 117 | + """Inspect only the lightweight .sopify/ workspace structure.""" |
| 118 | + return _safe_tool("status", workspace_status_lite, workspace_root) |
| 119 | + |
| 120 | + @server.tool(name="sopify.protocol_check") |
| 121 | + def tool_protocol_check(workspace_root: str, scenario: str) -> dict[str, Any]: |
| 122 | + """Run the Sopify protocol checker for new-plan, continuation, or finalize.""" |
| 123 | + return _safe_tool("protocol_check", protocol_check, workspace_root, scenario) |
| 124 | + |
| 125 | + return server |
| 126 | + |
| 127 | + |
| 128 | +def main() -> int: |
| 129 | + try: |
| 130 | + create_mcp_server().run(transport="stdio") |
| 131 | + except RuntimeError as exc: |
| 132 | + print(str(exc), file=sys.stderr) |
| 133 | + return 2 |
| 134 | + return 0 |
| 135 | + |
| 136 | + |
| 137 | +if __name__ == "__main__": |
| 138 | + raise SystemExit(main()) |
0 commit comments