main / bench/core/claude_mcp.py · 2396 bytes · raw
"""Stdio MCP server that relays an instrument's tools to the bench process (claude_cli.py).
claude -p starts it from --mcp-config. It lists the tools the bridge serves and forwards every
call to it, so the tools run in the bench process on the attempt's Workspace, exactly as they do
for HTTP lanes. Stdlib only, newline-delimited JSON-RPC 2.0 on stdin/stdout.
env BENCH_BRIDGE_URL http://127.0.0.1:<port> of the attempt's bridge
BENCH_BRIDGE_TOKEN shared secret, sent as X-Bench-Token
"""
import json
import os
import sys
import urllib.request
URL = os.environ.get("BENCH_BRIDGE_URL", "")
TOKEN = os.environ.get("BENCH_BRIDGE_TOKEN", "")
def bridge(path, payload=None):
data = None if payload is None else json.dumps(payload).encode()
req = urllib.request.Request(URL + path, data=data, headers={
"X-Bench-Token": TOKEN, "Content-Type": "application/json"})
with urllib.request.urlopen(req, timeout=3600) as r:
return json.load(r)
def handle(method, params):
if method == "initialize":
return {"protocolVersion": params.get("protocolVersion") or "2025-06-18",
"capabilities": {"tools": {"listChanged": False}},
"serverInfo": {"name": "llm-bench", "version": "1"}}
if method == "tools/list":
return {"tools": bridge("/tools")}
if method == "tools/call":
r = bridge("/call", {"name": params.get("name"), "arguments": params.get("arguments") or {}})
return {"content": [{"type": "text", "text": r["text"]}], "isError": False}
if method == "ping":
return {}
raise LookupError(method)
def main():
for line in sys.stdin:
if not line.strip():
continue
msg = json.loads(line)
if "id" not in msg:
continue # a notification (notifications/initialized, cancelled ...)
out = {"jsonrpc": "2.0", "id": msg["id"]}
try:
out["result"] = handle(msg.get("method"), msg.get("params") or {})
except LookupError:
out["error"] = {"code": -32601, "message": "method not found: %s" % msg.get("method")}
except Exception as e: # noqa: BLE001 — report it to the client, keep serving
out["error"] = {"code": -32603, "message": repr(e)[:500]}
sys.stdout.write(json.dumps(out) + "\n")
sys.stdout.flush()
if __name__ == "__main__":
main()