Files
hx/crates/hx-mcp/tests/fake_mcp.py
T
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

93 lines
4.4 KiB
Python

"""Tiny MCP server for tests. stdio (default) or streamable HTTP: `fake_mcp.py http PORT`.
Tools: echo(message), add(a,b), crash() (exits the process), big() (large output), slow(). One prompt, one resource.
"""
import json, sys, time
TOOLS = [
{"name": "echo", "description": "Echo a message back\nsecond line ignored in index",
"inputSchema": {"type": "object", "properties": {"message": {"type": "string"}}, "required": ["message"]}},
{"name": "add", "description": "Add two integers",
"inputSchema": {"type": "object", "properties": {"a": {"type": "integer"}, "b": {"type": "integer"}}, "required": ["a", "b"]}},
{"name": "crash", "description": "Kill the server", "inputSchema": {"type": "object", "properties": {}}},
{"name": "big", "description": "Large output", "inputSchema": {"type": "object", "properties": {}}},
{"name": "slow", "description": "Sleeps 5s", "inputSchema": {"type": "object", "properties": {}}},
]
def handle(m):
meth, p, i = m.get("method"), m.get("params") or {}, m.get("id")
if i is None:
return None
r = None
if meth == "initialize":
r = {"protocolVersion": p.get("protocolVersion"), "serverInfo": {"name": "fake", "version": "1"},
"capabilities": {"tools": {}, "prompts": {}, "resources": {}}, "instructions": "Use echo for tests."}
elif meth == "tools/list":
# paginate: first page 2 tools
if p.get("cursor") == "p2":
r = {"tools": TOOLS[2:]}
else:
r = {"tools": TOOLS[:2], "nextCursor": "p2"}
elif meth == "tools/call":
n, a = p["name"], p.get("arguments") or {}
if n == "echo":
r = {"content": [{"type": "text", "text": "echo: " + a["message"]}]}
elif n == "add":
r = {"content": [{"type": "text", "text": str(a["a"] + a["b"])}]}
elif n == "crash":
sys.exit(3)
elif n == "big":
r = {"content": [{"type": "text", "text": "x" * 100000}]}
elif n == "slow":
time.sleep(5)
r = {"content": [{"type": "text", "text": "late"}]}
else:
r = {"content": [{"type": "text", "text": "no such tool"}], "isError": True}
elif meth == "prompts/list":
r = {"prompts": [{"name": "review", "description": "Review a file", "arguments": [{"name": "path", "required": True}]}]}
elif meth == "prompts/get":
r = {"messages": [{"role": "user", "content": {"type": "text", "text": "Please review " + p["arguments"].get("path", "?")}}]}
elif meth == "resources/list":
r = {"resources": [{"uri": "mem://notes", "name": "notes"}]}
elif meth == "resources/read":
r = {"contents": [{"uri": p["uri"], "text": "note body"}]}
else:
return {"jsonrpc": "2.0", "id": i, "error": {"code": -32601, "message": "method not found"}}
return {"jsonrpc": "2.0", "id": i, "result": r}
if len(sys.argv) > 1 and sys.argv[1] == "http":
import http.server
class H(http.server.BaseHTTPRequestHandler):
def log_message(self, *a):
pass
def do_POST(self):
if self.headers.get("authorization") != "Bearer t0k":
self.send_response(401); self.end_headers(); return
m = json.loads(self.rfile.read(int(self.headers["content-length"])))
if m.get("method") != "initialize" and self.headers.get("mcp-session-id") != "s1":
self.send_response(400); self.end_headers(); return
r = handle(m)
if r is None:
self.send_response(202); self.end_headers(); return
self.send_response(200)
self.send_header("mcp-session-id", "s1")
# answer tools/call as SSE, everything else as JSON
if m.get("method") == "tools/call":
self.send_header("content-type", "text/event-stream"); self.end_headers()
self.wfile.write(b'event: message\ndata: {"jsonrpc":"2.0","method":"notifications/progress","params":{}}\n\n')
self.wfile.write(("event: message\ndata: " + json.dumps(r) + "\n\n").encode())
else:
self.send_header("content-type", "application/json"); self.end_headers()
self.wfile.write(json.dumps(r).encode())
http.server.ThreadingHTTPServer(("127.0.0.1", int(sys.argv[2])), H).serve_forever()
else:
for line in sys.stdin:
r = handle(json.loads(line))
if r is not None:
sys.stdout.write(json.dumps(r) + "\n")
sys.stdout.flush()