#!/bin/sh
# The MCP client against a fake MCP server (test/pic/fake_mcp.c) over streamable HTTP: pic mcp add
# in Pi's and Claude Code's argument shapes and as a pasted mcpServers snippet; the current
# revision (2026-07-28) first and the 2025 handshake as the fallback, JSON and SSE answers; the
# server's tools registered as mcp__SERVER__TOOL and called through a real request, journaled
# like any tool's, with PostToolUse hooks on their results; replay safety from readOnlyHint, so a
# crash during a write is "outcome unknown" and never run again; errors, a timeout, a server
# that dies mid-call and a long result; headers sealed and never shown or journaled.
# --no-faults (the release build, which has no crash points) skips the crash case.
set -eu
PIC=${PIC:?set PIC to the pic binary}
MCP=${MCP:-$(dirname "$PIC")/../fake_mcp}
SQLITE3=${SQLITE3:-sqlite3}
FAULTS=yes
[ "${1:-}" = --no-faults ] && FAULTS=no
T=$(mktemp -d "${TMPDIR:-/tmp}/pic-mcp.XXXXXX")
T=$(cd "$T" && pwd -P)
pids=
stop() { for p in $pids; do kill "$p" 2>/dev/null && wait "$p" 2>/dev/null || :; done; pids=; }
cleanup() { stop; rm -rf -- "$T"; }
trap cleanup EXIT HUP INT TERM
fail() { printf 'FAIL: %s\n' "$*" >&2; exit 1; }
unset PIC_TEST_PASSPHRASE
mkdir "$T/home" "$T/ws"
export HOME="$T/home" PIC_MODEL_CATALOG=offline PIC_PI_EXPORT=0 PIC_NO_KEY_CHECKS=1
DB="$T/j"
pic() { "$PIC" --db "$DB" --workspace "$T/ws" "$@"; }
# serve NAME ERA [sse]: a fresh fake MCP server, its requests in $T/NAME/k.req, its URL in $url
serve() {
name=$1
shift
mkdir "$T/$name"
"$MCP" "$T/$name.port" "$T/$name" 120 "$@" &
pids="$pids $!"
i=0
while [ ! -s "$T/$name.port" ]; do i=$((i + 1)); [ "$i" -lt 600 ] || fail "server $name"; sleep 0.05; done
url="http://127.0.0.1:$(cat "$T/$name.port")/mcp"
}
fresh() { rm -f "$DB" "$DB-wal" "$DB-shm"; }
declared() { "$SQLITE3" "$DB" "SELECT group_concat(json_extract(value,'\$.name'),' ') FROM pic_session, json_each(tools)"; }
iserr() { "$SQLITE3" "$DB" "SELECT group_concat(is_error) FROM pic_effect WHERE tool = '$1'"; }
ledger() { if [ -f "$T/$1/ledger" ]; then wc -l < "$T/$1/ledger" | tr -d ' '; else echo 0; fi; }
SECRET=sk-mcp-SECRET-7f3a9c
# ---- refusals: nothing is saved ---------------------------------------------------------------
pic mcp add bad/name --url http://127.0.0.1:1/mcp 2> "$T/e" && fail "a name with a slash was saved"
grep -q "letters, digits" "$T/e" || fail "name check: $(cat "$T/e")"
pic mcp add --transport sse old http://127.0.0.1:1/sse 2> "$T/e" && fail "an SSE server was saved"
grep -q "streamable HTTP" "$T/e" || fail "sse refusal: $(cat "$T/e")"
pic mcp add -s project proj http://127.0.0.1:1/mcp 2> "$T/e" && fail "a project-scoped server was saved"
grep -q "project" "$T/e" || fail "project refusal: $(cat "$T/e")"
pic mcp add far --url http://example.com/mcp 2> "$T/e" && fail "plain http to another host was saved"
pic mcp list > "$T/l" 2>&1 || :
grep -q "No MCP servers" "$T/l" || fail "a refused add saved something: $(cat "$T/l")"
printf 'PASS: bad names, the old SSE transport, project scope and plain remote http are refused\n'
# ---- Pi's shape, a 2025-only server: the current revision is tried first, then the handshake ----
serve old legacy
pic mcp add old --url "$url" --description "An old server" > "$T/o" 2> "$T/e" || fail "add old: $(cat "$T/e")"
grep -q "Added old" "$T/o" || fail "add old said: $(cat "$T/o")"
grep -q "7 tools" "$T/o" || fail "add old tools: $(cat "$T/o")"
grep -q '"method":"server/discover"' "$T/old/1.req" || fail "the current revision was not tried first"
grep -qi '^MCP-Protocol-Version: 2026-07-28' "$T/old/1.req" || fail "discover header: $(cat "$T/old/1.req")"
grep -q '"io.modelcontextprotocol/protocolVersion":"2026-07-28"' "$T/old/1.req" || fail "discover _meta"
grep -q '"method":"initialize"' "$T/old/2.req" || fail "no fallback to initialize: $(cat "$T/old/2.req")"
grep -q '"method":"notifications/initialized"' "$T/old/3.req" || fail "no initialized notification"
grep -qi '^Mcp-Session-Id: sess-1' "$T/old/4.req" || fail "the session id was not sent: $(cat "$T/old/4.req")"
grep -q '"cursor":"p2"' "$T/old"/*.req || fail "the second page of tools was not asked for"
pic mcp list > "$T/l" 2>&1 || fail "list: $(cat "$T/l")"
grep -q "^old *http *2025-11-25 *7 tools" "$T/l" || fail "list old: $(cat "$T/l")"
printf 'PASS: pic mcp add in Pi'"'"'s shape; a 2025 server is reached through the handshake\n'
# ---- Claude Code's shape, a current server answering with SSE, with a sealed header -----------
serve cur modern sse
pic mcp add --transport http cur "$url" -H "Authorization: Bearer $SECRET" > "$T/o" 2> "$T/e" ||
fail "add cur: $(cat "$T/e")"
grep -q "7 tools" "$T/o" || fail "add cur tools: $(cat "$T/o")"
grep -qi "^Authorization: Bearer $SECRET" "$T/cur/1.req" || fail "the header was not sent: $(cat "$T/cur/1.req")"
grep -q '"method":"initialize"' "$T/cur"/*.req && fail "a current server was sent initialize"
grep -qi '^Mcp-Method: tools/list' "$T/cur/2.req" || fail "Mcp-Method: $(cat "$T/cur/2.req")"
pic mcp list > "$T/l" 2>&1 || fail "list: $(cat "$T/l")"
grep -q "^cur *http *2026-07-28 *7 tools" "$T/l" || fail "list cur: $(cat "$T/l")"
# the same shapes again: a URL with options after it, and a pasted snippet
pic mcp add -t http snip2 "$url" --header "X-Team: blue" > /dev/null 2>&1 || fail "add with a header after the URL"
pic mcp get snip2 | grep -q "X-Team" || fail "get snip2: $(pic mcp get snip2)"
pic mcp add "{\"mcpServers\":{\"snip\":{\"url\":\"$url\"}}}" > "$T/o" 2>&1 || fail "add snippet: $(cat "$T/o")"
grep -q "Added snip" "$T/o" || fail "snippet said: $(cat "$T/o")"
pic mcp add-json snip3 "{\"type\":\"http\",\"url\":\"$url\"}" > /dev/null 2>&1 || fail "add-json"
for s in snip snip2 snip3; do pic mcp remove "$s" > /dev/null || fail "remove $s"; done
printf 'PASS: pic mcp add in Claude Code'"'"'s shape and as a snippet; SSE answers are read\n'
# ---- a secret is never shown or kept in plain text --------------------------------------------
pic mcp list > "$T/l" 2>&1
pic mcp get cur > "$T/g" 2>&1 || fail "get cur: $(cat "$T/g")"
grep -q "Authorization" "$T/g" || fail "get does not name the header: $(cat "$T/g")"
grep -q "sealed" "$T/g" || fail "get does not say the header is sealed: $(cat "$T/g")"
grep -c "$SECRET" "$T/l" "$T/g" "$T/o" "$T/e" | grep -qv ':0$' && fail "the secret was printed"
cat "$HOME/.pic/pic.sqlite"* | grep -c "$SECRET" > /dev/null && fail "the secret is in pic.sqlite in plain text"
printf 'PASS: headers are sealed and never printed\n'
# ---- tools reach a request, run through it, and are journaled -----------------------------------
fresh
printf 'tool raw mcp__cur__echo {"text":"hi"}\traw mcp__old__write_note {"note":"n1"}\ntext done\n' > "$T/s1"
pic submit job 'use the servers' > /dev/null
pic run --script "$T/s1" > /dev/null 2> "$T/e" || fail "run: $(cat "$T/e")"
declared | grep -q 'mcp__cur__echo' || fail "not declared: $(declared)"
declared | grep -q 'mcp__old__write_note' || fail "old not declared: $(declared)"
pic dump > "$T/d"
grep -q '^effect 1 .* mcp__cur__echo done safe=1 ' "$T/d" || fail "echo effect: $(grep '^effect' "$T/d")"
grep -q '^effect 2 .* mcp__old__write_note done safe=0 ' "$T/d" || fail "note effect: $(grep '^effect' "$T/d")"
grep -q 'echo: {\\"text\\":\\"hi\\"}' "$T/d" || grep -q 'echo: {"text":"hi"}' "$T/d" || fail "echo result: $(cat "$T/d")"
grep -q '^submission 1 job answered ' "$T/d" || fail "not answered"
[ "$(ledger old)" = 1 ] || fail "write_note ran $(ledger old) times"
grep -qi "^Mcp-Name: echo" "$T/cur"/*.req || fail "Mcp-Name header"
grep -c "$SECRET" "$T/d" "$DB" "$DB-wal" 2>/dev/null | grep -qv ':0$' && fail "the secret reached the journal"
printf 'PASS: server tools are declared, called through a request and journaled; secrets stay out\n'
# ---- errors, a timeout, a dying server and a long answer -----------------------------------------
fresh
printf 'tool raw mcp__cur__fail {}\traw mcp__cur__boom {}\traw mcp__cur__big {}\ntext done\n' > "$T/s2"
pic submit job 'errors' > /dev/null
pic run --script "$T/s2" > /dev/null 2> "$T/e" || fail "run errors: $(cat "$T/e")"
pic dump > "$T/d"
[ "$(iserr mcp__cur__fail)" = 1 ] || fail "isError not an error: $(grep '^effect' "$T/d")"
grep -q 'it failed' "$T/d" || fail "fail text"
[ "$(iserr mcp__cur__boom)" = 1 ] || fail "boom not an error"
grep -q 'boom' "$T/d" || fail "boom text"
grep -q 'chars truncated' "$T/d" || fail "the long answer was not cut"
grep -q 'pic:output/3' "$T/d" || fail "the long answer's full text is not named"
printf 'PASS: tool errors, protocol errors and long answers\n'
serve slowsrv modern
pic mcp add slowsrv --url "$url" --timeout 2 > /dev/null 2>&1 || fail "add slowsrv"
fresh
printf 'tool raw mcp__slowsrv__slow {}\ntext done\n' > "$T/s3"
pic submit job 'slow' > /dev/null
start=$(date +%s)
pic run --script "$T/s3" > /dev/null 2>&1 || fail "run slow"
[ $(($(date +%s) - start)) -lt 20 ] || fail "the timeout did not hold"
[ "$(iserr mcp__slowsrv__slow)" = 1 ] || fail "slow: $(pic dump | grep '^effect')"
pic dump | grep -q 'timed out' || fail "slow: no timeout message"
printf 'PASS: a call that takes longer than the server'"'"'s timeout fails\n'
serve dying modern
pic mcp add dying --url "$url" > /dev/null 2>&1 || fail "add dying"
fresh
printf 'tool raw mcp__dying__die {"x":1}\ntext done\n' > "$T/s4"
pic submit job 'die' > /dev/null
pic run --script "$T/s4" > /dev/null 2>&1 || fail "run die"
[ "$(iserr mcp__dying__die)" = 1 ] || fail "die: $(pic dump | grep '^effect')"
[ "$(ledger dying)" = 1 ] || fail "die ran $(ledger dying) times"
printf 'PASS: a server that dies mid-call gives an error, and the call is not repeated\n'
# ---- hooks see MCP results ---------------------------------------------------------------------
cat > "$HOME/.pic/hooks.json" <<EOF
{"hooks": {"PostToolUse": [{"matcher": "mcp__cur__echo", "hooks": [{"type":"command","required":true,
"command":"printf '{\"hookSpecificOutput\":{\"hookEventName\":\"PostToolUse\",\"updatedToolOutput\":{\"content\":[{\"type\":\"text\",\"text\":\"checked by hook\"}]}}}'"}]}]}}
EOF
fresh
printf 'tool raw mcp__cur__echo {"text":"hooked"}\ntext done\n' > "$T/s5"
pic submit job 'hook' > /dev/null
pic run --script "$T/s5" > /dev/null 2>&1 || fail "run hook"
pic dump | grep -q 'checked by hook' || fail "the required PostToolUse hook did not replace the result: $(pic dump)"
rm "$HOME/.pic/hooks.json"
printf 'PASS: PostToolUse hooks, required ones included, run on MCP results\n'
# ---- a crash during a write: outcome unknown, never run again ------------------------------------
if [ "$FAULTS" = yes ]; then
fresh
before=$(ledger old)
printf 'tool raw mcp__old__write_note {"note":"crash"}\ntext done\n' > "$T/s6"
pic submit job 'crash' > /dev/null
if PIC_CRASH_AT=after-exec:1 pic run --script "$T/s6" > /dev/null 2>&1; then fail "did not crash"; fi
[ "$("$SQLITE3" "$DB" "SELECT state || ' ' || replay_safe FROM pic_effect")" = "started 0" ] ||
fail "the call was not left started"
pic run --script "$T/s6" > /dev/null 2>&1 || fail "recovery run"
pic dump > "$T/d"
grep -q '^effect 1 .* mcp__old__write_note unknown safe=0 ' "$T/d" || fail "not unknown: $(grep '^effect' "$T/d")"
grep -q 'outcome unknown' "$T/d" || fail "the model was not told"
[ "$(ledger old)" = $((before + 1)) ] || fail "write_note ran again after the crash"
printf 'PASS: a crashed MCP write is outcome unknown and never run again\n'
fi
# ---- pic mcp call, and remove ------------------------------------------------------------------
pic mcp call cur echo '{"text":"direct"}' > "$T/c" 2>&1 || fail "call: $(cat "$T/c")"
grep -q 'echo: {"text":"direct"}' "$T/c" || fail "call said: $(cat "$T/c")"
pic mcp remove cur > /dev/null || fail "remove cur"
pic mcp get cur > /dev/null 2>&1 && fail "cur outlived its removal"
"$SQLITE3" "$HOME/.pic/pic.sqlite" "SELECT count(*) FROM picu_mcp_server WHERE name = 'cur'" | grep -q '^0$' ||
fail "the row is still there"
fresh
pic submit job 'after' > /dev/null
pic run --script "$T/s1" > /dev/null 2>&1 || :
declared | grep -q 'mcp__cur__' && fail "a removed server's tools were declared: $(declared)"
printf 'PASS: pic mcp call runs a tool directly; remove forgets the server and its tools\n'