#!/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"
pic mcp list > "$T/l" 2>&1 && fail "list exited 0 with a server down"
grep -q "^dying .* not connected: " "$T/l" || fail "list did not say dying is down: $(cat "$T/l")"
pic mcp remove dying > /dev/null || fail "remove dying"
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 env PIC_CRASH_AT=after-exec:1 "$PIC" --db "$DB" --workspace "$T/ws" 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'

# ---- stdio: Pi's shape with a sealed --env, Claude Code's with the command as positionals ------
SECRET2=sk-stdio-SECRET-91bd2e
mkdir "$T/sd" "$T/sl"
pic mcp add files --env FAKE_MCP_TOKEN="$SECRET2" -- "$MCP" --stdio "$T/sd" 120 modern > "$T/o" 2> "$T/e" ||
    fail "add stdio: $(cat "$T/e")"
grep -q "Added files (stdio): 7 tools, MCP 2026-07-28" "$T/o" || fail "add stdio said: $(cat "$T/o")"
grep -qx "$SECRET2" "$T/sd/env" || fail "the sealed environment did not reach the server"
pic mcp add -t stdio -e X=1 old2 "$MCP" --stdio "$T/sl" 120 legacy > "$T/o" 2> "$T/e" || fail "add old2: $(cat "$T/e")"
grep -q "MCP 2025-11-25" "$T/o" || fail "add old2 said: $(cat "$T/o")"
grep -q '"method":"initialize"' "$T/sl/stdio.log" || fail "a 2025 stdio server was not sent initialize"
pic mcp get files > "$T/g" 2>&1 || fail "get files"
grep -q "FAKE_MCP_TOKEN (values sealed)" "$T/g" || fail "get files: $(cat "$T/g")"
pic mcp list > "$T/l" 2>&1 || fail "list: $(cat "$T/l")"
grep -c "$SECRET2" "$T/g" "$T/l" "$T/o" | grep -qv ':0$' && fail "the stdio secret was printed"
cat "$HOME/.pic/pic.sqlite"* | grep -c "$SECRET2" > /dev/null && fail "the stdio secret is in pic.sqlite in plain text"
for p in $(cat "$T/sd/pids" "$T/sl/pids"); do kill -0 "$p" 2> /dev/null && fail "a listed stdio server ($p) is still running"; done
printf 'PASS: stdio servers are added in both shapes, their environment sealed, and stopped after listing\n'

# A reply's calls to one stdio server share one process, started in the workspace and stopped at the end.
fresh
started=$(wc -l < "$T/sd/pids" | tr -d ' ')
printf 'tool raw mcp__files__echo {"text":"one"}\traw mcp__files__write_note {"note":"two"}\ntext done\n' > "$T/s7"
pic submit job 'stdio' > /dev/null
pic run --script "$T/s7" > /dev/null 2> "$T/e" || fail "run stdio: $(cat "$T/e")"
pic dump > "$T/d"
grep -q '^effect 1 .* mcp__files__echo done safe=1 ' "$T/d" || fail "stdio echo: $(grep '^effect' "$T/d")"
grep -q '^effect 2 .* mcp__files__write_note done safe=0 ' "$T/d" || fail "stdio note: $(grep '^effect' "$T/d")"
grep -q 'echo: {\\"text\\":\\"one\\"}\|echo: {"text":"one"}' "$T/d" || fail "stdio echo result"
[ "$(wc -l < "$T/sd/pids" | tr -d ' ')" = $((started + 1)) ] || fail "the calls did not share one server process"
[ "$(tail -1 "$T/sd/cwd")" = "$T/ws" ] || fail "the server did not start in the workspace: $(tail -1 "$T/sd/cwd")"
kill -0 "$(tail -1 "$T/sd/pids")" 2> /dev/null && fail "the stdio server outlived the request"
grep -c "$SECRET2" "$T/d" "$DB" "$DB-wal" 2>/dev/null | grep -qv ':0$' && fail "the stdio secret reached the journal"
printf 'PASS: stdio tools run through a request in one server process, stopped afterwards\n'

# A stdio server that exits mid-call, and one that does not answer in time.
mkdir "$T/sx"
pic mcp add sdie --timeout 2 -- "$MCP" --stdio "$T/sx" 120 modern > /dev/null 2>&1 || fail "add sdie"
fresh
printf 'tool raw mcp__sdie__die {"x":2}\ntext done\n' > "$T/s8"
pic submit job 'stdio die' > /dev/null
pic run --script "$T/s8" > /dev/null 2>&1 || fail "run stdio die"
[ "$(iserr mcp__sdie__die)" = 1 ] || fail "stdio die: $(pic dump | grep '^effect')"
[ "$(ledger sx)" = 1 ] || fail "stdio die ran $(ledger sx) times"
fresh
printf 'tool raw mcp__sdie__slow {}\ntext done\n' > "$T/s9"
pic submit job 'stdio slow' > /dev/null
start=$(date +%s)
pic run --script "$T/s9" > /dev/null 2>&1 || fail "run stdio slow"
[ $(($(date +%s) - start)) -lt 20 ] || fail "the stdio timeout did not hold"
pic dump | grep -q 'timed out' || fail "stdio slow: $(pic dump | grep '^effect')"
kill -0 "$(tail -1 "$T/sx/pids")" 2> /dev/null && fail "the timed-out stdio server is still running"
printf 'PASS: a stdio server that exits or stalls mid-call gives an error and is stopped\n'

if [ "$FAULTS" = yes ]; then
    fresh
    before=$(ledger sd)
    printf 'tool raw mcp__files__write_note {"note":"crash"}\ntext done\n' > "$T/s10"
    pic submit job 'stdio crash' > /dev/null
    if env PIC_CRASH_AT=after-exec:1 "$PIC" --db "$DB" --workspace "$T/ws" run --script "$T/s10" > /dev/null 2>&1; then fail "stdio: did not crash"; fi
    pic run --script "$T/s10" > /dev/null 2>&1 || fail "stdio recovery run"
    pic dump | grep -q '^effect 1 .* mcp__files__write_note unknown safe=0 ' || fail "stdio: not unknown"
    [ "$(ledger sd)" = $((before + 1)) ] || fail "the stdio write ran again after the crash"
    i=0
    while kill -0 "$(tail -1 "$T/sd/pids")" 2> /dev/null; do
        i=$((i + 1)); [ "$i" -lt 100 ] || fail "the crashed pic's stdio server is still running"; sleep 0.05
    done
    printf 'PASS: a crashed stdio write is outcome unknown, and its server ends with pic\n'
fi

# ---- a trusted project's own servers: read, never written; untrusted, ignored ------------------
mkdir -p "$T/ws/.pic" "$T/sp"
printf '{"mcpServers":{"proj":{"command":"%s","args":["--stdio","%s",120,"modern"],"env":{"FAKE_MCP_TOKEN":"${PROJ_TOKEN}"}},"old":{"enabled":false}}}\n' \
    "$MCP" "$T/sp" > "$T/ws/.mcp.json"
printf '{"mcpServers":{"proj":{"command":"%s","args":["--stdio","%s","120","modern"],"env":{"FAKE_MCP_TOKEN":"${PROJ_TOKEN}"},"description":"from pic'"'"'s file"}}}\n' \
    "$MCP" "$T/sp" > "$T/ws/.pic/mcp.json"
cp "$T/ws/.mcp.json" "$T/before.mcp.json"
cp "$T/ws/.pic/mcp.json" "$T/before.pic.json"
pic mcp list > "$T/l" 2>&1 || :
grep -q '^proj ' "$T/l" && fail "an untrusted project's server was listed"
grep -q '^old .*disabled' "$T/l" && fail "an untrusted project's override was applied"
pic trust yes > /dev/null || fail "trust"
PROJ_TOKEN=proj-token-1 pic mcp list > "$T/l" 2>&1 || :
grep -q '^proj  *stdio  *2026-07-28  *project  *7 tools  connected' "$T/l" || fail "the project's server: $(cat "$T/l")"
grep -q '^old .*disabled' "$T/l" || fail "the project's override did not turn old off: $(cat "$T/l")"
grep -qx proj-token-1 "$T/sp/env" || fail "the project's \${PROJ_TOKEN} did not reach its server"
pic mcp get proj | grep -q "from: .*/\.pic/mcp\.json" || fail "the later file did not win: $(pic mcp get proj)"
pic mcp remove proj 2> "$T/e" && fail "a project's server was removed"
grep -q "pic does not change that file" "$T/e" || fail "remove proj said: $(cat "$T/e")"
fresh
printf 'tool raw mcp__proj__echo {"text":"p"}\ntext done\n' > "$T/s11"
pic submit job 'project' > /dev/null
PROJ_TOKEN=proj-token-2 pic run --script "$T/s11" > /dev/null 2> "$T/e" || fail "run project: $(cat "$T/e")"
declared | grep -q 'mcp__proj__echo' || fail "the project's tools were not declared: $(declared)"
declared | grep -q 'mcp__old__' && fail "a server the project turned off was declared: $(declared)"
pic dump | grep -q '^effect 1 .* mcp__proj__echo done safe=1 ' || fail "project call: $(pic dump | grep '^effect')"
cmp -s "$T/ws/.mcp.json" "$T/before.mcp.json" && cmp -s "$T/ws/.pic/mcp.json" "$T/before.pic.json" ||
    fail "a project file was written"
pic trust no > /dev/null
rm -rf "$T/ws/.mcp.json" "$T/ws/.pic/mcp.json"
printf 'PASS: a trusted project'"'"'s .mcp.json and .pic/mcp.json are read, never written\n'

# ---- the RPC commands behind /mcp ---------------------------------------------------------------
rpc() { printf '%s\n' "$1" | "$PIC" --db "$DB" --workspace "$T/ws" --mode rpc 2>/dev/null | grep '"type":"response"'; }
rpc '{"id":"g","type":"get_mcp_servers"}' > "$T/r"
grep -q '"success":true' "$T/r" || fail "get_mcp_servers: $(cat "$T/r")"
grep -q '"name":"files","source":"user","transport":"stdio"' "$T/r" || fail "get_mcp_servers files: $(cat "$T/r")"
grep -q '"env":\["FAKE_MCP_TOKEN"\]' "$T/r" || fail "get_mcp_servers env names: $(cat "$T/r")"
grep -q '"name":"mcp__files__echo","tool":"echo","readOnly":true' "$T/r" || fail "get_mcp_servers tools: $(cat "$T/r")"
grep -c "$SECRET2" "$T/r" | grep -qv '^0$' && fail "get_mcp_servers showed a secret"
mkdir "$T/sr"
rpc "{\"id\":\"a\",\"type\":\"add_mcp_server\",\"args\":[\"viarpc\",\"--\",\"$MCP\",\"--stdio\",\"$T/sr\",\"120\",\"legacy\"]}" > "$T/r"
grep -q '"servers":\[{"name":"viarpc","count":7,"era":"2025-11-25","error":null}\]' "$T/r" || fail "add_mcp_server: $(cat "$T/r")"
rpc '{"id":"e","type":"set_mcp_server_enabled","name":"viarpc","enabled":false}' | grep -q '"enabled":false' ||
    fail "set_mcp_server_enabled"
pic mcp get viarpc | grep -q '  disabled' || fail "viarpc not disabled"
rpc '{"id":"f","type":"refresh_mcp_servers","name":"files"}' > "$T/r"
grep -q '"name":"files","count":7,"era":"2026-07-28","error":null' "$T/r" || fail "refresh_mcp_servers: $(cat "$T/r")"
rpc '{"id":"r","type":"remove_mcp_server","name":"viarpc"}' | grep -q '"removed":true' || fail "remove_mcp_server"
printf 'PASS: get, add, set_enabled, refresh and remove over RPC\n'
