M6: native MCP client, lazy index, mcp-serve (closes #42-#51)
This commit is contained in:
1 parent
6d35274f39
commit
99dc90fd3b
18 files changed
+1723
-22
No files matched your search
+4
-1
@@ -6,7 +6,9 @@ set -e
|
||||
cargo fmt --all --check
|
||||
cargo clippy -q --workspace --all-targets -- -D warnings
|
||||
log=$(mktemp)
|
||||
cargo test -q --workspace >"$log" 2>&1 || { grep -E 'FAILED|panicked' -A4 "$log"; exit 1; }
|
||||
# rust-analyzer test runs alone: under parallel cargo load its flycheck can publish stale results (see issue)
|
||||
cargo test -q --workspace -- --skip rust_analyzer_turn_end >"$log" 2>&1 || { grep -E 'FAILED|panicked' -A4 "$log"; exit 1; }
|
||||
cargo test -q -p hx-lens rust_analyzer_turn_end >>"$log" 2>&1 || { grep -E 'FAILED|panicked' -A4 "$log"; exit 1; }
|
||||
grep -oE 'ok. [0-9]+ passed' "$log" | awk '{s+=$2} END {print s" unit tests ok"}'
|
||||
cargo build -q
|
||||
sh tests/e2e.sh
|
||||
@@ -14,3 +16,4 @@ python3 tests/tui_smoke.py | grep -q FAIL && { echo "tui smoke FAIL"; exit 1; }
|
||||
sh tests/lens_e2e.sh
|
||||
sh tests/m4_e2e.sh
|
||||
sh tests/m5_e2e.sh
|
||||
sh tests/m6_e2e.sh
|
||||
@@ -0,0 +1,69 @@
|
||||
"""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()
|
||||
@@ -0,0 +1,49 @@
|
||||
#!/bin/sh
|
||||
# M6 e2e: model discovers an MCP tool via the lazy index, describes and calls it; `hx mcp` reports costs;
|
||||
# `hx mcp-serve` answers another MCP client (hx itself, configured as a server) with lens tools.
|
||||
set -eu
|
||||
root=$(cd "$(dirname "$0")/.." && pwd)
|
||||
tmp=$(mktemp -d)
|
||||
trap 'kill $srv 2>/dev/null; rm -rf "$tmp"' EXIT
|
||||
port=$((20000 + $$ % 20000))
|
||||
python3 "$root/tests/fake_m6.py" $port & srv=$!
|
||||
mkdir -p "$tmp/home/.hx" "$tmp/repo/.hx"
|
||||
echo '{"claude":{"access":"fake-token","refresh":"r","expires":99999999999999}}' > "$tmp/home/.hx/auth.json"
|
||||
printf '[lsp]\nmode = "off"\ntests = "off"\n' > "$tmp/home/.hx/config.toml"
|
||||
cat > "$tmp/repo/.hx/mcp.json" <<EOF
|
||||
{"mcpServers": {"f": {"command": "python3", "args": ["$root/crates/hx-mcp/tests/fake_mcp.py"], "eager": false}}}
|
||||
EOF
|
||||
sleep 0.5
|
||||
cd "$tmp/repo"
|
||||
export HOME="$tmp/home" HX_BASE_URL="http://127.0.0.1:$port"
|
||||
"$root/target/debug/hx" -p "sum" > "$tmp/out" 2> "$tmp/err" || { cat "$tmp/err"; exit 1; }
|
||||
grep -q "FINISHED OK:42" "$tmp/out" || { echo "--- out"; cat "$tmp/out"; echo "--- log"; curl -s "http://127.0.0.1:$port/"; echo "--- err"; cat "$tmp/err"; exit 1; }
|
||||
"$root/target/debug/hx" mcp > "$tmp/st"
|
||||
grep -q "f: 5 tools.*lazy (index ~[0-9]* tok, eager schemas ~[0-9]* tok)" "$tmp/st" || { cat "$tmp/st"; exit 1; }
|
||||
# mcp-serve: hx as MCP server, consumed by hx as MCP client
|
||||
printf 'fn main() {}\n' > main.rs
|
||||
cat > "$tmp/repo/.hx/mcp.json" <<EOF
|
||||
{"mcpServers": {"hx": {"command": "$root/target/debug/hx", "args": ["mcp-serve"], "eager": false}}}
|
||||
EOF
|
||||
"$root/target/debug/hx" mcp > "$tmp/st2"
|
||||
grep -q "hx.lsp: Language server queries" "$tmp/st2" || { cat "$tmp/st2"; exit 1; }
|
||||
# a foreign client (python, raw JSON-RPC) gets pyright diagnostics through hx mcp-serve
|
||||
if command -v pyright-langserver >/dev/null; then
|
||||
printf 'x: int = "no"\n' > bad.py
|
||||
python3 - "$root/target/debug/hx" > "$tmp/diag" <<'PY'
|
||||
import json, subprocess, sys
|
||||
p = subprocess.Popen([sys.argv[1], "mcp-serve"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, text=True)
|
||||
def rpc(i, m, params):
|
||||
p.stdin.write(json.dumps({"jsonrpc": "2.0", "id": i, "method": m, "params": params}) + "\n"); p.stdin.flush()
|
||||
while True:
|
||||
r = json.loads(p.stdout.readline())
|
||||
if r.get("id") == i:
|
||||
return r
|
||||
rpc(1, "initialize", {"protocolVersion": "2025-06-18", "capabilities": {}, "clientInfo": {"name": "t", "version": "0"}})
|
||||
r = rpc(2, "tools/call", {"name": "lsp", "arguments": {"action": "diagnostics", "path": "bad.py"}})
|
||||
print(r["result"]["content"][0]["text"])
|
||||
p.stdin.close(); p.wait(timeout=30)
|
||||
PY
|
||||
grep -q 'error.*not assignable' "$tmp/diag" || { echo "--- diag"; cat "$tmp/diag"; exit 1; }
|
||||
fi
|
||||
echo "m6 e2e ok"
|
||||
Reference in new issue
Block a user