#!/bin/sh
# /mcp in the terminal interface, on a pseudo-terminal: the MCP servers in the settings panel's
# frame, one needing attention first, then the connected ones with their tool count and revision;
# Enter lists a server's tools, where e leaves one out and offers it again; e turns a server off
# and on, d removes one after y, a and the add row
# put /mcp add in the editor, and /mcp add saves and lists a server. The rows come from the RPC
# commands behind the screen. In the box over the transcript (fullscreen) and docked (regular).
set -eu
PIC=${PIC:?set PIC to the pic binary}
DRIVE=${DRIVE:?set DRIVE to the pty_drive binary}
MCP=${MCP:-$(dirname "$PIC")/../fake_mcp}
SQLITE3=${SQLITE3:-sqlite3}
T=$(mktemp -d "${TMPDIR:-/tmp}/pic-tui-mcp.XXXXXX")
T=$(cd "$T" && pwd -P)
spid=
cleanup() { [ -n "$spid" ] && kill "$spid" 2>/dev/null || :; rm -rf -- "$T"; }
trap cleanup EXIT HUP INT TERM
fail() { printf 'FAIL: %s\n' "$*" >&2; exit 1; }
mkdir "$T/ws" "$T/home" "$T/srv"
for v in $(env | sed -nE 's/^([A-Za-z0-9_]*_(API_KEY|TOKEN))=.*/\1/p'); do unset "$v"; done
export HOME="$T/home" PIC_MODEL_CATALOG=offline PIC_PI_EXPORT=0 PIC_NO_NATIVE_CLIPBOARD=1 PIC_NO_KEY_CHECKS=1
"$MCP" "$T/port" "$T/srv" 120 modern &
spid=$!
i=0
while [ ! -s "$T/port" ]; do i=$((i + 1)); [ "$i" -lt 600 ] || fail "server"; sleep 0.05; done
URL="http://127.0.0.1:$(cat "$T/port")/mcp"
pic() { "$PIC" --db "$T/j" --workspace "$T/ws" "$@"; }
pic mcp add cur "$URL" > /dev/null 2>&1 || fail "add cur"
pic mcp add broken -- "$T/no-such-program" > /dev/null 2>&1 && fail "a missing program was reached"
pic mcp exposure cur 'wr*' hidden > /dev/null || fail "hide wr*"
# a trusted project narrows cur to deferred; offering a tool writes only your own choice
printf '{"mcpServers":{"cur":{"exposure":"deferred","toolExposure":{"slow":"hidden"}}}}\n' > "$T/ws/.mcp.json"
pic mcp exposure cur slow hidden > /dev/null || fail "hide slow"
pic trust yes > /dev/null
screen() { perl -pe 's/\e\[[0-9;?]*[A-Za-z]//g; s/\e\][^\a\e]*(\a|\e\\)//g; s/\r//g' "$1" | grep -v '^\s*$' | tail -20; }
drive() { # NAME MODE: pic on a pseudo-terminal, the script on stdin
"$DRIVE" "$T/$1.tx" 110 32 "$PIC" --tui-mode "$2" --db "$T/j" --workspace "$T/ws" || fail "$1: $(screen "$T/$1.tx")"
}
enabled() { "$SQLITE3" "$HOME/.pic/pic.sqlite" "SELECT coalesce((SELECT enabled FROM picu_mcp_server WHERE name = '$1'), '-')"; }
# The list, problems first; Enter lists the tools, e leaves one out; e turns a server off and on again.
drive list fullscreen <<'EOF2'
expect pic
send /mcp\r
expect MCP servers
expect Needs attention
expect ! not connected
expect 7 tools · MCP 2026-07-28
expect enter: tools · r: reconnect · e: on/off · d: remove · a: add ─
send \e[B
expect 127.0.0.1:
send \r
expect MCP tools
expect cur · 5 tools offered · 2 left out
expect deferred · read-only
expect e: leave it out · esc: the servers
send \e[F
expect e: offer it
send e
expect slow: offered by you; the project's file still leaves it out
send \e[H
wait 300
send e
expect hidden · read-only
expect echo is left out from the next request
send e
expect echo is offered from the next request
wait 300
send \e[F
wait 200
send \e[A
wait 200
send e
expect write_note is offered from the next request
send \e
expect MCP servers
send e
expect Turned off
expect cur is off
send e
expect cur is on
send \e
wait 200
send \x04\x04
EOF2
[ "$(enabled cur)" = 1 ] || fail "cur is not on again: $(enabled cur)"
[ "$("$SQLITE3" "$HOME/.pic/pic.sqlite" "SELECT coalesce(json_extract(tool_exposure, '\$.echo'), '-') || ' ' || json_extract(tool_exposure, '\$.write_note') FROM picu_mcp_server WHERE name = 'cur'")" = '- direct' ] ||
fail "e among the tools did not leave echo out: $("$SQLITE3" "$HOME/.pic/pic.sqlite" "SELECT tool_exposure FROM picu_mcp_server")"
printf 'PASS: /mcp lists the servers, problems first, lists and leaves out tools, and turns one off and on\n'
# d then y removes a server, any other key keeps it; a puts /mcp add in the editor, which adds one.
drive edit regular <<EOF2
expect pic
send /mcp\r
expect MCP servers
send d
expect Remove broken and its sealed values? y: remove
send n
wait 200
send d
expect Remove broken
send y
expect Removed broken.
send a
wait 200
send more $URL\r
expect Added more: 7 tools, MCP 2026-07-28
send \x04\x04
EOF2
[ "$(enabled broken)" = - ] || fail "broken outlived d y"
[ "$(enabled more)" = 1 ] || fail "/mcp add did not save more"
printf 'PASS: d removes a server after y, and a or /mcp add adds one\n'