GA4 MCP Server

GA4 MCP Server

Enables querying Google Analytics 4 data through natural language, including running reports, comparing periods, and exploring realtime metrics across multi-tenant properties with OAuth-based authentication.

Category
访问服务器

README

GA4 MCP Server

Production-ready, multi-tenant Google Analytics 4 MCP server for Claude.

This is the first connector in a larger platform. Shared infrastructure (auth, clients, Google OAuth token storage, authorization, logging, errors) is reusable. GA4-specific code stays under src/connectors/ga4 and src/mcp/ga4. Do not treat this as a monolithic Google Marketing MCP.

Architecture

Claude
  |  MCP OAuth 2.1 + Streamable HTTP
  v
Next.js / Vercel  (/mcp)
  |-- MCP tools (ga4_*)
  |-- Authorization (client_members only)
  |-- GA4 service
  |-- Google OAuth token manager
  v
Supabase
  |-- clients
  |-- client_members   (application identity)
  |-- google_connections (Google credential, not app identity)
  |-- ga4_properties

Application users are Supabase Auth users. Tenant access is only client_members(user_id, client_id, role). A Google-connected account is a client-owned credential and is never the application user.

Local setup

npm install
cp .env.example .env.local
# fill in the values documented below
npx supabase start   # or link a remote project
npx supabase db reset --local
npm run dev

Open http://localhost:3000, create an application account, create a client, then connect Google Analytics.

Google Cloud setup

  1. Create or select a Google Cloud project.
  2. Enable Google Analytics Data API and Google Analytics Admin API.
  3. Configure the OAuth consent screen (External or Internal).
  4. Create OAuth 2.0 Client ID credentials (Web application).
  5. Authorized redirect URI:
    • Local: http://localhost:3000/api/auth/google/callback
    • Production: https://<your-domain>/api/auth/google/callback
  6. Request only GA4 scopes:
    • https://www.googleapis.com/auth/analytics.readonly
    • openid
    • email

Do not add DV360 or Google Ads scopes to this connector.

GA4 setup

  1. Sign in to this app with email/password (application identity).
  2. Create a client (you become owner in client_members).
  3. Optionally add other application users as owner or member.
  4. Click Connect Google Analytics. This attaches a Google credential to the client.
  5. Link one or more GA4 properties discovered from that Google account.

A member who did not click Connect can still query the client if they have a client_members row.

Supabase setup

This repo includes SQL migrations in supabase/migrations. Create them with the CLI; do not invent filenames.

Local:

npx supabase start
npx supabase db reset --local

Remote:

npx supabase link --project-ref <project-ref>
npx supabase db push

The service role key is server-only. Never expose SUPABASE_SERVICE_ROLE_KEY or Google refresh tokens to the browser.

Environment variables

See .env.example. Required:

Variable Purpose
NEXT_PUBLIC_SUPABASE_URL Supabase project URL
NEXT_PUBLIC_SUPABASE_ANON_KEY Browser/server user client
SUPABASE_SERVICE_ROLE_KEY Server-side admin (never NEXT_PUBLIC_)
GOOGLE_CLIENT_ID / GOOGLE_CLIENT_SECRET Google OAuth app
GOOGLE_REDIRECT_URI Must match Google Cloud
GOOGLE_OAUTH_SCOPES GA4-only scopes
MCP_BASE_URL Public origin, e.g. https://your-app.vercel.app
MCP_TOKEN_SIGNING_SECRET Signs MCP access tokens
TOKEN_ENCRYPTION_KEY 64 hex chars (32 bytes) for Google token encryption

Do not put a GA4 property ID in .env. Properties belong to clients in Supabase.

Generate a token encryption key:

node -e "console.log(require('crypto').randomBytes(32).toString('hex'))"

MCP setup (Claude)

Remote endpoint:

https://<your-domain>/mcp

This server implements MCP OAuth 2.1:

  • Protected resource metadata: /.well-known/oauth-protected-resource
  • Authorization server metadata: /.well-known/oauth-authorization-server
  • Authorize: /oauth/authorize (application login + consent)
  • Token: /oauth/token (authorization_code + refresh_token, PKCE S256)
  • Register: /oauth/register (DCR fallback)
  • CIMD is advertised (client_id_metadata_document_supported)

In Claude: Settings → Connectors → Add custom connector → paste https://<your-domain>/mcp. Claude will run OAuth. After you approve, tools are available.

Local inspector:

npx @modelcontextprotocol/inspector

Connect with Streamable HTTP to http://localhost:3000/mcp. You still need a valid MCP Bearer token from the OAuth flow.

Tools

Tool Purpose
ga4_run_report Primary report (dates, metrics, dimensions, limit, order, basic filter)
ga4_realtime Realtime active users / traffic breakdown
ga4_metadata Discover metrics and dimensions for a property
ga4_list_properties Properties visible to the client's Google connection
ga4_compare_periods Current vs previous period with abs/% change

Every tool requires client_id. Property-scoped tools also require a linked property_id. Authorization always checks client_members before Google is called.

Example: ga4_run_report

{
  "client_id": "11111111-1111-1111-1111-111111111111",
  "property_id": "123456789",
  "date_start": "2026-08-01",
  "date_end": "2026-08-15",
  "dimensions": ["country"],
  "metrics": ["activeUsers", "sessions", "totalRevenue"],
  "limit": 10
}

Example: ga4_compare_periods

{
  "client_id": "11111111-1111-1111-1111-111111111111",
  "property_id": "123456789",
  "current_start": "2026-08-01",
  "current_end": "2026-08-15",
  "previous_start": "2026-07-17",
  "previous_end": "2026-07-31",
  "metrics": ["activeUsers", "sessions", "totalRevenue"]
}

Claude can then answer questions such as:

  • How many users did we have yesterday?
  • Top 10 countries this month
  • Compare this month with last month
  • How many active users are on the site right now?

Testing

npm test

Tests use mocked Google and Supabase. They never use production OAuth tokens.

Vercel

Deploy the Next.js app and set the same environment variables. MCP_BASE_URL and GOOGLE_REDIRECT_URI must use the production domain. The /mcp route runs on the Node.js runtime with a 60s max duration.

Project layout

src/
  auth/              application authentication
  authorization/     authorizeClient via client_members
  oauth/             Google OAuth + token manager
  mcp-oauth/         MCP OAuth 2.1 authorization server
  connectors/ga4/    Google Analytics APIs
  mcp/ga4/           MCP tools
  repositories/      Supabase data access
  app/mcp/           Streamable HTTP transport

Future connectors (DV360, Google Ads) should add src/connectors/<name> and src/mcp/<name> and reuse the shared core. Do not create a generic Google API dumping ground.

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选