#!/bin/sh # The `ask` tool over MCP: the question comes back, the answer resumes the turn. # # What it guards: `splinter_say` used to park for the engine's full ask wait # (900 s) when the model asked a question -- the MCP client timed out at 15 s # and the answer could never be delivered, so the turn was lost. The check is # that `say` RETURNS with the question, and that `splinter_answer` then makes # the turn finish with the answer the operator gave. A test that only checked # "the tool is listed" would have passed while that hang was in the code. set -u cd "$(dirname "$0")/.." || exit 1 . tests/sh/port.sh SP=${SPLINTER:-./splinter} D=$(mktemp -d "${TMPDIR:-/var/tmp}/splinter-askmcp-XXXXXX") || exit 1 set -- $(free_ports 2) P=$1 M=$2 F= S= cleanup() { [ -n "$F" ] && kill $F 2>/dev/null; [ -n "$S" ] && kill $S 2>/dev/null rm -rf "$D"; } trap cleanup EXIT INT TERM fail=0 check() { if eval "$2"; then echo "ok $1"; else echo "FAIL $1"; fail=$((fail + 1)); fi; } mkdir -p "$D/home" python3 -I tests/fake_openai_ask.py "$P" "green,blue" > "$D/fake.out" 2>&1 & F=$! if ! wait_listen "$P" "$F"; then echo "FAIL the fake endpoint did not start" cat "$D/fake.out" exit 1 fi HOME=$D/home $SP --no-config --autonomous --model m \ --endpoint "http://127.0.0.1:$P/v1/chat/completions" \ --mcp-server-listen 127.0.0.1 --mcp-listen-port "$M" \ > "$D/srv.out" 2>&1 & S=$! if ! wait_tcp "$M" "$S"; then echo "FAIL the MCP server did not start" cat "$D/srv.out" exit 1 fi python3 - "$M" "$D" <<'PY' || fail=$((fail + 1)) import json, socket, sys, time port, d = int(sys.argv[1]), sys.argv[2] s = socket.create_connection(("127.0.0.1", port), 5) # the client's own timeout is the point: a park longer than this is the bug s.settimeout(15) f = s.makefile("rw", encoding="utf-8") def req(i, method, params=None): o = {"jsonrpc": "2.0", "id": i, "method": method} if params is not None: o["params"] = params f.write(json.dumps(o) + "\n"); f.flush() return json.loads(f.readline()) def call(i, name, args): r = req(i, "tools/call", {"name": name, "arguments": args}) if "error" in r: print("error:", r["error"], file=sys.stderr); sys.exit(1) return r["result"]["content"][0]["text"] def out(line): with open(d + "/result", "a") as fh: fh.write(line + "\n") req(1, "initialize", {}) t0 = time.time() say = call(2, "splinter_say", {"text": "which colour"}) took = time.time() - t0 out("ask_returned %s" % ("yes" if "the model asks" in say else "no")) out("ask_fast %s" % ("yes" if took < 10 else "no")) out("ask_has_question %s" % ("yes" if "colour" in say else "no")) out("ask_names_answer_tool %s" % ("yes" if "splinter_answer" in say else "no")) ans = call(3, "splinter_answer", {"text": "blue"}) out("answer_ok %s" % ("yes" if "answered" in ans else "no")) # the turn must finish on its own now; poll instead of sleeping a fixed time done = False for i in range(4, 40): time.sleep(0.5) if "busy: no" in call(i, "splinter_state", {}): done = True break out("turn_finished %s" % ("yes" if done else "no")) # a second answer with nothing pending is an error, not a silent accept stray = call(90, "splinter_answer", {"text": "red"}) out("stray_refused %s" % ("yes" if "no question is pending" in stray else "no")) PY while read -r key val; do case $key in ask_returned) check 'say returns with the question instead of parking' '[ "$val" = yes ]' ;; ask_fast) check 'say returns in seconds, not at the engine timeout' '[ "$val" = yes ]' ;; ask_has_question) check 'the question text reaches the client' '[ "$val" = yes ]' ;; ask_names_answer_tool) check 'the reply names splinter_answer' '[ "$val" = yes ]' ;; answer_ok) check 'splinter_answer is accepted' '[ "$val" = yes ]' ;; turn_finished) check 'the turn finishes once it is answered' '[ "$val" = yes ]' ;; stray_refused) check 'an answer with no question pending is refused' '[ "$val" = yes ]' ;; esac done < "$D/result" 2>/dev/null # the client died before it wrote anything (a park longer than its timeout): # that is the failure, and it must not be read as "no check failed" [ -s "$D/result" ] || { echo "FAIL the client got no answer at all"; fail=$((fail + 1)); } [ "$fail" -eq 0 ] || { echo "--- server log ---"; cat "$D/srv.out"; exit 1; } echo "test_ask_mcp: ok"