mcp/run.sh (view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
#!/bin/sh
# Starts the MCP server (server.mjs), installing its pinned dependencies first when they are
# missing or the lockfile changed. Arguments go to server.mjs: none for stdio, which MCP clients
# launch once per session, or --http HOST:PORT for a shared server. --install-only installs and
# exits, which is worth doing once after cloning so the first client start is not a long one.
#
# stdout is the MCP protocol, so everything here writes to stderr.
set -eu
here="$(cd "$(dirname "$0")" && pwd)"
# MCP clients start servers with a bare environment: Claude Desktop in particular has no node on
# its PATH. Add the usual places one is installed, without disturbing an inherited PATH.
PATH="${PATH}:${HOME}/.local/share/n/bin:${HOME}/.volta/bin:/opt/homebrew/bin:/usr/local/bin"
export PATH
# nvm installs per version, with no stable bin directory, so take the newest it has.
if ! command -v node >/dev/null 2>&1; then
for dir in "${HOME}"/.nvm/versions/node/*/bin; do
[ -x "${dir}/node" ] && nvm_bin="${dir}"
done
[ -n "${nvm_bin:-}" ] && PATH="${nvm_bin}:${PATH}" && export PATH
fi
if ! command -v node >/dev/null 2>&1; then
echo "pi-chrome-mcp: no node on PATH; install Node 20.6 or newer, or start this with one on PATH" >&2
exit 1
fi
stamp="${here}/node_modules/.pi-chrome-mcp-deps"
if ! cmp -s "${here}/package-lock.json" "${stamp}" 2>/dev/null; then
# Two clients starting at once would otherwise run npm ci over each other.
lock="${here}/.install-lock"
tries=0
until mkdir "${lock}" 2>/dev/null; do
tries=$((tries + 1))
if [ "${tries}" -gt 120 ]; then
echo "pi-chrome-mcp: gave up waiting for ${lock}; remove it if no install is running" >&2
exit 1
fi
sleep 1
done
trap 'rmdir "${lock}" 2>/dev/null || true' EXIT
if ! cmp -s "${here}/package-lock.json" "${stamp}" 2>/dev/null; then
echo "pi-chrome-mcp: installing pinned dependencies" >&2
npm_config_cache="${here}/.npm-cache" \
npm ci --prefix "${here}" --no-audit --no-fund --silent >&2
cp "${here}/package-lock.json" "${stamp}"
fi
rmdir "${lock}" 2>/dev/null || true
trap - EXIT
fi
if [ "${1:-}" = "--install-only" ]; then
echo "pi-chrome-mcp: ready. The companion extension to load in chrome://extensions: $(cd "${here}/../extensions/chrome-profile-bridge/browser-extension" && pwd)" >&2
exit 0
fi
exec node "${here}/server.mjs" "$@"
|