#!/usr/bin/env bash # Public HTTPS address of the MCP server for the Claude connector: Tailscale Funnel # (service claude-funnel). The address https://zp-agent..ts.net/mcp stays the # same across restarts; the Tailscale login is kept in the tailscale-state volume, so # TS_AUTHKEY (in .env, never in Git) is needed only for the first start. # start - start the MCP server and Funnel, write the address to .env # (ZP_MCP_PUBLIC_URL, used by OAuth) and check it from outside # status - show the address and whether it answers set -euo pipefail project_root="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)" env_file="$project_root/.env" compose=(bash "$project_root/scripts/compose.sh" --profile claude) funnel_id() { "${compose[@]}" ps -q claude-funnel; } mcp_id() { "${compose[@]}" ps -q claude-mcp; } current_url() { local id; id="$(funnel_id)" [[ -n "$id" ]] || return 0 docker exec "$id" tailscale status --json 2>/dev/null \ | python3 -c 'import json,sys; n=json.load(sys.stdin).get("Self",{}).get("DNSName","").rstrip("."); print("https://"+n if n else "")' \ || true } configured() { grep -E '^ZP_MCP_PUBLIC_URL=' "$env_file" | cut -d= -f2- || true; } public_ok() { [[ -n "${1:-}" ]] && [[ "$(curl -s -o /dev/null -w '%{http_code}' -m 20 "$1/.well-known/oauth-authorization-server" || true)" == 200 ]] } funnel_hint() { # Tailscale prints a link when Funnel or HTTPS is not enabled for the tailnet yet local id; id="$(funnel_id)" [[ -n "$id" ]] && docker logs "$id" 2>&1 | grep -oE 'https://login\.tailscale\.com/[^ "]+' | tail -1 || true } case "${1:-status}" in start) if ! grep -qE '^TS_AUTHKEY=tskey-' "$env_file" && [[ -z "$(funnel_id)" ]]; then echo "Chýba TS_AUTHKEY v .env (len na prvé prihlásenie zariadenia do Tailscale)." >&2 exit 1 fi "${compose[@]}" up -d --wait claude-mcp >/dev/null "${compose[@]}" up -d claude-funnel >/dev/null url="" for _ in $(seq 1 60); do url="$(current_url)" [[ -n "$url" ]] && break sleep 2 done [[ -n "$url" ]] || { echo "Tailscale sa neprihlásil. $(funnel_hint)" >&2; exit 1; } if [[ "$(configured)" != "$url/mcp" ]]; then tmp="$(mktemp "$env_file.XXXX")" grep -vE '^ZP_MCP_PUBLIC_URL=' "$env_file" > "$tmp" || true echo "ZP_MCP_PUBLIC_URL=$url/mcp" >> "$tmp" chmod 600 "$tmp"; mv "$tmp" "$env_file" "${compose[@]}" up -d --no-deps --force-recreate --wait claude-mcp >/dev/null fi for _ in $(seq 1 60); do public_ok "$url" && break sleep 5 done if public_ok "$url"; then echo "Link pre konektor v Claude (stály): $url/mcp" else echo "Adresa $url/mcp zatiaľ zvonku neodpovedá (prvý certifikát môže trvať pár minút)." >&2 hint="$(funnel_hint)"; [[ -n "$hint" ]] && echo "Povoľ Funnel/HTTPS v Tailscale: $hint" >&2 exit 1 fi ;; status) url="$(current_url)" echo "Funnel: ${url:-nebeží}${url:+/mcp}" echo "V .env: $(configured)" if [[ -n "$url" && "$(configured)" == "$url/mcp" ]] && public_ok "$url"; then echo "Stav: OK, link pre konektor: $url/mcp" else echo "Stav: NEFUNKČNÝ -> spusti: bash scripts/claude_tunnel.sh start" hint="$(funnel_hint)"; [[ -n "$hint" ]] && echo "Tailscale: $hint" exit 1 fi ;; *) echo "Použitie: bash scripts/claude_tunnel.sh {start|status}" >&2 exit 2 ;; esac