Files
mesh-tools/node-tools/test/fixtures/whole-bundle.py
T
jschoubben a1c12af8b2 T1 page by cursor: a state page continues from an opaque cursor in sequence order
mesh/state.page reads a state from an ordered watch starting after the cursor's sequence, never a
replay of the bucket per page: each kept message once across the pages of a read, a key's older
kept revisions included, at most 768 KB a page, next the opaque cursor, empty at the end. A value
written after the read reached its bucket comes marked seen-again (the cursor carries that mark;
the runner keeps nothing per read); a deleted key gets no entry. A state kept one bucket per node is
read one node's bucket after another.

Builds: design 47 H2 §779, H-A-254, H-A-382
2026-11-22 00:10:01 +01:00

69 lines
3.9 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)); a tool `page`, which reads one page of its state `pages` after the cursor it is given,
# through the runtime's `mesh/state.page`, and says the keys, those seen again and the next cursor (design 47 H2 §779).
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": "page", "description": "one page of the state pages after a cursor", "inputSchema": {"type": "object", "properties": {"cursor": {"type": "string"}}}},
{"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 == "page":
cursor = (p.get("arguments") or {}).get("cursor", "")
say({"id": "p1", "method": "mesh/state.page", "params": {"state": "pages", "cursor": cursor}})
for answer in sys.stdin:
a = json.loads(answer)
if a.get("id") == "p1":
if "error" in a:
text(rid, {"error": a["error"]})
else:
r = a.get("result") or {}
es = r.get("entries") or []
text(rid, {"keys": [e["key"] for e in es if not e.get("seen-again")],
"again": [e["key"] for e in es if e.get("seen-again")], "next": r.get("next", "")})
break
continue
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": {}})