#!/bin/bash # ChatMCP Agent CLI Installer # Native Windows shells lack the POSIX runtime required by managed agents. case "$OSTYPE" in msys*|cygwin*|win32*) echo "Windows setup requires WSL 2 / Ubuntu. Open Settings > Windows setup instructions." echo "Run this installer inside Ubuntu, not Git Bash or a native Windows shell." exit 1 ;; esac # Cleanup on exit TEMP_FILE="agent-cli.sh.tmp" trap 'rm -f "$TEMP_FILE"' EXIT GLOBAL_INSTALL_DIR="$HOME/.chatmcp" GLOBAL_INSTALL_PATH="$GLOBAL_INSTALL_DIR/agent-cli.sh" LOCAL_INSTALL_PATH="$(pwd)/agent-cli.sh" install_jetspace_command() { local rc_file="$1" local marker_start="# >>> chatmcp jetspace >>>" local marker_end="# <<< chatmcp jetspace <<<" mkdir -p "$(dirname "$rc_file")" touch "$rc_file" if grep -Fq "$marker_start" "$rc_file"; then return 0 fi cat >> "$rc_file" < /dev/null; then echo "Detected macOS with Homebrew. Installing gum via brew..." brew install gum else echo "Error: Homebrew is required to install gum on macOS." echo "Install Homebrew first: https://brew.sh" exit 1 fi elif [[ "$OSTYPE" == "linux-gnu"* ]]; then # Linux - detect package manager if command -v apt-get &> /dev/null; then # Debian/Ubuntu echo "Detected Debian/Ubuntu. Installing gum via apt..." sudo mkdir -p /etc/apt/keyrings curl -fsSL https://repo.charm.sh/apt/gpg.key | sudo gpg --dearmor -o /etc/apt/keyrings/charm.gpg echo "deb [signed-by=/etc/apt/keyrings/charm.gpg] https://repo.charm.sh/apt/ * *" | sudo tee /etc/apt/sources.list.d/charm.list sudo apt update && sudo apt install -y gum elif command -v dnf &> /dev/null; then # Fedora/RHEL echo "Detected Fedora/RHEL. Installing gum via dnf..." echo '[charm] name=Charm baseurl=https://repo.charm.sh/yum/ enabled=1 gpgcheck=1 gpgkey=https://repo.charm.sh/yum/gpg.key' | sudo tee /etc/yum.repos.d/charm.repo sudo dnf install -y gum elif command -v yum &> /dev/null; then # CentOS/older RHEL echo "Detected CentOS/RHEL. Installing gum via yum..." echo '[charm] name=Charm baseurl=https://repo.charm.sh/yum/ enabled=1 gpgcheck=1 gpgkey=https://repo.charm.sh/yum/gpg.key' | sudo tee /etc/yum.repos.d/charm.repo sudo yum install -y gum elif command -v pacman &> /dev/null; then # Arch Linux echo "Detected Arch Linux. Installing gum via pacman..." sudo pacman -Sy --noconfirm gum elif command -v zypper &> /dev/null; then # openSUSE echo "Detected openSUSE. Installing gum via zypper..." echo '[charm] name=Charm baseurl=https://repo.charm.sh/yum/ enabled=1 gpgcheck=1 gpgkey=https://repo.charm.sh/yum/gpg.key' | sudo tee /etc/zypp/repos.d/charm.repo sudo zypper refresh && sudo zypper install -y gum elif command -v apk &> /dev/null; then # Alpine Linux echo "Detected Alpine Linux. Installing gum via apk..." apk add --no-cache gum elif command -v nix-env &> /dev/null; then # NixOS echo "Detected NixOS. Installing gum via nix-env..." nix-env -iA nixpkgs.gum elif command -v brew &> /dev/null; then # Linux with Homebrew (Linuxbrew) echo "Detected Linuxbrew. Installing gum via brew..." brew install gum else echo "Error: Could not detect package manager to install gum." echo "" echo "Please install gum manually: https://github.com/charmbracelet/gum#installation" exit 1 fi else echo "Error: Unsupported operating system: $OSTYPE" echo "Please install gum manually: https://github.com/charmbracelet/gum#installation" exit 1 fi # Verify installation if ! command -v gum &> /dev/null; then echo "" echo "Error: gum installation failed. Please install manually:" echo "https://github.com/charmbracelet/gum#installation" exit 1 fi echo "" echo "✓ gum installed successfully!" echo "" } # Check if gum is installed, if not install it automatically if ! command -v gum &> /dev/null; then echo "gum is not installed. It's required for this installer." echo "" # Ask for permission to install (using basic bash since gum isn't available yet) read -p "Would you like to install gum automatically? [Y/n] " -n 1 -r < /dev/tty echo "" if [[ ! $REPLY =~ ^[Nn]$ ]]; then install_gum else echo "Installation cancelled. Please install gum manually:" echo "https://github.com/charmbracelet/gum#installation" exit 1 fi fi # Welcome banner gum style \ --border double \ --border-foreground 212 \ --padding "1 2" \ --margin "1 0" \ --align center \ "ChatMCP Agent CLI - Quick Setup" # Show instructions gum style --foreground 245 "This installer will set up the Agent CLI for managing your AI agents." echo "" gum style --bold "To get your API key:" echo "" echo "$(gum style --foreground 212 " • New user?") $(gum style --foreground 250 "Register at: https://chatmcp.space/register")" echo "$(gum style --foreground 212 " • Existing user?") $(gum style --foreground 250 "Get key from: https://chatmcp.space/settings")" echo "" gum style --italic --foreground 245 "Your API key looks like: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" echo "" # Prompt for API key with gum (redirect from /dev/tty for curl|bash compatibility) gum style --foreground 245 "Enter your API key below (input will be hidden for security):" API_KEY=$(gum input --password --prompt "🔑 " < /dev/tty) if [ -z "$API_KEY" ]; then echo "" gum style --foreground 196 "✗ Error: API key is required" exit 1 fi # Trim whitespace to avoid hidden copy/paste characters API_KEY="$(echo "$API_KEY" | tr -d '[:space:]')" # Basic UUID format validation if ! [[ "$API_KEY" =~ ^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$ ]]; then echo "" gum style --foreground 196 "✗ Error: Invalid API key format" gum style --foreground 245 "API key should be in UUID format (e.g., xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)" exit 1 fi echo "" gum style --foreground 46 "✓ API key validated" echo "" # Fetch customized script with spinner. Body, headers and HTTP code are captured # to separate files so an optional integrity checksum can be verified against the # RAW downloaded bytes (before any shell reconstruction that could alter them). CLI_BODY_FILE="/tmp/cli_body_$$.sh" CLI_HDR_FILE="/tmp/cli_hdr_$$.txt" CLI_CODE_FILE="/tmp/cli_code_$$.txt" gum spin --spinner dot --title "Fetching your customized CLI script..." -- \ bash -c "curl -sL -H 'X-API-Key: $API_KEY' -D '$CLI_HDR_FILE' -o '$CLI_BODY_FILE' -w '%{http_code}' 'https://chatmcp.space/api/agent/cli-script' > '$CLI_CODE_FILE' 2>/dev/null" if [ ! -f "$CLI_BODY_FILE" ] || [ ! -f "$CLI_CODE_FILE" ]; then echo "" gum style --foreground 196 "✗ Error: Failed to fetch script (network error)" rm -f "$CLI_BODY_FILE" "$CLI_HDR_FILE" "$CLI_CODE_FILE" exit 1 fi HTTP_CODE=$(cat "$CLI_CODE_FILE") rm -f "$CLI_CODE_FILE" SCRIPT=$(cat "$CLI_BODY_FILE") # Check for errors if [ "$HTTP_CODE" != "200" ]; then echo "" gum style --foreground 196 "✗ Error: Failed to fetch script (HTTP $HTTP_CODE)" if [ "$HTTP_CODE" == "401" ]; then gum style --foreground 245 "Invalid API key. Please check your key and try again." gum style --foreground 245 "Get your key at: https://chatmcp.space/settings" elif [ "$HTTP_CODE" == "500" ]; then gum style --foreground 245 "Server error. Please try again later or contact support." else gum style --foreground 245 "Unexpected error. Please try again." fi rm -f "$CLI_BODY_FILE" "$CLI_HDR_FILE" exit 1 fi echo "" gum style --foreground 46 "✓ Script fetched successfully" # Optional integrity check. If the server advertised an X-Script-SHA256 header, # verify the downloaded bytes match it. This is backward-compatible: older # servers omit the header, so we skip verification (with a note) rather than # failing the install. It catches truncated/corrupted/tampered downloads in # transit — it is NOT a defense against a malicious operator (who controls both # the script and the checksum). EXPECTED_SHA=$(grep -i '^x-script-sha256:' "$CLI_HDR_FILE" 2>/dev/null | tail -n1 | tr -d '\r' | awk '{print $2}') if [ -n "$EXPECTED_SHA" ]; then if command -v openssl >/dev/null 2>&1; then ACTUAL_SHA=$(openssl dgst -sha256 "$CLI_BODY_FILE" 2>/dev/null | awk '{print $NF}') if [ "$ACTUAL_SHA" = "$EXPECTED_SHA" ]; then gum style --foreground 46 "✓ Script integrity verified (SHA-256)" else echo "" gum style --foreground 196 "✗ Error: Script integrity check FAILED" gum style --foreground 245 "Expected: $EXPECTED_SHA" gum style --foreground 245 "Actual: $ACTUAL_SHA" gum style --foreground 245 "The download may be corrupted or tampered with. Aborting install." rm -f "$CLI_BODY_FILE" "$CLI_HDR_FILE" exit 1 fi else gum style --foreground 214 "⚠️ openssl not found — skipping integrity verification" fi fi # Optional preview (default to No for faster installation) if gum confirm "Preview script before saving?" --default=false < /dev/tty; then echo "" gum style --border normal --border-foreground 212 --padding "0 1" "First 20 lines of script:" echo "$SCRIPT" | head -20 | gum style --foreground 250 echo "" if ! gum confirm "Continue with installation?" --default=true < /dev/tty; then gum style --foreground 245 "Installation cancelled." rm -f "$CLI_BODY_FILE" "$CLI_HDR_FILE" exit 0 fi echo "" fi # Save the verified script bytes directly (preserves exact server output). mv "$CLI_BODY_FILE" "$LOCAL_INSTALL_PATH" rm -f "$CLI_HDR_FILE" chmod +x "$LOCAL_INSTALL_PATH" # Save reusable global copy for shell command usage mkdir -p "$GLOBAL_INSTALL_DIR" cp "$LOCAL_INSTALL_PATH" "$GLOBAL_INSTALL_PATH" chmod +x "$GLOBAL_INSTALL_PATH" gum style --foreground 46 "✓ Saved to: $LOCAL_INSTALL_PATH" gum style --foreground 46 "✓ Global launcher saved to: $GLOBAL_INSTALL_PATH" echo "" JETSPACE_INSTALLED=false JETSPACE_TARGETS="" if gum confirm "Install the 'jetspace' shell command for quick agent setup in any folder?" --default=true < /dev/tty; then install_jetspace_command "$HOME/.bashrc" JETSPACE_TARGETS="$HOME/.bashrc" if [ "$(basename "${SHELL:-}")" = "zsh" ] || [ -f "$HOME/.zshrc" ]; then install_jetspace_command "$HOME/.zshrc" JETSPACE_TARGETS="$JETSPACE_TARGETS $HOME/.zshrc" fi JETSPACE_INSTALLED=true gum style --foreground 46 "✓ Installed jetspace shell command" echo "" fi gum style \ --border double \ --border-foreground 46 \ --padding "1 2" \ --margin "1 0" \ --align center \ "✓ Setup Complete!" echo "" gum style --bold "Your CLI script has been saved to:" gum style --foreground 212 " $LOCAL_INSTALL_PATH" gum style --foreground 212 " $GLOBAL_INSTALL_PATH" echo "" gum style --bold "To get started, run:" gum style --foreground 212 " bash agent-cli.sh" if [ "$JETSPACE_INSTALLED" = "true" ]; then echo "" gum style --bold "After opening a new shell, you can run from any project folder:" gum style --foreground 212 " jetspace" gum style --foreground 245 "Shell config updated:$JETSPACE_TARGETS" fi echo ""