#!/bin/sh
# Unfragile: add the MCP server to the coding agents on this machine, asking before each one.
# Source: https://unfragile.ai/install.sh   Docs: https://unfragile.ai/docs/agents
# It runs "unfragile setup" from the npm package: one entry named "unfragile" per agent you say yes to, a backup of any
# file it edits, never a model, a base URL, a key or another server. Nothing is downloaded but the package, by npx.
set -eu
PKG="@unfragile/mcp-server"

if ! command -v npx >/dev/null 2>&1; then
  cat <<EOF
unfragile: npx was not found (it comes with Node.js 18 or later). Add this block to your MCP client's configuration:

{
  "mcpServers": {
    "unfragile": { "url": "https://unfragile.ai/api/mcp" }
  }
}

Details: https://unfragile.ai/docs/agents
EOF
  exit 0
fi

# curl | sh feeds this script on stdin, so the questions are asked on the terminal itself
if [ -r /dev/tty ] && [ -w /dev/tty ]; then
  npx -y -p "$PKG@latest" unfragile setup </dev/tty
else
  npx -y -p "$PKG@latest" unfragile setup --dry-run
  echo "unfragile: there is no terminal to ask in, so nothing was changed. To add it: npx -y -p $PKG unfragile setup"
fi
