#!/bin/bash # NodeMCP macOS Agent: SHA-pinned agent and official Python prerequisite. set -euo pipefail umask 077 agent_url='https://app.nodemcp.dev/install/nodemcp-agent-macos-v1.0.2.tar.gz' agent_sha='c118f102173c38856c9105424ff43f3ec59cbf94890a1b56a362423a4e7efa0c' python_url='https://www.python.org/ftp/python/3.14.10/python-3.14.10-macos11.pkg' python_sha='286c7018aa2730f6747ca433dccdc2b31be45f078a9ea0ffa44d5f06368fd2d7' mode='--install' if [ "$#" -gt 0 ]; then mode="$1"; fi if [ "$#" -eq 2 ] && [ "$1" = '--uninstall' ] && [ "$2" = '--purge' ]; then mode='--purge' elif [ "$#" -gt 1 ]; then echo '[NodeMCP Agent] Invalid options' >&2; exit 2; fi case "$mode" in --install|--upgrade|--check|--dry-run|--uninstall|--purge) ;; *) echo 'Usage: NodeMCP Agent [--install|--upgrade|--check|--dry-run|--uninstall [--purge]]' >&2; exit 2 ;; esac [ "$(uname -s)" = Darwin ] || { echo '[NodeMCP] macOS required' >&2; exit 1; } [ "$(id -u)" -ne 0 ] || { echo '[NodeMCP] Run as macOS desktop user, WITHOUT sudo' >&2; exit 1; } msg(){ printf '[NodeMCP Agent] %s\n' "$*"; } fail(){ msg "ERROR: $*" >&2; exit 1; } find_python(){ local item found for item in python3.14 python3.13 python3.12 python3 /Library/Frameworks/Python.framework/Versions/3.14/bin/python3.14 /Library/Frameworks/Python.framework/Versions/3.13/bin/python3.13 /Library/Frameworks/Python.framework/Versions/3.12/bin/python3.12 /usr/local/bin/python3 /opt/homebrew/bin/python3; do if command -v "$item" >/dev/null 2>&1; then found="$(command -v "$item")" if "$found" -c 'import sys;assert sys.version_info >= (3,12)' >/dev/null 2>&1; then printf '%s\n' "$found"; return 0; fi fi done return 1 } tmp="$(mktemp -d)" trap 'rm -rf "$tmp"' EXIT msg 'Downloading verified NodeMCP agent...' curl -fsSL --proto '=https' --tlsv1.2 --connect-timeout 15 --retry 2 --max-time 120 "$agent_url" -o "$tmp/agent.tar.gz" printf '%s %s\n' "$agent_sha" "$tmp/agent.tar.gz" | shasum -a 256 -c - >/dev/null || fail 'Agent archive checksum mismatch' tar -xzf "$tmp/agent.tar.gz" -C "$tmp" dir="$tmp/macos-system-v1.0.2" [ -f "$dir/SHA256SUMS" ] || fail 'Agent manifest missing' (cd "$dir" && shasum -a 256 -c SHA256SUMS >/dev/null) || fail 'Agent module checksum mismatch' if [ "$mode" = '--uninstall' ] || [ "$mode" = '--purge' ]; then sudo -H /bin/bash "$dir/uninstall-macos-system.sh" "$([ "$mode" = '--purge' ] && echo --purge || echo --keep-data)" exit 0 fi python_bin="$(find_python || true)" if [ -z "$python_bin" ]; then if [ "$mode" = '--check' ] || [ "$mode" = '--dry-run' ]; then msg 'Python not found; it will be installed automatically with --install.' msg 'NodeMCP Agent package verified; no files or services modified.' exit 0 fi msg 'Downloading official Python 3.14.10 universal2 installer...' curl -fsSL --proto '=https' --tlsv1.2 --connect-timeout 20 --retry 3 --max-time 600 "$python_url" -o "$tmp/python.pkg" printf '%s %s\n' "$python_sha" "$tmp/python.pkg" | shasum -a 256 -c - >/dev/null || fail 'Python package checksum mismatch' signature="$(/usr/sbin/pkgutil --check-signature "$tmp/python.pkg")" || fail 'Invalid Python Apple installer signature' printf '%s\n' "$signature" | grep -F 'Developer ID Installer: Python Software Foundation (' >/dev/null || fail 'Unexpected Python installer signer' msg 'Python archive SHA-256 and Developer ID verified.' msg 'macOS may request your administrator password to install Python and the NodeMCP Agent system service.' sudo -v || fail 'Administrator authorization required' sudo /usr/sbin/installer -pkg "$tmp/python.pkg" -target / || fail 'Python installation failed' python_bin="$(find_python || true)" [ -n "$python_bin" ] || fail 'Python install completed but Python 3.12+ not found' helper='/Applications/Python 3.14/Install Certificates.command' [ -f "$helper" ] || fail 'Official Python SSL certificate initializer missing' msg 'Initializing Python SSL certificates...' /bin/sh "$helper" || fail 'SSL certificate initialization failed' msg "Python ready: $("$python_bin" -V 2>&1)" else msg "Using existing Python: $("$python_bin" -V 2>&1)" fi if [ "$mode" = '--check' ] || [ "$mode" = '--dry-run' ]; then msg "Archive verified; preflight PASS. Nothing installed."; exit 0 fi if [ -r /etc/ssl/cert.pem ]; then export PIP_CERT=/etc/ssl/cert.pem; export SSL_CERT_FILE=/etc/ssl/cert.pem; fi msg "Installing NodeMCP Agent system service (macOS may request your administrator password)..." sudo -H /bin/bash "$dir/install-macos-system.sh" "$(id -u)" "$HOME"