:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
chore(infra): support internal Grafana in MCP helpers (#117622)
Sven Lange715926b31385
diff --git a/tools/infra-scripts/mcp/README.md b/tools/infra-scripts/mcp/README.mdindex c4f578ff0a39..6a22cd65e60d 100644--- a/tools/infra-scripts/mcp/README.md+++ b/tools/infra-scripts/mcp/README.md@@ -5,7 +5,7 @@ Scripts for connecting AI assistants (Claude Code, Cursor, etc.) to PostHog's in## Why these scripts existPostHog's Grafana instances are reachable directly over Tailscale at plain HTTP hostnames-(`grafana-prod-us`, `grafana-prod-eu`, `grafana-dev`).+(`grafana-prod-us`, `grafana-prod-eu`, `grafana-dev`, `grafana-internal`).Authentication is handled at the Grafana layer via a service account Bearer token,which the Grafana MCP server supports natively.@@ -15,9 +15,39 @@ These scripts glue that together:2. Pull the service account token from macOS Keychain3. Hand both to `mcp-grafana`+### Connecting without the wrapper++The wrapper is optional. `mcp-grafana` accepts the Grafana URL and service account token directly.+Tailscale provides network access; Grafana still requires authentication.+You still need an MCP server process to translate tool calls into Grafana API requests.++To connect to internal without the wrapper, configure your MCP client:++```json+{+ "mcpServers": {+ "grafana-internal": {+ "command": "/Users/YOUR_USERNAME/go/bin/mcp-grafana",+ "args": ["-disable-write"],+ "env": {+ "GRAFANA_URL": "http://grafana-internal"+ }+ }+ }+}+```++Adjust the binary path to match your installation.+Supply `GRAFANA_SERVICE_ACCOUNT_TOKEN` through your client's secret configuration or inherited environment.+Do not commit tokens to MCP configuration files.+Use a Viewer service account for read-only access.++Keep the wrapper if you want per-environment tokens in macOS Keychain or the `grafana-region` switcher.+The wrapper does not add a proxy, tunnel, or authentication bypass.+## Prerequisites-- **Tailscale** connected, with access to the PostHog tailnet (hostnames `grafana-prod-us`, `grafana-prod-eu`, `grafana-dev` must resolve)+- **Tailscale** connected, with access to the selected Grafana host (`grafana-prod-us`, `grafana-prod-eu`, `grafana-dev`, or `grafana-internal`)- **mcp-grafana** binary installed- **macOS** (for Keychain-based token storage) or Linux with environment variables@@ -61,6 +91,7 @@ source ~/.zshrc # or ~/.bashrcgrafana-token us <your-us-token>grafana-token eu <your-eu-token>grafana-token dev <your-dev-token> # optional+grafana-token internal <your-internal-token> # optional```### 4. Configure your MCP client@@ -92,11 +123,12 @@ The wrapper script determines which Grafana region to connect to using (in orderRegion → host mapping:-| Region | Tailscale host |-| ------ | ------------------------ |-| `us` | `http://grafana-prod-us` |-| `eu` | `http://grafana-prod-eu` |-| `dev` | `http://grafana-dev` |+| Region | Tailscale host |+| ---------- | ------------------------- |+| `us` | `http://grafana-prod-us` |+| `eu` | `http://grafana-prod-eu` |+| `dev` | `http://grafana-dev` |+| `internal` | `http://grafana-internal` |#### Single-server setup (switching regions manually)@@ -107,6 +139,7 @@ grafana-region # Show current regiongrafana-region us # Switch to prod-us (us-east-1)grafana-region eu # Switch to prod-eu (eu-central-1)grafana-region dev # Switch to dev environment+grafana-region internal # Switch to internal environment```**Important: Restart required after switching regions**@@ -145,6 +178,19 @@ Add to `~/.config/claude-code/.mcp.json`:}```+To add internal alongside those servers, add a `grafana-internal` entry to `mcpServers`:++```json+{+ "command": "/Users/YOUR_USERNAME/dev/posthog/posthog/tools/infra-scripts/mcp/mcp-grafana-wrapper.sh",+ "args": ["-disable-write"],+ "env": { "GRAFANA_REGION": "internal" }+}+```++Store its token with `grafana-token internal <your-internal-token>`, then restart your MCP client.+Supported regions are explicit so each selection maps to a known host and its own Keychain token.+#### Migrating from single-server to multi-serverIf you already have a single `grafana` server configured with both US and EU tokens in Keychain, run the migration script:@@ -187,7 +233,7 @@ You may want to use a secrets manager or encrypted file for production use.### Hostname doesn't resolve / connection refused-The Tailscale hostnames (`grafana-prod-us`, `grafana-prod-eu`, `grafana-dev`) only resolve when Tailscale is connected to the PostHog tailnet. Check with:+The Tailscale hostnames (`grafana-prod-us`, `grafana-prod-eu`, `grafana-dev`, `grafana-internal`) only resolve when Tailscale is connected to the PostHog tailnet. Check with:```bashtailscale statusdiff --git a/tools/infra-scripts/mcp/grafana-region b/tools/infra-scripts/mcp/grafana-regionindex 3590c0623851..4c6bf1ce6e83 100755--- a/tools/infra-scripts/mcp/grafana-region+++ b/tools/infra-scripts/mcp/grafana-region@@ -1,11 +1,12 @@#!/usr/bin/env bash-# Switch Grafana MCP server between prod-us, prod-eu, and dev regions+# Switch Grafana MCP server between prod-us, prod-eu, dev, and internal## Usage:# grafana-region # Show current region# grafana-region us # Switch to prod-us# grafana-region eu # Switch to prod-eu# grafana-region dev # Switch to dev+# grafana-region internal # Switch to internal## After switching, restart your MCP client (e.g., Claude Code: /exit)@@ -21,13 +22,14 @@ show_current() {}show_usage() {- echo "Usage: grafana-region [us|eu|dev]"+ echo "Usage: grafana-region [us|eu|dev|internal]"echo ""echo "Commands:"echo " (no args) Show current region"echo " us Switch to prod-us (us-east-1)"echo " eu Switch to prod-eu (eu-central-1)"echo " dev Switch to dev (us-east-1)"+ echo " internal Switch to internal"echo ""echo "After switching, restart your MCP client to apply the change."}@@ -36,7 +38,7 @@ case "${1:-}" in"")show_current;;- us|eu|dev)+ us|eu|dev|internal)echo "$1" > "$REGION_FILE"echo "Switched Grafana region to: $1"echo ""@@ -46,7 +48,7 @@ case "${1:-}" inshow_usage;;*)- echo "Error: Invalid region '$1'. Must be 'us', 'eu', or 'dev'." >&2+ echo "Error: Invalid region '$1'. Must be 'us', 'eu', 'dev', or 'internal'." >&2echo ""show_usageexit 1diff --git a/tools/infra-scripts/mcp/grafana-token b/tools/infra-scripts/mcp/grafana-tokenindex 2ca681ecd094..8d011c2822a9 100755--- a/tools/infra-scripts/mcp/grafana-token+++ b/tools/infra-scripts/mcp/grafana-token@@ -6,9 +6,11 @@# grafana-token us # Show if US token exists# grafana-token eu # Show if EU token exists# grafana-token dev # Show if dev token exists+# grafana-token internal # Show if internal token exists# grafana-token us <token> # Set prod-us token# grafana-token eu <token> # Set prod-eu token# grafana-token dev <token> # Set dev token+# grafana-token internal <token> # Set internal token## Note: This script requires macOS (uses Keychain).# On Linux, set the GRAFANA_SERVICE_ACCOUNT_TOKEN environment variable instead.@@ -43,19 +45,27 @@ show_status() {elseecho " dev: not configured"fi++ if security find-generic-password -a "$USER" -s "grafana-service-account-token-internal" &>/dev/null; then+ echo " internal: configured"+ else+ echo " internal: not configured"+ fi}show_usage() {- echo "Usage: grafana-token [us|eu|dev] [token]"+ echo "Usage: grafana-token [us|eu|dev|internal] [token]"echo ""echo "Commands:"echo " (no args) Show token status for all regions"echo " us Show if US token is configured"echo " eu Show if EU token is configured"echo " dev Show if dev token is configured"+ echo " internal Show if internal token is configured"echo " us <token> Set prod-us token"echo " eu <token> Set prod-eu token"echo " dev <token> Set dev token"+ echo " internal <token> Set internal token"echo ""echo "After setting a token, restart your MCP client to pick it up."echo ""@@ -69,7 +79,7 @@ case "${1:-}" in"")show_status;;- us|eu|dev)+ us|eu|dev|internal)REGION="$1"SERVICE="grafana-service-account-token-$REGION"@@ -109,7 +119,7 @@ case "${1:-}" inshow_usage;;*)- echo "Error: Invalid region '$1'. Must be 'us', 'eu', or 'dev'." >&2+ echo "Error: Invalid region '$1'. Must be 'us', 'eu', 'dev', or 'internal'." >&2echo ""show_usageexit 1diff --git a/tools/infra-scripts/mcp/mcp-grafana-wrapper.sh b/tools/infra-scripts/mcp/mcp-grafana-wrapper.shindex 2a8fee30580a..711d1e2ebcd4 100755--- a/tools/infra-scripts/mcp/mcp-grafana-wrapper.sh+++ b/tools/infra-scripts/mcp/mcp-grafana-wrapper.sh@@ -2,15 +2,15 @@# Wrapper script for mcp-grafana that connects to PostHog Grafana via Tailscale.## PostHog's Grafana instances are reachable over Tailscale at plain HTTP hostnames-# (grafana-prod-us, grafana-prod-eu, grafana-dev). This wrapper looks up a service+# (grafana-prod-us, grafana-prod-eu, grafana-dev, grafana-internal). This wrapper looks up a service# account token from macOS Keychain and hands both the URL and token to mcp-grafana.#-# Supports switching between prod-us, prod-eu, and dev regions via GRAFANA_REGION env var-# or ~/.grafana-region file (env var takes precedence). Use `grafana-region us`,-# `grafana-region eu`, or `grafana-region dev` to switch, then restart your MCP client.+# Supports switching between prod-us, prod-eu, dev, and internal via GRAFANA_REGION env var+# or ~/.grafana-region file (env var takes precedence). Use `grafana-region <region>`+# to switch, then restart your MCP client.## Prerequisites:-# - Tailscale connected (grafana-prod-us / grafana-prod-eu / grafana-dev must resolve)+# - Tailscale connected with access to the selected Grafana host# - mcp-grafana binary installed (go install github.com/grafana/mcp-grafana/cmd/mcp-grafana@latest)# - Grafana service account token stored in macOS Keychain (see grafana-token script)@@ -35,8 +35,8 @@ if [ -n "${GRAFANA_REGION:-}" ]; thenfi# Validate region-if [[ "$CURRENT_REGION" != "us" && "$CURRENT_REGION" != "eu" && "$CURRENT_REGION" != "dev" ]]; then- echo "Error: Invalid region '$CURRENT_REGION' from $REGION_SOURCE. Must be 'us', 'eu', or 'dev'." >&2+if [[ "$CURRENT_REGION" != "us" && "$CURRENT_REGION" != "eu" && "$CURRENT_REGION" != "dev" && "$CURRENT_REGION" != "internal" ]]; then+ echo "Error: Invalid region '$CURRENT_REGION' from $REGION_SOURCE. Must be 'us', 'eu', 'dev', or 'internal'." >&2exit 1fi@@ -54,6 +54,10 @@ case "$CURRENT_REGION" inGRAFANA_HOST="grafana-dev"KEYCHAIN_SERVICE="grafana-service-account-token-dev";;+ internal)+ GRAFANA_HOST="grafana-internal"+ KEYCHAIN_SERVICE="grafana-service-account-token-internal"+ ;;esac# Find mcp-grafana binary