garage44.eu/jeroen/midas

logclone: git clone https://garage44.eu/jeroen/midas.git

.. /tests/e2e/client.py

raw

#!/usr/bin/env python3
"""An MCP client for the end-to-end test: runs the command an agent's MCP
configuration names (`midas bot mcp`), and does what a client does — initialize,
list the tools, call two — insisting that every line on its stdout is JSON-RPC.

    python3 client.py EXPECTED_TOKEN_SHA256_PREFIX -- midas bot mcp
"""
import json, subprocess, sys

want_sha = sys.argv[1]
cmd = sys.argv[sys.argv.index("--") + 1:]
p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, text=True, bufsize=1)

def send(msg):
    p.stdin.write(json.dumps(msg) + "\n")
    p.stdin.flush()

def recv(mid):
    while True:
        line = p.stdout.readline()
        if not line:
            sys.exit(f"client: stdout ended waiting for id {mid}")
        try:
            msg = json.loads(line)  # anything else on stdout breaks MCP
        except ValueError:
            sys.exit(f"client: not JSON on stdout: {line!r}")
        if msg.get("id") == mid:
            return msg

send({"jsonrpc": "2.0", "id": 1, "method": "initialize", "params": {
    "protocolVersion": "2025-06-18", "capabilities": {}, "clientInfo": {"name": "e2e", "version": "0"}}})
init = recv(1)
assert init["result"]["serverInfo"]["name"] == "fakebot", init
send({"jsonrpc": "2.0", "method": "notifications/initialized"})
send({"jsonrpc": "2.0", "id": 2, "method": "tools/list"})
names = [t["name"] for t in recv(2)["result"]["tools"]]
assert names == ["echo", "token"], names
send({"jsonrpc": "2.0", "id": 3, "method": "tools/call", "params": {"name": "echo", "arguments": {"text": "hello over ssh"}}})
echo = recv(3)["result"]["content"][0]["text"]
assert echo == "echo: hello over ssh", echo
send({"jsonrpc": "2.0", "id": 4, "method": "tools/call", "params": {"name": "token", "arguments": {}}})
tok = recv(4)["result"]["content"][0]["text"]
assert tok.endswith(f"sha256 {want_sha}"), f"the bot did not get the owner's secret: {tok}"
p.stdin.close()
rest = p.stdout.read()
assert rest.strip() == "", f"left on stdout: {rest!r}"
p.wait(timeout=20)
print(f"client: initialize, tools/list {names}, {echo!r}, token: {tok}")