#!/bin/sh
# More of the MCP client against the fake MCP server (test/pic/fake_mcp.c): the servers'
# instructions in the system prompt, prompts as slash commands, @server:uri mentions,
# list_changed notifications, --mcp-config and --strict-mcp-config, MAX_MCP_OUTPUT_TOKENS and
# MCP_TOOL_TIMEOUT, pic mcp serve, add-from-claude-desktop, headersHelper and !command values,
# and a UTCP manual through its MCP bridge.
set -eu
PIC=${PIC:?set PIC to the pic binary}
MCP=${MCP:-$(dirname "$PIC")/../fake_mcp}
SQLITE3=${SQLITE3:-sqlite3}
T=$(mktemp -d "${TMPDIR:-/tmp}/pic-mcpmore.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 MAX_MCP_OUTPUT_TOKENS MCP_TOOL_TIMEOUT
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 over HTTP, its requests in $T/NAME, 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"; }
system() { "$SQLITE3" "$DB" "SELECT system_prompt FROM pic_session"; }
declared() { "$SQLITE3" "$DB" "SELECT group_concat(json_extract(value,'\$.name'),' ') FROM pic_session, json_each(tools)"; }
result() { "$SQLITE3" "$DB" "SELECT result FROM pic_effect WHERE tool = '$1'"; }
done_script() { printf 'text done\n' > "$T/done"; }
done_script
# ---- the servers' instructions reach the system prompt -------------------------------------------
mkdir "$T/si" "$T/sh"
pic mcp add inst -- "$MCP" --stdio "$T/si" 120 modern > /dev/null 2>&1 || fail "add inst"
pic mcp add quiet --exposure hidden -- "$MCP" --stdio "$T/sh" 120 legacy > /dev/null 2>&1 || fail "add quiet"
fresh
pic submit job 'hello' > /dev/null
pic run --script "$T/done" > /dev/null 2> "$T/e" || fail "run: $(cat "$T/e")"
system > "$T/sp"
grep -q '^# MCP Server Instructions$' "$T/sp" || fail "no MCP section: $(tail -20 "$T/sp")"
grep -q '^The following MCP servers have provided instructions for how to use their tools and resources:$' "$T/sp" ||
fail "the section's words: $(tail -20 "$T/sp")"
grep -A1 '^## inst$' "$T/sp" | grep -q '^A fake server for tests\.$' || fail "inst's instructions: $(tail -20 "$T/sp")"
grep -q '^## quiet$' "$T/sp" && fail "a hidden server's instructions were sent"
fresh
pic --no-mcp submit job 'hello' > /dev/null
pic --no-mcp run --script "$T/done" > /dev/null 2>&1 || fail "run --no-mcp"
system | grep -q 'MCP Server Instructions' && fail "--no-mcp still sent the servers' instructions"
pic mcp remove quiet > /dev/null
for k in 1 2 3 4 5; do
mkdir "$T/sbig$k"
pic mcp add "big$k" -- env FAKE_MCP_INSTRUCTIONS_KB=9 "$MCP" --stdio "$T/sbig$k" 120 modern > /dev/null 2>&1 ||
fail "add big$k"
done
fresh
pic submit job 'hello' > /dev/null
pic run --script "$T/done" > /dev/null 2> "$T/e" || fail "run big: $(cat "$T/e")"
system > "$T/sp"
took=$(grep -oE 'i{200,}' "$T/sp" | tr -d '\n' | wc -c | tr -d ' ')
[ "$took" -le 32768 ] || fail "the servers' instructions took $took bytes, over 32 KB in all"
[ "$took" -ge 30000 ] || fail "the servers' instructions took only $took bytes"
grep -q '^(The instructions of 2 more servers are left out: .* 32 KB in all\.)$' "$T/sp" ||
fail "no note of the instructions left out: $(tail -c 400 "$T/sp")"
for k in 1 2 3 4 5; do pic mcp remove "big$k" > /dev/null; done
printf 'PASS: the instructions MCP servers give are in the system prompt, unless hidden or --no-mcp, 32 KB in all\n'
# ---- MAX_MCP_OUTPUT_TOKENS and MCP_TOOL_TIMEOUT, as Claude Code's ------------------------------
serve lim modern
pic mcp add lim --url "$url" > /dev/null 2>&1 || fail "add lim"
long=$(printf '%0600d' 0)
printf 'tool raw mcp__lim__echo {"text":"%s"}\traw mcp__lim__big {}\ntext done\n' "$long" > "$T/s6"
fresh
pic submit job 'limits' > /dev/null
MAX_MCP_OUTPUT_TOKENS=100 pic run --script "$T/s6" > /dev/null 2> "$T/e" || fail "run tokens: $(cat "$T/e")"
result mcp__lim__echo | grep -q 'chars truncated' || fail "100 tokens did not cut 600 characters: $(result mcp__lim__echo)"
fresh
pic submit job 'limits' > /dev/null
MAX_MCP_OUTPUT_TOKENS=20000 pic run --script "$T/s6" > /dev/null 2> "$T/e" || fail "run tokens: $(cat "$T/e")"
result mcp__lim__big | grep -q 'chars truncated' && fail "20000 tokens still cut 30000 bytes"
[ "$(result mcp__lim__big | wc -c | tr -d ' ')" -ge 30000 ] || fail "the big answer: $(result mcp__lim__big | wc -c)"
printf 'tool raw mcp__lim__slow {}\ntext done\n' > "$T/s6b"
fresh
pic submit job 'slow' > /dev/null
start=$(date +%s)
MCP_TOOL_TIMEOUT=1500 pic run --script "$T/s6b" > /dev/null 2>&1 || fail "run slow"
[ $(($(date +%s) - start)) -lt 20 ] || fail "MCP_TOOL_TIMEOUT did not hold"
result mcp__lim__slow | grep -q 'timed out (no answer within 2 seconds)' || fail "slow: $(result mcp__lim__slow)"
pic mcp remove lim > /dev/null
printf 'PASS: MAX_MCP_OUTPUT_TOKENS sets where a result is cut, MCP_TOOL_TIMEOUT how long a call may take\n'
# ---- headersHelper (Claude Code's) and !command values (Pi's) in headers and env ---------------
serve hh modern
helper='printf '"'"'{"X-Over":"dynamic","X-Who":"%s","X-At":"%s"}'"'"' "$CLAUDE_CODE_MCP_SERVER_NAME" "$CLAUDE_CODE_MCP_SERVER_URL"'
json=$("$SQLITE3" :memory: "SELECT json_object('type','http','url','$url','headers',json_object('X-Static','s1','X-Over','static'),'headersHelper','$(printf '%s' "$helper" | sed "s/'/''/g")')")
pic mcp add-json hh "$json" > "$T/o" 2> "$T/e" || fail "add hh: $(cat "$T/e")"
grep -q "7 tools" "$T/o" || fail "add hh said: $(cat "$T/o")"
grep -qi '^X-Over: dynamic' "$T/hh/1.req" || fail "the helper's header did not win: $(cat "$T/hh/1.req")"
grep -qi '^X-Over: static' "$T/hh/1.req" && fail "the static header was sent beside the helper's"
grep -qi '^X-Who: hh' "$T/hh/1.req" || fail "the helper did not get the server's name"
grep -qi "^X-At: $url" "$T/hh/1.req" || fail "the helper did not get the server's URL"
grep -qi '^X-Static: s1' "$T/hh/1.req" || fail "a static header was lost"
pic mcp get hh | grep -q "headers helper: printf" || fail "get hh: $(pic mcp get hh)"
pic mcp add-json hh2 '{"type":"stdio","command":"true","headersHelper":"echo {}"}' 2> "$T/e" && fail "a stdio server took a headersHelper"
grep -q "headersHelper is for an HTTP server" "$T/e" || fail "stdio helper refusal: $(cat "$T/e")"
serve bang modern
pic mcp add bang --url "$url" -H 'X-Cmd: !printf "cmd-%s" ok' > "$T/o" 2> "$T/e" || fail "add bang: $(cat "$T/e")"
grep -qF 'X-Cmd runs: printf "cmd-%s" ok' "$T/o" || fail "add bang did not print its !command: $(cat "$T/o")"
grep -qi '^X-Cmd: cmd-ok' "$T/bang/1.req" || fail "the !command header: $(cat "$T/bang/1.req")"
pic mcp add bad --url "$url" -H 'X-Bad: !exit 3' > "$T/o" 2> "$T/e" && fail "a failing !command was reached"
grep -qF 'X-Bad runs: exit 3' "$T/o" || fail "add bad did not print its !command: $(cat "$T/o")"
grep -q "X-Bad.*command failed" "$T/e" || fail "the failing command: $(cat "$T/e")"
mkdir "$T/sb"
pic mcp add sbang --env 'FAKE_MCP_TOKEN=!printf tok-from-cmd' -- "$MCP" --stdio "$T/sb" 120 modern > "$T/o" 2> "$T/e" ||
fail "add sbang: $(cat "$T/e")"
grep -qF 'FAKE_MCP_TOKEN runs: printf tok-from-cmd' "$T/o" || fail "add sbang did not print its !command: $(cat "$T/o")"
grep -qx tok-from-cmd "$T/sb/env" || fail "the !command environment value: $(cat "$T/sb/env" 2>/dev/null)"
# a headersHelper alone, nothing sealed: Claude Code's usual shape
serve hho modern
helper='printf '"'"'{"Authorization":"Bearer from-helper"}'"'"''
json=$("$SQLITE3" :memory: "SELECT json_object('type','http','url','$url','headersHelper','$(printf '%s' "$helper" | sed "s/'/''/g")')")
pic mcp add-json hho "$json" > "$T/o" 2> "$T/e" || fail "add hho: $(cat "$T/e")"
grep -qi '^Authorization: Bearer from-helper' "$T/hho/1.req" || fail "a headersHelper with no fixed headers: $(cat "$T/hho/1.req")"
# its 10 seconds hold for the whole run: one that closes its output and goes on is killed then
json=$("$SQLITE3" :memory: "SELECT json_object('type','http','url','$url','headersHelper','echo {}; exec >&-; sleep 25')")
start=$(date +%s)
pic mcp add-json slowh "$json" > /dev/null 2> "$T/e" && fail "a helper that did not end was reached"
[ $(($(date +%s) - start)) -lt 18 ] || fail "the helper's 10 seconds did not hold: $(($(date +%s) - start)) s"
grep -q "headersHelper: the command did not finish within 10 seconds" "$T/e" || fail "the slow helper: $(cat "$T/e")"
# a leading ! is a command only in Pi's format (pic mcp add's flags, .pic/mcp.json); in Claude
# Code's JSON (add-json, a snippet, Claude Desktop's, .mcp.json, --mcp-config) it is the value
serve lit modern
pic mcp add-json lit "{\"type\":\"http\",\"url\":\"$url\",\"headers\":{\"X-Lit\":\"!printf ran\"}}" > "$T/o" 2> "$T/e" ||
fail "add lit: $(cat "$T/e")"
grep -q 'runs:' "$T/o" && fail "add-json said a literal ! would run: $(cat "$T/o")"
cat "$T/lit/"*.req | grep -qi '^X-Lit: !printf ran' || fail "add-json ran a ! value: $(cat "$T/lit/1.req")"
pic mcp add esc --url "$url" -H 'X-Esc: $!kept' > "$T/o" 2> "$T/e" || fail "add esc: $(cat "$T/e")"
grep -q 'runs:' "$T/o" && fail "escaped \$! said a command would run: $(cat "$T/o")"
cat "$T/lit/"*.req | grep -qi '^X-Esc: !kept' || fail "\$! did not give a literal !"
mkdir -p "$T/pw/.pic"
printf '{"mcpServers":{"plit":{"url":"%s","headers":{"X-P1":"!printf ran"}}}}\n' "$url" > "$T/pw/.mcp.json"
printf '{"mcpServers":{"prun":{"url":"%s","headers":{"X-P2":"!printf ran"}}}}\n' "$url" > "$T/pw/.pic/mcp.json"
"$PIC" --db "$DB" --workspace "$T/pw" trust yes > /dev/null || fail "trust pw"
"$PIC" --db "$DB" --workspace "$T/pw" mcp list > "$T/l" 2>&1 || :
cat "$T/lit/"*.req | grep -qi '^X-P1: !printf ran' || fail ".mcp.json's ! value: $(cat "$T/l")"
cat "$T/lit/"*.req | grep -qi '^X-P2: ran' || fail ".pic/mcp.json's !command: $(cat "$T/l")"
"$PIC" --db "$DB" --workspace "$T/pw" trust no > /dev/null
for s in hh bang bad sbang hho slowh lit esc; do pic mcp remove "$s" > /dev/null; done
printf 'PASS: headersHelper adds and replaces headers, alone too, in 10 s; a !command runs in Pi'"'"'s format\n'
# ---- pic mcp add-from-claude-desktop -----------------------------------------------------------
serve desk modern
mkdir "$T/sdk" "$T/sdk2"
for dir in "$HOME/Library/Application Support/Claude" "$HOME/.config/Claude"; do
mkdir -p "$dir"
printf '{"mcpServers":{"files":{"command":"%s","args":["--stdio","%s","120","modern"],"env":{"FAKE_MCP_TOKEN":"desk-secret-1"}},"web":{"url":"%s"},"other":{"command":"%s","args":["--stdio","%s","120","legacy"]}},"globalShortcut":"x"}\n' \
"$MCP" "$T/sdk" "$url" "$MCP" "$T/sdk2" > "$dir/claude_desktop_config.json"
done
pic mcp add web --url "$url" > /dev/null 2>&1 || fail "add web"
pic mcp add-from-claude-desktop files web > "$T/o" 2> "$T/e" || fail "add-from-claude-desktop: $(cat "$T/e")"
grep -q "Added files (stdio): 7 tools" "$T/o" || fail "files: $(cat "$T/o")"
grep -q "Added web_1 (" "$T/o" || fail "web was not added as web_1: $(cat "$T/o")"
pic mcp get other > /dev/null 2>&1 && fail "a server not named was imported"
grep -qx desk-secret-1 "$T/sdk/env" || fail "the desktop server's environment"
pic mcp get files | grep -q "FAKE_MCP_TOKEN (values sealed)" || fail "the environment was not sealed"
cat "$HOME/.pic/pic.sqlite"* | grep -c desk-secret-1 > /dev/null && fail "the desktop secret is in pic.sqlite in plain text"
pic mcp add-from-claude-desktop nosuch 2> "$T/e" && fail "a missing name was imported"
grep -q "nosuch" "$T/e" || fail "the missing name: $(cat "$T/e")"
pic mcp add-from-claude-desktop > "$T/o" 2>&1 || fail "import all: $(cat "$T/o")"
grep -q "Added other " "$T/o" || fail "import all: $(cat "$T/o")"
for s in files web web_1 files_1 web_2 other; do pic mcp remove "$s" > /dev/null 2>&1 || :; done
printf 'PASS: pic mcp add-from-claude-desktop imports the named servers, or all, a taken name with _1\n'
# ---- --mcp-config and --strict-mcp-config, as Claude Code's -------------------------------------
serve cfgsrv modern
mkdir "$T/sc"
printf '{"mcpServers":{"cfg":{"type":"http","url":"%s"}}}\n' "$url" > "$T/cfg.json"
inline=$(printf '{"mcpServers":{"cfg2":{"command":"%s","args":["--stdio","%s","120","modern"]}}}' "$MCP" "$T/sc")
printf 'tool raw mcp__cfg__echo {"text":"c1"}\traw mcp__cfg2__echo {"text":"c2"}\ntext done\n' > "$T/s5"
fresh
pic --mcp-config "$T/cfg.json" --mcp-config "$inline" submit job 'configs' > /dev/null
pic --mcp-config "$T/cfg.json" --mcp-config "$inline" run --script "$T/s5" > /dev/null 2> "$T/e" || fail "run configs: $(cat "$T/e")"
declared | grep -q 'mcp__cfg__echo' || fail "--mcp-config's file server was not declared: $(declared)"
declared | grep -q 'mcp__cfg2__echo' || fail "--mcp-config's inline server was not declared: $(declared)"
declared | grep -q 'mcp__inst__echo' || fail "without --strict-mcp-config your servers stay: $(declared)"
result mcp__cfg__echo | grep -q 'echo: {"text":"c1"}' || fail "cfg call: $(result mcp__cfg__echo)"
result mcp__cfg2__echo | grep -q 'echo: {"text":"c2"}' || fail "cfg2 call: $(result mcp__cfg2__echo)"
fresh
pic --strict-mcp-config --mcp-config "$T/cfg.json" submit job 'strict' > /dev/null
pic --strict-mcp-config --mcp-config "$T/cfg.json" run --script "$T/done" > /dev/null 2> "$T/e" || fail "run strict: $(cat "$T/e")"
declared | grep -q 'mcp__cfg__echo' || fail "strict: the config's server: $(declared)"
declared | grep -q 'mcp__inst__' && fail "strict: one of your servers was declared: $(declared)"
pic --mcp-config "$T/cfg.json" mcp list > "$T/l" 2>&1 || fail "list with a config: $(cat "$T/l")"
grep -q '^cfg *http *2026-07-28 *config *7 tools connected' "$T/l" || fail "list cfg: $(cat "$T/l")"
pic --strict-mcp-config mcp list > "$T/l" 2>&1 || :
grep -q '^inst ' "$T/l" && fail "strict with no config listed your servers: $(cat "$T/l")"
pic --mcp-config "$T/nope.json" mcp list 2> "$T/e" && fail "a missing config file was accepted"
grep -q "nope.json" "$T/e" || fail "the missing config: $(cat "$T/e")"
pic --mcp-config '{"mcpServers":' mcp list 2> "$T/e" && fail "a broken config was accepted"
"$SQLITE3" "$HOME/.pic/pic.sqlite" "SELECT count(*) FROM picu_mcp_server WHERE name LIKE 'cfg%'" | grep -q '^0$' ||
fail "a --mcp-config server was saved as one of yours"
printf '{"mcpServers":{"cfgs":{"type":"http","url":"%s","headers":{"X-Secret":"cfg-secret-4242"}}}}\n' "$url" > "$T/cfgs.json"
pic --mcp-config "$T/cfgs.json" mcp list > "$T/l" 2>&1 || fail "list cfgs: $(cat "$T/l")"
grep -q '^cfgs ' "$T/l" || fail "list cfgs: $(cat "$T/l")"
fresh
pic --mcp-config "$T/cfgs.json" submit job 'secret' > /dev/null
pic --mcp-config "$T/cfgs.json" run --script "$T/done" > /dev/null 2> "$T/e" || fail "run cfgs: $(cat "$T/e")"
grep -q 'X-Secret: cfg-secret-4242' "$T/cfgsrv/"*.req || fail "the config's header was not sent"
cat "$HOME/.pic/pic.sqlite"* "$DB" "$DB-wal" 2> /dev/null | grep -q cfg-secret-4242 && fail "a --mcp-config secret was written to a database"
# A leftover --mcp-config row from an older build is cleaned on any plain start (no --mcp-config).
"$SQLITE3" "$HOME/.pic/pic.sqlite" \
"INSERT OR REPLACE INTO picu_mcp_project(workspace, name, entry) VALUES('--mcp-config', 'stale', '{\"url\":\"http://127.0.0.1:1/mcp\",\"headers\":{\"X-Old\":\"old-secret-9999\"}}')"
pic mcp list > /dev/null 2>&1 || :
[ "$("$SQLITE3" "$HOME/.pic/pic.sqlite" "SELECT count(*) FROM picu_mcp_project WHERE workspace = '--mcp-config'")" = 0 ] ||
fail "a plain start without --mcp-config left a stale --mcp-config row in pic.sqlite"
printf 'PASS: --mcp-config adds servers from files and JSON for one run; --strict-mcp-config uses only them\n'
# ---- a model server for pic ask: each request's body in $T/llm/k.req ----------------------------
SERVER=${SERVER:-$(dirname "$PIC")/../fake_server}
S=$(cd "$(dirname "$0")/streams" && pwd)
mkdir "$T/llm"
"$SERVER" "$T/llm.port" "$T/llm" 120 "sse:$S/text.sse" "sse:$S/text.sse" "sse:$S/text.sse" "sse:$S/text.sse" \
"sse:$S/text.sse" "sse:$S/text.sse" "sse:$S/text.sse" "sse:$S/text.sse" &
pids="$pids $!"
i=0
while [ ! -s "$T/llm.port" ]; do i=$((i + 1)); [ "$i" -lt 600 ] || fail "model server"; sleep 0.05; done
printf '{"providers":{"fake":{"baseUrl":"http://127.0.0.1:%s","api":"anthropic-messages","apiKey":"$ANTHROPIC_API_KEY","models":[{"id":"m"}]}}}' \
"$(cat "$T/llm.port")" > "$T/models.json"
export PIC_MODELS_FILE="$T/models.json" ANTHROPIC_API_KEY=pic-mcp-more-key
"$PIC" settings cacheRetention none > /dev/null
asked=0
# ask TEXT: pic ask on the model server; the request's body in $T/llm/$asked.req
ask() { asked=$((asked + 1)); pic --model fake/m ask "$1" > "$T/ask.out" 2> "$T/ask.err"; }
# ---- MCP prompts as slash commands, /mcp__SERVER__PROMPT ARGS, as Claude Code's ------------------
mkdir "$T/sp1"
FAKE_MCP_PROMPTS=1 pic mcp add pr -- env FAKE_MCP_PROMPTS=1 "$MCP" --stdio "$T/sp1" 120 modern > "$T/o" 2> "$T/e" ||
fail "add pr: $(cat "$T/e")"
grep -q "and 2 prompts" "$T/o" || fail "add pr said: $(cat "$T/o")"
printf '{"id":"c","type":"get_commands"}\n' | pic --mode rpc 2> /dev/null | grep '"type":"response"' > "$T/r"
grep -q '"name":"mcp__pr__review","description":"Review a file","source":"mcp"' "$T/r" || fail "get_commands: $(cat "$T/r")"
grep -q '"argumentHint":"<file> \[focus\]"' "$T/r" || fail "the prompt's hint: $(cat "$T/r")"
ask '/mcp__pr__review main.c error handling' || fail "ask review: $(cat "$T/ask.err")"
grep -qF 'Review main.c focusing on error handling.' "$T/llm/$asked.req" || fail "the prompt was not expanded: $(cut -c1-600 "$T/llm/$asked.req")"
grep -qF '/mcp__pr__review' "$T/llm/$asked.req" && fail "the command itself was sent"
ask '/mcp__pr__hello' || fail "ask hello: $(cat "$T/ask.err")"
grep -qF 'Hello\n\nHi there' "$T/llm/$asked.req" || fail "hello: $(cut -c1-600 "$T/llm/$asked.req")"
asked=$((asked + 0))
pic --model fake/m ask '/mcp__pr__review' > /dev/null 2> "$T/e" && fail "a prompt without its required argument was sent"
grep -q "file" "$T/e" || fail "the missing argument: $(cat "$T/e")"
printf 'PASS: an MCP server'"'"'s prompts are slash commands, expanded with their arguments\n'
# ---- @server:uri mentions read the resource into the message, as Claude Code's ----------------
mkdir "$T/sr1"
pic mcp add res -- env FAKE_MCP_RESOURCES=1 "$MCP" --stdio "$T/sr1" 120 modern > /dev/null 2>&1 || fail "add res"
ask 'Sum up @res:file:///notes.txt, and mail [email protected] about @nobody:file:///x' || fail "ask mention: $(cat "$T/ask.err")"
grep -qF '<resource server=\"res\" uri=\"file:///notes.txt\">\nhello from a resource\n</resource>' "$T/llm/$asked.req" ||
fail "the resource was not attached: $(cut -c1-800 "$T/llm/$asked.req")"
grep -qF 'Sum up @res:file:///notes.txt, and mail [email protected] about @nobody:file:///x' "$T/llm/$asked.req" ||
fail "the message was not kept as typed"
grep -qF 'uri=\"file:///x\"' "$T/llm/$asked.req" && fail "a server that is none of yours was read"
pic --model fake/m ask 'Read @res:file:///missing.txt' > /dev/null 2> "$T/e" && fail "a missing resource was sent"
grep -q "Resource not found" "$T/e" || fail "the missing resource: $(cat "$T/e")"
printf 'PASS: @server:uri reads an MCP resource into the message; other @words are left alone\n'
# ---- notifications/tools/list_changed, prompts/list_changed and resources/list_changed ---------
mkdir "$T/sg"
pic mcp add chg -- env FAKE_MCP_CHANGE=1 FAKE_MCP_PROMPTS=1 "$MCP" --stdio "$T/sg" 120 modern > "$T/o" 2>&1 ||
fail "add chg: $(cat "$T/o")"
grep -q "8 tools and 2 prompts" "$T/o" || fail "add chg said: $(cat "$T/o")"
printf 'tool raw mcp__chg__change {}\traw mcp__chg__change {}\ntext done\n' > "$T/s8"
fresh
pic submit job 'change' > /dev/null
lists=$(grep -c '"method":"tools/list"' "$T/sg/stdio.log" || :)
pic run --script "$T/s8" > /dev/null 2> "$T/e" || fail "run change: $(cat "$T/e")"
lists=$(($(grep -c '"method":"tools/list"' "$T/sg/stdio.log") - lists))
[ "$lists" = 2 ] || fail "two changes in one request listed the tools $lists times, not once (two pages)"
result mcp__chg__change | grep -q '^changed$' || fail "change: $(result mcp__chg__change)"
pic mcp get chg > "$T/g" 2>&1
grep -q 'mcp__chg__added' "$T/g" || fail "tools/list_changed did not list the tools again: $(cat "$T/g")"
grep -q 'resources: offered' "$T/g" || fail "resources/list_changed did not offer resources: $(cat "$T/g")"
printf '{"id":"c","type":"get_commands"}\n' | pic --mode rpc 2> /dev/null | grep -q '"name":"mcp__chg__later"' ||
fail "prompts/list_changed did not list the prompts again"
fresh
pic submit job 'after the change' > /dev/null
pic run --script "$T/done" > /dev/null 2>&1 || fail "run after change"
declared | grep -q 'mcp__chg__added' || fail "the next request did not declare the new tool: $(declared)"
# Two requests in one RPC process, each with a list_changed notification on the kept connection:
# both relist (once per request, not once per process).
cat > "$T/call_chg.sse" <<'EOF'
event: message_start
data: {"type":"message_start","message":{"id":"msg_01","type":"message","role":"assistant","model":"claude-opus-5-5","content":[],"stop_reason":null,"usage":{"input_tokens":25,"output_tokens":1}}}
event: content_block_start
data: {"type":"content_block_start","index":0,"content_block":{"type":"tool_use","id":"toolu_chg1","name":"mcp__chg__change","input":{}}}
event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"input_json_delta","partial_json":"{}"}}
event: content_block_stop
data: {"type":"content_block_stop","index":0}
event: message_delta
data: {"type":"message_delta","delta":{"stop_reason":"tool_use","stop_sequence":null},"usage":{"output_tokens":10}}
event: message_stop
data: {"type":"message_stop"}
EOF
sed 's/toolu_chg1/toolu_chg2/' "$T/call_chg.sse" > "$T/call_chg2.sse"
mkdir "$T/rpc_llm"
"$SERVER" "$T/rpc_llm.port" "$T/rpc_llm" 120 "sse:$T/call_chg.sse" "sse:$S/text.sse" \
"sse:$T/call_chg2.sse" "sse:$S/text.sse" &
pids="$pids $!"
i=0
while [ ! -s "$T/rpc_llm.port" ]; do i=$((i + 1)); [ "$i" -lt 600 ] || fail "rpc model server"; sleep 0.05; done
printf '{"providers":{"fake":{"baseUrl":"http://127.0.0.1:%s","api":"anthropic-messages","apiKey":"$ANTHROPIC_API_KEY","models":[{"id":"m"}]}}}' \
"$(cat "$T/rpc_llm.port")" > "$T/rpc_models.json"
fresh
mkfifo "$T/rpc.in"
lists=$(grep -c '"method":"tools/list"' "$T/sg/stdio.log" || :)
PIC_MODELS_FILE="$T/rpc_models.json" pic --model fake/m --mode rpc < "$T/rpc.in" > "$T/rpc.ev" 2> "$T/rpc.err" &
rpc_pid=$!
exec 3>"$T/rpc.in"
printf '%s\n' '{"id":"p1","type":"prompt","message":"first"}' >&3
i=0
while [ "$(grep -c '"type":"agent_settled"' "$T/rpc.ev" 2>/dev/null || :)" -lt 1 ]; do
i=$((i + 1)); [ "$i" -lt 400 ] || fail "rpc prompt 1: $(cat "$T/rpc.ev") $(cat "$T/rpc.err")"; sleep 0.05
done
printf '%s\n' '{"id":"p2","type":"prompt","message":"second"}' >&3
i=0
while [ "$(grep -c '"type":"agent_settled"' "$T/rpc.ev" 2>/dev/null || :)" -lt 2 ]; do
i=$((i + 1)); [ "$i" -lt 400 ] || fail "rpc prompt 2: $(cat "$T/rpc.ev") $(cat "$T/rpc.err")"; sleep 0.05
done
exec 3>&-
wait "$rpc_pid" || fail "rpc exit: $(cat "$T/rpc.err")"
lists=$(($(grep -c '"method":"tools/list"' "$T/sg/stdio.log") - lists))
[ "$lists" = 4 ] || fail "two RPC requests in one process listed the tools $lists times, not once each (4 pages)"
pic mcp remove chg > /dev/null
mkdir "$T/sgb"
pic mcp add chb -- env FAKE_MCP_CHANGE=bare "$MCP" --stdio "$T/sgb" 120 modern > /dev/null 2>&1 || fail "add chb"
pic mcp call chb change > "$T/c" 2>&1 || fail "call chb: $(cat "$T/c")"
pic mcp get chb | grep -q 'resources: offered' && fail "a resources/list_changed alone offered resources"
pic mcp remove chb > /dev/null
export FAKE_MCP_CHANGE=1
serve chgh modern sse
unset FAKE_MCP_CHANGE
pic mcp add chgh --url "$url" > /dev/null 2>&1 || fail "add chgh"
pic mcp get chgh | grep -q 'mcp__chgh__added' && fail "chgh had the new tool before the change"
pic mcp call chgh change > "$T/c" 2>&1 || fail "call chgh: $(cat "$T/c")"
pic mcp get chgh | grep -q 'mcp__chgh__added' || fail "an SSE stream's tools/list_changed: $(pic mcp get chgh)"
pic mcp remove chgh > /dev/null
# The once-per-request relist cap holds beyond 64 servers too (65 servers; s65 called twice).
{
printf '{"mcpServers":{'
k=1
while [ "$k" -le 65 ]; do
[ "$k" -gt 1 ] && printf ','
printf '"s%02d":{"type":"http","url":"%s","toolExposure":{"*":"hidden","change":"direct"}}' "$k" "$url"
k=$((k + 1))
done
printf '}}\n'
} > "$T/many65.json"
{
k=1
while [ "$k" -le 65 ]; do
printf 'tool raw mcp__s%02d__change {}\n' "$k"
k=$((k + 1))
done
printf 'tool raw mcp__s65__change {}\ntext done\n'
} > "$T/s65"
fresh
pic --strict-mcp-config --mcp-config "$T/many65.json" submit job '65 servers' > /dev/null
lists=$(grep -c '"method":"tools/list"' "$T/chgh/"*.req 2>/dev/null | awk -F: '{s+=$2} END{print s+0}')
pic --strict-mcp-config --mcp-config "$T/many65.json" run --script "$T/s65" > /dev/null 2> "$T/e" || fail "run 65: $(cat "$T/e")"
lists=$(($(grep -c '"method":"tools/list"' "$T/chgh/"*.req 2>/dev/null | awk -F: '{s+=$2} END{print s+0}') - lists))
# 65 initial listings (130 pages) + 65 single relists (130 pages) = 260; a second relist of s65 would be 262.
[ "$lists" = 260 ] || fail "65 servers in one request listed tools $lists times, expected 260 (s65 relisted more than once)"
printf 'PASS: list_changed notifications list a server'"'"'s tools and prompts again, for the next request\n'
# ---- pic mcp serve: pic's own tools to an MCP client over stdio, as claude mcp serve ------------
printf 'hello from the workspace\n' > "$T/ws/f.txt"
{
printf '%s\n' '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-06-18","capabilities":{},"clientInfo":{"name":"t","version":"1"}}}'
printf '%s\n' '{"jsonrpc":"2.0","method":"notifications/initialized"}'
printf '%s\n' '{"jsonrpc":"2.0","id":2,"method":"tools/list"}'
printf '%s\n' '{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"read","arguments":{"path":"f.txt"}}}'
printf '%s\n' '{"jsonrpc":"2.0","id":4,"method":"tools/call","params":{"name":"bash","arguments":{"command":"echo from-bash"}}}'
printf '%s\n' '{"jsonrpc":"2.0","id":5,"method":"tools/call","params":{"name":"nosuch","arguments":{}}}'
printf '%s\n' '{"jsonrpc":"2.0","id":6,"method":"bogus"}'
printf '%s\n' 'not json'
} | pic mcp serve > "$T/serve.out" 2> "$T/serve.err" || fail "mcp serve: $(cat "$T/serve.err")"
line() { sed -n "$1p" "$T/serve.out"; }
[ "$(wc -l < "$T/serve.out" | tr -d ' ')" = 7 ] || fail "serve answered: $(cat "$T/serve.out")"
line 1 | grep -q '"id":1,"result":{"protocolVersion":"2025-06-18","capabilities":{"tools":{}},"serverInfo":{"name":"pic"' ||
fail "initialize: $(line 1)"
line 2 | grep -q '"name":"read","description":' || fail "tools/list: $(line 2)"
line 2 | grep -q '"name":"bash"' || fail "tools/list has no bash: $(line 2)"
line 2 | grep -q '"inputSchema":{' || fail "tools/list schemas: $(line 2)"
line 2 | grep -q '"name":"send_message"\|"name":"wake"\|"name":"mcp__' && fail "serve offered a tool of the journal's or an MCP server's: $(line 2)"
line 3 | grep -q 'hello from the workspace' || fail "read: $(line 3)"
line 4 | grep -q 'from-bash' || fail "bash: $(line 4)"
line 5 | grep -q '"isError":true' || fail "an unknown tool: $(line 5)"
line 6 | grep -q '"code":-32601' || fail "an unknown method: $(line 6)"
line 7 | grep -q '"code":-32700' || fail "not JSON: $(line 7)"
printf '%s\n' '{"jsonrpc":"2.0","id":1,"method":"tools/list"}' | pic --tools read mcp serve > "$T/serve.out" 2>&1
grep -q '"name":"bash"' "$T/serve.out" && fail "--tools read still served bash"
grep -q '"name":"read"' "$T/serve.out" || fail "--tools read: $(cat "$T/serve.out")"
for flag in '--permission-mode read-only' '--model fake/m' '--no-mcp'; do
# shellcheck disable=SC2086
pic $flag mcp serve < /dev/null > /dev/null 2> "$T/e" && fail "pic mcp serve took $flag"
grep -q "pic mcp serve does not take ${flag%% *}" "$T/e" || fail "serve $flag: $(cat "$T/e")"
done
# pic as its own client: the current revision, and a call through it
pic mcp add self -- "$PIC" --workspace "$T/ws" mcp serve > "$T/o" 2> "$T/e" || fail "add self: $(cat "$T/e")"
grep -q "Added self (stdio): .* tools, MCP 2026-07-28" "$T/o" || fail "add self said: $(cat "$T/o")"
pic mcp call self read '{"path":"f.txt"}' > "$T/c" 2>&1 || fail "call self: $(cat "$T/c")"
grep -q 'hello from the workspace' "$T/c" || fail "call self said: $(cat "$T/c")"
pic mcp remove self > /dev/null
printf 'PASS: pic mcp serve offers pic'"'"'s own tools over stdio, to another client and to pic\n'
# ---- UTCP manuals through UTCP's MCP bridge (experimental), added with pic mcp add ---------------
mkdir "$T/su"
printf 'weather-manual\n' > "$T/utcp_config.json"
pic mcp add utcp -e UTCP_CONFIG_FILE="$T/utcp_config.json" -- env FAKE_MCP_UTCP=1 "$MCP" --stdio "$T/su" 120 legacy \
> "$T/o" 2> "$T/e" || fail "add the bridge: $(cat "$T/e")"
grep -q "Added utcp (stdio): 4 tools" "$T/o" || fail "add the bridge said: $(cat "$T/o")"
printf 'tool raw mcp__utcp__call_tool {"tool_name":"weather.get","tool_args":{"city":"Oslo"}}\ntext done\n' > "$T/s9"
fresh
pic submit job 'utcp' > /dev/null
pic run --script "$T/s9" > /dev/null 2> "$T/e" || fail "run utcp: $(cat "$T/e")"
declared | grep -q 'mcp__utcp__register_manual' || fail "the bridge's tools: $(declared)"
result mcp__utcp__call_tool | grep -q 'utcp weather.get: {"city":"Oslo"} from weather-manual' ||
fail "a UTCP call through the bridge: $(result mcp__utcp__call_tool)"
pic mcp remove utcp > /dev/null
printf 'PASS: a UTCP manual'"'"'s tools are called through its MCP bridge, added with pic mcp add\n'