From 277aff5d16a8690699f68facce2159fc84ff3626 Mon Sep 17 00:00:00 2001 From: jp170na Date: Thu, 19 Nov 2026 15:48:13 +0100 Subject: [PATCH] tailscale funnel Co-Authored-By: Claude Opus 5.5 --- README.md | 6 ++- deployment/docker-compose.yml | 23 ++++++-- deployment/tailscale/serve.json | 5 ++ scripts/claude_tunnel.sh | 93 +++++++++++++++++++-------------- test/api/test_configuration.py | 13 ++--- 5 files changed, 90 insertions(+), 50 deletions(-) create mode 100644 deployment/tailscale/serve.json diff --git a/README.md b/README.md index 862f347..8269179 100644 --- a/README.md +++ b/README.md @@ -110,7 +110,11 @@ s osobami a témami ZP Wiki (`PROBABLY_SAME`, `SAME_TOPIC`). - **Tiktoken** – meranie a delenie textových úsekov. - **MCP (FastMCP) a Claude Skills** – nástroje `/rag` a `/graphrag` v Claude. - **OAuth s PKCE, Cloudflare Tunnel** – zabezpečený verejný prístup k MCP serveru. -- **Docker Compose** – nasadenie API, Neo4j, MCP servera a tunela. +- **Docker Compose** – nasadenie API, Neo4j, MCP servera a verejnej adresy (Tailscale Funnel). +- **Tailscale Funnel** – stála verejná HTTPS adresa MCP servera pre konektor v Claude + (`https://zp-agent..ts.net/mcp`), rovnaká aj po reštarte. Prvé spustenie potrebuje + `TS_AUTHKEY` v `.env` (nie v gite); `bash scripts/claude_tunnel.sh start` adresu zistí, zapíše do `.env` + (`ZP_MCP_PUBLIC_URL`) a overí, `bash scripts/claude_tunnel.sh status` ukáže stav. - **Git a Gitea webhooky** – získavanie zmien zo ZP Wiki. - **Pytest** – testy funkcionality a regresie. diff --git a/deployment/docker-compose.yml b/deployment/docker-compose.yml index 14c6847..fec2be4 100644 --- a/deployment/docker-compose.yml +++ b/deployment/docker-compose.yml @@ -119,15 +119,27 @@ services: # The tunnel connects through Docker DNS, not a possibly different WSL # localhost service. OAuth on claude-mcp still protects every tool call. - claude-tunnel: - profiles: [claude-tunnel] - image: cloudflare/cloudflared@sha256:4955524aeaa379e154023d5a027c250f7b35041a2e7e950cf13f144bc9b9c5ba - command: [tunnel, --no-autoupdate, --protocol, http2, --url, "http://claude-mcp:8001"] + # Tailscale Funnel: one stable public HTTPS address (https://zp-agent..ts.net) for the Claude + # connector, kept across restarts (the login is kept in the tailscale-state volume). It forwards to the + # authenticated MCP server in this network; OAuth on claude-mcp still protects every tool call. + claude-funnel: + profiles: [claude] + image: tailscale/tailscale@sha256:72c1e19e0aedca96b8533bbb0b4368564410ff20b17e53b804eb6c1f04a31be4 # 1.104.7 + hostname: zp-agent + environment: + TS_AUTHKEY: ${TS_AUTHKEY:-} + TS_AUTH_ONCE: "true" + TS_HOSTNAME: zp-agent + TS_STATE_DIR: /var/lib/tailscale + TS_USERSPACE: "true" + TS_SERVE_CONFIG: /config/serve.json + volumes: + - tailscale-state:/var/lib/tailscale + - ./tailscale/serve.json:/config/serve.json:ro depends_on: claude-mcp: condition: service_healthy restart: unless-stopped - read_only: true cap_drop: [ALL] security_opt: [no-new-privileges:true] @@ -161,6 +173,7 @@ services: volumes: claude-oauth: + tailscale-state: hf-cache: neo4j-data: neo4j-logs: diff --git a/deployment/tailscale/serve.json b/deployment/tailscale/serve.json new file mode 100644 index 0000000..0297971 --- /dev/null +++ b/deployment/tailscale/serve.json @@ -0,0 +1,5 @@ +{ + "TCP": {"443": {"HTTPS": true}}, + "Web": {"${TS_CERT_DOMAIN}:443": {"Handlers": {"/": {"Proxy": "http://claude-mcp:8001"}}}}, + "AllowFunnel": {"${TS_CERT_DOMAIN}:443": true} +} diff --git a/scripts/claude_tunnel.sh b/scripts/claude_tunnel.sh index 85ffb25..0c18c30 100755 --- a/scripts/claude_tunnel.sh +++ b/scripts/claude_tunnel.sh @@ -1,66 +1,83 @@ #!/usr/bin/env bash -# Status and one-command recovery of the temporary Cloudflare quick tunnel for the -# Claude/ChatGPT MCP connector. A quick tunnel gets a NEW random address after a -# restart; "refresh" writes it to .env (ZP_MCP_PUBLIC_URL, used by OAuth) and -# recreates the MCP service. A stable address needs a named tunnel, Tailscale -# Funnel or ngrok with a static domain instead of this quick tunnel. +# 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 --profile claude-tunnel) +compose=(bash "$project_root/scripts/compose.sh" --profile claude) -tunnel_id() { "${compose[@]}" ps -q claude-tunnel; } +funnel_id() { "${compose[@]}" ps -q claude-funnel; } mcp_id() { "${compose[@]}" ps -q claude-mcp; } current_url() { - local id; id="$(tunnel_id)" + local id; id="$(funnel_id)" [[ -n "$id" ]] || return 0 - docker logs "$id" 2>&1 | grep -oE 'https://[a-z0-9-]+\.trycloudflare\.com' | tail -1 + 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 - status) - url="$(current_url)" - configured="$(grep -E '^ZP_MCP_PUBLIC_URL=' "$env_file" | cut -d= -f2- || true)" - echo "Tunel: ${url:-nebeží}" - echo "V .env: ${configured:-chýba}" - if public_ok "$url" && [[ "$configured" == "$url/mcp" ]]; then - echo "Stav: OK, link pre konektor: $url/mcp" - else - echo "Stav: NEFUNKČNÝ alebo nezhoduje sa s .env -> spusti: bash scripts/claude_tunnel.sh refresh" + 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 - ;; - refresh) - old="$(current_url)" - "${compose[@]}" up -d --no-deps claude-tunnel >/dev/null - docker restart "$(tunnel_id)" >/dev/null + "${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" && "$url" != "$old" ]] && break + [[ -n "$url" ]] && break sleep 2 done - [[ -n "$url" && "$url" != "$old" ]] || { echo "Nový tunel sa nepodarilo získať." >&2; exit 1; } - 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 claude-mcp >/dev/null + [[ -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 - [[ "$(docker inspect -f '{{.State.Health.Status}}' "$(mcp_id)" 2>/dev/null)" == healthy ]] && break - sleep 2 + public_ok "$url" && break + sleep 5 done - for _ in $(seq 1 15); do public_ok "$url" && break; sleep 2; done - public_ok "$url" || { echo "Tunel beží, ale verejná adresa ešte neodpovedá: $url" >&2; exit 1; } - echo "Nový link pre konektor v Claude/ChatGPT: $url/mcp" - echo "V Claude odstráň starý konektor, pridaj tento link a prihlás sa (ZP_MCP_LOGIN_PASSWORD z .env)." + 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 {status|refresh}" >&2 - exit 64 + echo "Použitie: bash scripts/claude_tunnel.sh {start|status}" >&2 + exit 2 ;; esac diff --git a/test/api/test_configuration.py b/test/api/test_configuration.py index 5780b3f..06ee50e 100644 --- a/test/api/test_configuration.py +++ b/test/api/test_configuration.py @@ -55,13 +55,14 @@ def test_secrets_are_ignored_by_git_and_docker() -> None: assert re.search(r"(?m)^\.env$", dockerignore) -def test_optional_tunnel_targets_the_authenticated_mcp_in_the_same_network(): +def test_the_funnel_targets_the_authenticated_mcp_in_the_same_network(): compose = read_project_file("deployment/docker-compose.yml") - service = compose.split(" claude-tunnel:", 1)[1].split(" neo4j:", 1)[0] - assert "profiles: [claude-tunnel]" in service - assert "http://claude-mcp:8001" in service and "localhost" not in service - assert "cloudflare/cloudflared@sha256:" in service and "ports:" not in service - assert "read_only: true" in service and "service_healthy" in service + service = compose.split(" claude-funnel:", 1)[1].split("\n neo4j:", 1)[0] + assert "profiles: [claude]" in service and "tailscale/tailscale@sha256:" in service + assert "TS_AUTHKEY: ${TS_AUTHKEY:-}" in service and "ports:" not in service and "service_healthy" in service + assert "cloudflared" not in compose # one public address only + serve = read_project_file("deployment/tailscale/serve.json") + assert '"Proxy": "http://claude-mcp:8001"' in serve and '"AllowFunnel"' in serve and "localhost" not in serve def test_runtime_requirements_contain_only_direct_dependencies() -> None: