Once told to stop, the tool runner makes no new ask of a bundle (a call after the stop is refused), waits for the asks in flight within the drain bound (30 s by default), cuts each still in flight at the bound with a journal line naming the bundle, the verb and the call's age, leaves its caller unanswered (a transport fault), and only then stops its bundles itself. Builds: design 47 H2 §813 (12), H-A-342 (2), H-A-353
52 lines
2.7 KiB
Python
Executable File
52 lines
2.7 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
# A tools bundle for the tool runner's whole test (design 47 H §774): MCP over stdio, no SDK. A tool `who`, the seat node-desk's verbs
|
|
# `who` (answers at once) and `slow` (answers after three seconds), and a tool `reach` that asks the seat verb node-desk.fresh as the
|
|
# bundle does, through the runtime's `mesh/ask`, and says what came back; a tool `hold`, which answers after the seconds it is given,
|
|
# beside other calls (design 47 H2 §813 (12)).
|
|
import json, os, sys, threading, time
|
|
|
|
LOCK = threading.Lock()
|
|
|
|
def say(m):
|
|
m["jsonrpc"] = "2.0"
|
|
with LOCK:
|
|
sys.stdout.write(json.dumps(m) + "\n"); sys.stdout.flush()
|
|
|
|
def text(rid, value):
|
|
say({"id": rid, "result": {"content": [{"type": "text", "text": json.dumps(value)}]}})
|
|
|
|
TOOLS = [
|
|
{"name": "who", "description": "the module's own tool", "inputSchema": {"type": "object", "properties": {}}},
|
|
{"name": "reach", "description": "asks the seat verb node-desk.fresh", "inputSchema": {"type": "object", "properties": {}}},
|
|
{"name": "hold", "description": "answers after the seconds it is given, beside other calls", "inputSchema": {"type": "object", "properties": {"seconds": {"type": "number"}}}},
|
|
{"name": "node-desk.who", "description": "the seat's verb", "inputSchema": {"type": "object", "properties": {}}},
|
|
{"name": "node-desk.slow", "description": "the seat's verb that answers late", "inputSchema": {"type": "object", "properties": {}}},
|
|
]
|
|
for line in sys.stdin:
|
|
req = json.loads(line); rid = req.get("id"); m = req.get("method"); p = req.get("params") or {}
|
|
if m == "initialize":
|
|
say({"id": rid, "result": {"protocolVersion": "2025-03-26", "capabilities": {"tools": {}}, "serverInfo": {"name": "ledger", "version": "1"}}})
|
|
elif m == "tools/list":
|
|
say({"id": rid, "result": {"tools": TOOLS}})
|
|
elif m == "tools/call":
|
|
name = p.get("name")
|
|
if name == "hold":
|
|
seconds = (p.get("arguments") or {}).get("seconds", 0)
|
|
with open(os.environ["HELD_FILE"], "a") as noted:
|
|
noted.write("hold %s\n" % seconds)
|
|
threading.Thread(target=lambda r=rid, n=seconds: (time.sleep(n), text(r, {"held": n})), daemon=True).start()
|
|
continue
|
|
if name == "node-desk.slow":
|
|
time.sleep(3)
|
|
if name == "reach":
|
|
say({"id": "q1", "method": "mesh/ask", "params": {"key": "seat:node-desk.fresh", "body": {}}})
|
|
for answer in sys.stdin:
|
|
a = json.loads(answer)
|
|
if a.get("id") == "q1":
|
|
text(rid, a.get("result") if "error" not in a else {"error": a["error"]})
|
|
break
|
|
else:
|
|
text(rid, {"tool": name})
|
|
elif rid is not None:
|
|
say({"id": rid, "result": {}})
|