Files
hx/tests/fake_m6.py
hermes 99dc90fd3b
ci / check (pull_request) Canceled after 0s
ci / check (push) Canceled after 0s
M6: native MCP client, lazy index, mcp-serve (closes #42-#51)
2026-09-30 15:51:25 +00:00

70 lines
2.8 KiB
Python

"""M6 e2e fake model: discovers an MCP tool from the system-prompt index, describes it, calls it.
turn 1: expect "- f.add:" in system prompt -> mcp_describe(f, add)
turn 2: expect schema in tool result -> mcp_call(f, add, {a:2,b:40})
turn 3: expect "42" -> finish
Usage: python3 tests/fake_m6.py PORT
"""
import http.server, json, sys
LOG = []
def sse(ev, d):
return f"event: {ev}\ndata: {json.dumps(d)}\n\n".encode()
def texts(msg):
out = []
for b in msg["content"]:
if b.get("type") == "text":
out.append(b["text"])
if b.get("type") == "tool_result":
c = b["content"]
if isinstance(c, list):
c = "".join(x.get("text", "") for x in c)
out.append(("ERR:" if b.get("is_error") else "OK:") + c)
return "\n".join(out)
class H(http.server.BaseHTTPRequestHandler):
def log_message(self, *a):
pass
def do_POST(self):
body = json.loads(self.rfile.read(int(self.headers["content-length"])))
system = json.dumps(body.get("system", ""))
tools = [t["name"] for t in body.get("tools", [])]
last = texts(body["messages"][-1])
LOG.append({"last": last, "tools": tools, "index": "- f.add: Add two integers" in system})
n = len(LOG)
if n == 1 and LOG[0]["index"] and "mcp_describe" in tools:
turn = ("mcp_describe", {"server": "f", "tool": "add"})
elif n == 2 and '"required":["a","b"]' in last:
turn = ("mcp_call", {"server": "f", "tool": "add", "arguments": {"a": 2, "b": 40}})
else:
turn = None
self.send_response(200)
self.send_header("content-type", "text/event-stream")
self.end_headers()
w = self.wfile.write
w(sse("message_start", {"message": {"usage": {"input_tokens": 1, "output_tokens": 1}}}))
if turn is None:
w(sse("content_block_start", {"index": 0, "content_block": {"type": "text", "text": ""}}))
w(sse("content_block_delta", {"index": 0, "delta": {"type": "text_delta", "text": "FINISHED " + last[:40]}}))
stop = "end_turn"
else:
w(sse("content_block_start", {"index": 0, "content_block": {"type": "tool_use", "id": f"t{n}", "name": turn[0], "input": {}}}))
w(sse("content_block_delta", {"index": 0, "delta": {"type": "input_json_delta", "partial_json": json.dumps(turn[1])}}))
stop = "tool_use"
w(sse("content_block_stop", {"index": 0}))
w(sse("message_delta", {"delta": {"stop_reason": stop}, "usage": {"output_tokens": 1}}))
w(sse("message_stop", {}))
def do_GET(self):
self.send_response(200)
self.end_headers()
self.wfile.write(json.dumps(LOG).encode())
http.server.ThreadingHTTPServer(("127.0.0.1", int(sys.argv[1])), H).serve_forever()