#!/usr/bin/env python3 """Generate server/go/mcp/tools_minimal.json from mcp/tools.json. The Go server embeds its tools/list payload, so the nine tools it exposes must be a byte-for-byte copy of the canonical schemas. This script extracts them without hand-editing, preserving tools.json order, and keeps only the four fields MCP publishes: name / description / inputSchema / outputSchema. Usage: python tools/mcp_tools/gen_go_tools.py # rewrite the embedded file python tools/mcp_tools/gen_go_tools.py --check # exit 1 if it is stale Run from anywhere; paths are resolved relative to this file. """ from __future__ import annotations import argparse import json import sys from pathlib import Path # The nine tools of the Go minimal server (design ยง3.1). Order in the embedded # file follows mcp/tools.json, not this list. MINIMAL_TOOLS = ( "list_hosts", "get_host_detail", "get_screenshot", "list_files", "get_audit_log", "get_server_status", "terminal_exec", "terminal_close", "read_file", ) OUTPUT_FIELDS = ("name", "description", "inputSchema", "outputSchema") REPO_ROOT = Path(__file__).resolve().parents[2] TOOLS_JSON = REPO_ROOT / "mcp" / "tools.json" OUTPUT_JSON = REPO_ROOT / "server" / "go" / "mcp" / "tools_minimal.json" def render() -> str: with TOOLS_JSON.open(encoding="utf-8") as f: doc = json.load(f) tools = doc["tools"] if isinstance(doc, dict) else doc by_name = {} for tool in tools: name = tool.get("name") if name in by_name: raise SystemExit(f"duplicate tool name in {TOOLS_JSON}: {name}") by_name[name] = tool missing = [n for n in MINIMAL_TOOLS if n not in by_name] if missing: raise SystemExit(f"tools missing from {TOOLS_JSON}: {', '.join(missing)}") minimal = [] for tool in tools: if tool["name"] not in MINIMAL_TOOLS: continue minimal.append({field: tool[field] for field in OUTPUT_FIELDS}) if len(minimal) != len(MINIMAL_TOOLS): raise SystemExit( f"expected {len(MINIMAL_TOOLS)} tools, got {len(minimal)}" ) return json.dumps(minimal, ensure_ascii=False, indent=2) + "\n" def main(argv: list[str]) -> int: parser = argparse.ArgumentParser(description=__doc__) parser.add_argument( "--check", action="store_true", help="do not write; exit 1 when tools_minimal.json is out of date", ) args = parser.parse_args(argv) want = render() if args.check: try: current = OUTPUT_JSON.read_text(encoding="utf-8") except FileNotFoundError: print(f"FAIL {OUTPUT_JSON} is missing", file=sys.stderr) return 1 if current != want: print( f"FAIL {OUTPUT_JSON} is stale; run gen_go_tools.py", file=sys.stderr, ) return 1 print(f"PASS {OUTPUT_JSON} matches mcp/tools.json") return 0 OUTPUT_JSON.parent.mkdir(parents=True, exist_ok=True) OUTPUT_JSON.write_text(want, encoding="utf-8", newline="\n") print(f"wrote {OUTPUT_JSON} ({len(MINIMAL_TOOLS)} tools)") return 0 if __name__ == "__main__": sys.exit(main(sys.argv[1:]))