#!/usr/bin/env bash # X402 Router: agent installer, doctor and capabilities # Source: https://router-production-ab89.up.railway.app/install.sh (plain text: read it before you run it) # # setup (default) Registers the hosted MCP server https://router-production-ab89.up.railway.app/mcp (Streamable HTTP, public, no auth, no keys) # in Claude Code, Cursor (~/.cursor/mcp.json) and/or Codex (~/.codex/config.toml). # Any existing config file is copied to ..bak before it is changed. # doctor Read-only check: /health, /mcp initialize + tools/list (expects 9 tools), /route/paid HTTP 402. # Sends no payment. # capabilities The 7 free read-only tools and the 2 paid tools, with prices. # # This script never asks for, reads or stores keys or wallets, never pays, never uses sudo, and only writes the # three client config files above (plus their .bak copies). # # Usage: # curl -fsSL https://router-production-ab89.up.railway.app/install.sh | bash # curl -fsSL https://router-production-ab89.up.railway.app/install.sh | bash -s -- --client cursor --dry-run # curl -fsSL https://router-production-ab89.up.railway.app/install.sh | bash -s -- doctor # curl -fsSL https://router-production-ab89.up.railway.app/install.sh | bash -s -- capabilities set -euo pipefail ROUTER="${X402_ROUTER_URL:-https://router-production-ab89.up.railway.app}" ROUTER="${ROUTER%/}" MCP_URL="$ROUTER/mcp" NAME="${X402_MCP_NAME:-x402-router}" case "$NAME" in ''|*[!A-Za-z0-9_-]*) printf 'X402_MCP_NAME must be letters, digits, "-" or "_" only (got: %s)\n' "$NAME" >&2; exit 2 ;; esac CMD="setup" CLIENT="all" DRY=0 TS="$(date +%Y%m%d%H%M%S)" FAILS=0 usage() { cat <<'USAGE' X402 Router installer bash install.sh [setup] [--client all|claude|cursor|codex] [--dry-run] bash install.sh doctor bash install.sh capabilities Environment: X402_ROUTER_URL (default https://router-production-ab89.up.railway.app), X402_MCP_NAME (default x402-router) USAGE } say() { printf '%s\n' "$*"; } run() { if [ "$DRY" = 1 ]; then say " [dry-run] $*"; else "$@" /dev/null 2>&1; then say " claude CLI not found; skipped. Manual: claude mcp add --transport http --scope user $NAME $MCP_URL" return 0 fi if claude mcp get "$NAME" /dev/null 2>&1; then say " '$NAME' is already configured; no change." return 0 fi backup "$HOME/.claude.json" run claude mcp add --transport http --scope user "$NAME" "$MCP_URL" if [ "$DRY" = 0 ]; then say " added '$NAME' -> $MCP_URL (user scope)"; fi } setup_cursor() { local dir="$HOME/.cursor" local f="$dir/mcp.json" say "Cursor ($f):" if [ -f "$f" ] && grep -q "\"$NAME\"" "$f"; then if grep -qF "$MCP_URL" "$f"; then say " '$NAME' is already configured; no change."; else say " '$NAME' exists with a different url; left unchanged. Point it at $MCP_URL by hand if you want."; fi return 0 fi if [ ! -f "$f" ]; then if [ "$DRY" = 1 ]; then say " [dry-run] create $f with mcpServers.$NAME.url = $MCP_URL"; return 0; fi mkdir -p "$dir" printf '{\n "mcpServers": {\n "%s": {\n "url": "%s"\n }\n }\n}\n' "$NAME" "$MCP_URL" > "$f" say " created $f" return 0 fi if ! command -v python3 >/dev/null 2>&1; then say " python3 not found, so the existing JSON was not edited. Add under \"mcpServers\": \"$NAME\": {\"url\": \"$MCP_URL\"}" return 0 fi backup "$f" if [ "$DRY" = 1 ]; then say " [dry-run] merge mcpServers.$NAME = {\"url\": \"$MCP_URL\"} into $f"; return 0; fi if python3 - "$f" "$NAME" "$MCP_URL" <<'PY' import json, os, sys path, name, url = sys.argv[1:4] with open(path) as fh: raw = fh.read() data = json.loads(raw) if raw.strip() else {} if not isinstance(data, dict) or not isinstance(data.setdefault("mcpServers", {}), dict): sys.exit("not a JSON object with an mcpServers object") data["mcpServers"][name] = {"url": url} with open(path + ".tmp", "w") as fh: json.dump(data, fh, indent=2) fh.write("\n") os.replace(path + ".tmp", path) PY then say " added '$NAME' -> $MCP_URL" else say " could not parse $f; left unchanged (backup kept)."; FAILS=$((FAILS + 1)); fi } codex_has() { # True when NAME is already configured in a Codex config.toml, in ANY of these TOML forms (a second definition would break the file): # [mcp_servers.NAME] / [mcp_servers.NAME.sub] / [mcp_servers."NAME"] tables (also with spaces around the dots) # NAME = {...} / NAME.url = "..." / "NAME".url = "..." under a [mcp_servers] table # mcp_servers.NAME = {...} / mcp_servers.NAME.url = "..." / mcp_servers."NAME".url = "..." dotted keys at any level awk -v n="$NAME" ' BEGIN { q = "[\"\047]?" } { line = $0; sub(/^[ \t]+/, "", line) } line ~ /^\[/ { ms = (line ~ /^\[[ \t]*mcp_servers[ \t]*\][ \t]*(#.*)?$/) } line ~ ("^\\[[ \t]*mcp_servers[ \t]*\\.[ \t]*" q n q "[ \t]*(\\]|\\.)") { f = 1 } ms && line ~ ("^" q n q "[ \t]*(=|\\.)") { f = 1 } line ~ ("^mcp_servers[ \t]*\\.[ \t]*" q n q "[ \t]*(=|\\.)") { f = 1 } END { exit f ? 0 : 1 }' "$1" } toml_check() { # prints "ok", "bad" or "skip" (no python3 with tomllib, i.e. Python < 3.11): is $1 valid TOML? command -v python3 >/dev/null 2>&1 || { echo skip; return 0; } python3 - "$1" <<'PY' 2>/dev/null || echo skip import sys try: import tomllib except Exception: print("skip"); sys.exit(0) try: with open(sys.argv[1], "rb") as fh: tomllib.load(fh) print("ok") except Exception: print("bad") PY } setup_codex() { local f="$HOME/.codex/config.toml" say "Codex ($f):" if [ -f "$f" ] && codex_has "$f"; then say " $NAME is already configured in $f (table, inline or dotted form); no change." return 0 fi backup "$f" if [ "$DRY" = 1 ]; then say " [dry-run] append to $f:"; say " [mcp_servers.$NAME]"; say " url = \"$MCP_URL\""; return 0; fi mkdir -p "$HOME/.codex" local before="skip" after="skip" tmp="$f.new.$$" [ -s "$f" ] && before="$(toml_check "$f")" { if [ -s "$f" ]; then cat "$f"; printf '\n'; fi; printf '[mcp_servers.%s]\nurl = "%s"\n' "$NAME" "$MCP_URL"; } > "$tmp" || { rm -f "$tmp"; say " could not write $f; left unchanged."; FAILS=$((FAILS + 1)); return 0; } after="$(toml_check "$tmp")" if [ "$after" = bad ] && [ "$before" != bad ]; then rm -f "$tmp" say " the edit would have made $f invalid TOML, so it was NOT written (file unchanged). Add by hand:" say " [mcp_servers.$NAME]"; say " url = \"$MCP_URL\"" FAILS=$((FAILS + 1)); return 0 fi # same inode/perms as the original: write the new content into it (keeps symlinks and mode); the .bak above is the rollback cat "$tmp" > "$f" && rm -f "$tmp" say " added [mcp_servers.$NAME] url = \"$MCP_URL\"$( [ "$after" = ok ] && printf ' (TOML validated)' )" } do_setup() { say "X402 Router setup: $MCP_URL (clients: $CLIENT$( [ "$DRY" = 1 ] && printf ', dry run' ))" case "$CLIENT" in all) setup_claude; setup_cursor; setup_codex ;; claude) setup_claude ;; cursor) setup_cursor ;; codex) setup_codex ;; esac say "" say "Next: restart your client, then check: curl -fsSL $ROUTER/install.sh | bash -s -- doctor" say "Free tools never spend; the 2 paid tools only settle when your x402 client signs a payment." [ "$FAILS" = 0 ] } field() { sed -n "s/.*\"$1\": *\"\{0,1\}\([^\",}]*\).*/\1/p" <<<"$2" | head -n 1; } ok() { say "PASS $1: $2"; } bad() { say "FAIL $1: $2"; FAILS=$((FAILS + 1)); } do_doctor() { command -v curl >/dev/null 2>&1 || { say "FAIL curl is required"; exit 1; } say "X402 Router doctor: $ROUTER (read-only; no payment is sent)" local resp meta body code t resp="$(curl -sS -m 20 -w $'\n%{http_code} %{time_total}' "$ROUTER/health" 2>/dev/null || printf '\n000 0')" meta="${resp##*$'\n'}"; body="${resp%$'\n'*}"; code="${meta%% *}"; t="${meta##* }" if [ "$code" = 200 ] && grep -q '"status": *"ok"' <<<"$body"; then ok "health" "HTTP 200 in ${t}s; $(field live_endpoints "$body") live endpoints; registry $(field registry_state "$body"), age $(field registry_age_s "$body")s; last fast pass $(field last_fast_pass_age_s "$body")s ago; top checked in 15 min $(field top_checked_last_15m_pct "$body")%" else bad "health" "HTTP $code from $ROUTER/health" fi local init list init='{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-06-18","capabilities":{},"clientInfo":{"name":"x402-router-doctor","version":"1"}}}' resp="$(curl -sS -m 20 -H 'Content-Type: application/json' -H 'Accept: application/json, text/event-stream' -d "$init" -w $'\n%{http_code} %{time_total}' "$MCP_URL" 2>/dev/null || printf '\n000 0')" meta="${resp##*$'\n'}"; body="${resp%$'\n'*}"; code="${meta%% *}"; t="${meta##* }" if [ "$code" = 200 ] && grep -q '"protocolVersion"' <<<"$body"; then ok "mcp initialize" "HTTP 200 in ${t}s; protocol $(field protocolVersion "$body"); server $(field version "$body")" else bad "mcp initialize" "HTTP $code from $MCP_URL" fi list='{"jsonrpc":"2.0","id":2,"method":"tools/list"}' resp="$(curl -sS -m 20 -H 'Content-Type: application/json' -H 'Accept: application/json, text/event-stream' -d "$list" -w $'\n%{http_code} %{time_total}' "$MCP_URL" 2>/dev/null || printf '\n000 0')" meta="${resp##*$'\n'}"; body="${resp%$'\n'*}"; code="${meta%% *}"; t="${meta##* }" local n missing="" tool n="$( (grep -o '"inputSchema"' <<<"$body" || true) | wc -l | tr -d ' ')" for tool in search_x402_endpoints route_x402_request compare_x402_endpoints get_endpoint_reliability get_x402_changes get_paid_route_requirements get_router_stats route_paid check_endpoint_paid; do grep -q "\"name\": *\"$tool\"" <<<"$body" || missing="$missing $tool"; done if [ "$code" = 200 ] && [ "$n" = 9 ] && [ -z "$missing" ]; then ok "mcp tools/list" "HTTP 200 in ${t}s; $n tools (7 free + 2 paid: route_paid, check_endpoint_paid)" else bad "mcp tools/list" "HTTP $code; $n tools; missing:${missing:- none}" fi local hdrs pr dec amounts resp="$(curl -sS -m 20 -o /dev/null -D - -w $'\n%{http_code} %{time_total}' "$ROUTER/route/paid" 2>/dev/null || printf '\n000 0')" meta="${resp##*$'\n'}"; hdrs="${resp%$'\n'*}"; code="${meta%% *}"; t="${meta##* }" pr="$(tr -d '\r' <<<"$hdrs" | sed -n 's/^[Pp][Aa][Yy][Mm][Ee][Nn][Tt]-[Rr][Ee][Qq][Uu][Ii][Rr][Ee][Dd]: *//p' | head -n 1)" if [ "$code" = 402 ] && [ -n "$pr" ]; then dec="$( (base64 -d <<<"$pr" || base64 -D <<<"$pr") 2>/dev/null || true)" amounts="$(grep -o '"amount":"[0-9]*"' <<<"$dec" | sort -u | tr '\n' ' ' || true)" ok "route/paid 402" "HTTP 402 in ${t}s with PAYMENT-REQUIRED ${amounts:+(}${amounts% }${amounts:+)}; nothing paid" else bad "route/paid 402" "expected HTTP 402 + PAYMENT-REQUIRED, got HTTP $code" fi say "" say "Server-side self-check (JSON): $ROUTER/doctor" if [ "$FAILS" = 0 ]; then say "doctor: all checks passed"; else say "doctor: $FAILS check(s) failed"; return 1; fi } do_capabilities() { cat <<'CAPS' X402 Router MCP tools (https://router-production-ab89.up.railway.app/mcp, Streamable HTTP, public host, no auth) TOOL TIER PRICE READ-ONLY SPENDS search_x402_endpoints free $0 yes never route_x402_request free $0 yes never compare_x402_endpoints free $0 yes never get_endpoint_reliability free $0 yes never get_x402_changes free $0 yes never get_paid_route_requirements free $0 yes never get_router_stats free $0 yes never route_paid paid 0.01 USDC no only what your client signs check_endpoint_paid paid 0.002 USDC no only what your client signs Free tools (7) are read-only: they never spend, never change state and need no wallet. Paid tools (2) settle only when your x402 client explicitly signs a payment in params._meta["x402/payment"] for the exact amount above to the receivers in https://router-production-ab89.up.railway.app/.well-known/x402 (Base 0x4a1ae0abcd9a6fc7bbcc2a51fa4bb1d6af1b76cc, Solana 7F9nMqE3Bipa7RAgYykxWETRCrixWJLyDHKbZNdv9XEK). Without a payment they return isError + PaymentRequired and nothing is charged. Machine-readable: https://router-production-ab89.up.railway.app/capabilities CAPS } main() { while [ $# -gt 0 ]; do case "$1" in setup|install) CMD=setup ;; doctor) CMD=doctor ;; capabilities|caps) CMD=capabilities ;; help|-h|--help) CMD=help ;; --client) [ $# -ge 2 ] || { say "error: --client needs a value" >&2; exit 2; }; CLIENT="$2"; shift ;; --client=*) CLIENT="${1#--client=}" ;; --dry-run|-n) DRY=1 ;; *) say "error: unknown argument: $1" >&2; usage >&2; exit 2 ;; esac shift done case "$CLIENT" in all|claude|cursor|codex) ;; *) say "error: --client must be all, claude, cursor or codex" >&2; exit 2 ;; esac case "$CMD" in setup) do_setup ;; doctor) do_doctor ;; capabilities) do_capabilities ;; help) usage ;; esac } main "$@"