Files
SimpleRemoter/tools/mcp_tools/gen_go_tools.py
T
yuanyuanxianganddeepseek-flash 3f1957a9c9 Feature(Go): MCP endpoint with bearer auth and tools/list
Add a stateless POST /mcp endpoint to the Go server, mounted by the web
server and built from an environment-driven Config in cmd.

The endpoint gates on YAMA_MCP_TOKEN: without it the route is not
registered and /mcp 404s. Requests use a constant-time bearer compare,
and transport failures stay transport failures (401 with a
WWW-Authenticate challenge, 405 for anything but POST, 413 above 8 MiB,
204 for notifications). Everything after a parsed JSON-RPC request is
HTTP 200, including -32600 for batch arrays and -32601 for unknown
methods or tools.

tools/list returns the nine minimal tools verbatim from an embedded
copy of the canonical schemas. tools/mcp_tools/gen_go_tools.py extracts
them from mcp/tools.json, preserving order and only the four published
fields, and its --check mode detects drift. Readonly mode hides and
refuses terminal_exec/terminal_close (-32006); tools that are listed but
not implemented yet answer -32000 until their milestone lands.

Also sync design §3.2 and the M2/M7/M8 plan text to tools.json, which
is the sole schema contract: files not entries, line-based read_file,
no since, camelCase capabilities, max_width [64,1920] defaulting to
1024, and outputSchema included in the M8 subset comparison.

Co-Authored-By: deepseek-flash
2026-11-12 20:35:03 +01:00

105 lines
3.2 KiB
Python

#!/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:]))