#!/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
printf 'PASS: the instructions MCP servers give are in the system prompt, unless hidden or --no-mcp\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' > /dev/null 2> "$T/e" || fail "add bang: $(cat "$T/e")"
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' > /dev/null 2> "$T/e" && fail "a failing !command was reached"
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 > /dev/null 2> "$T/e" ||
    fail "add sbang: $(cat "$T/e")"
grep -qx tok-from-cmd "$T/sb/env" || fail "the !command environment value: $(cat "$T/sb/env" 2>/dev/null)"
for s in hh bang bad sbang; do pic mcp remove "$s" > /dev/null; done
printf 'PASS: headersHelper adds and replaces headers; a !command value is the command'"'"'s output\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 '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 me@example.com 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 me@example.com 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 {}\ntext done\n' > "$T/s8"
fresh
pic submit job 'change' > /dev/null
pic run --script "$T/s8" > /dev/null 2> "$T/e" || fail "run change: $(cat "$T/e")"
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)"
pic mcp remove chg > /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
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")"
# 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'
