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.
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
- Create or select a Google Cloud project.
- Enable Google Analytics Data API and Google Analytics Admin API.
- Configure the OAuth consent screen (External or Internal).
- Create OAuth 2.0 Client ID credentials (Web application).
- Authorized redirect URI:
- Local:
http://localhost:3000/api/auth/google/callback - Production:
https://<your-domain>/api/auth/google/callback
- Local:
- Request only GA4 scopes:
https://www.googleapis.com/auth/analytics.readonlyopenidemail
Do not add DV360 or Google Ads scopes to this connector.
GA4 setup
- Sign in to this app with email/password (application identity).
- Create a client (you become
ownerinclient_members). - Optionally add other application users as
ownerormember. - Click Connect Google Analytics. This attaches a Google credential to the client.
- 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.
Recommended Servers
playwright-mcp
A Model Context Protocol server that enables LLMs to interact with web pages through structured accessibility snapshots without requiring vision models or screenshots.
Magic Component Platform (MCP)
An AI-powered tool that generates modern UI components from natural language descriptions, integrating with popular IDEs to streamline UI development workflow.
Audiense Insights MCP Server
Enables interaction with Audiense Insights accounts via the Model Context Protocol, facilitating the extraction and analysis of marketing insights and audience data including demographics, behavior, and influencer engagement.
VeyraX MCP
Single MCP tool to connect all your favorite tools: Gmail, Calendar and 40 more.
graphlit-mcp-server
The Model Context Protocol (MCP) Server enables integration between MCP clients and the Graphlit service. Ingest anything from Slack to Gmail to podcast feeds, in addition to web crawling, into a Graphlit project - and then retrieve relevant contents from the MCP client.
Kagi MCP Server
An MCP server that integrates Kagi search capabilities with Claude AI, enabling Claude to perform real-time web searches when answering questions that require up-to-date information.
Neon Database
MCP server for interacting with Neon Management API and databases
E2B
Using MCP to run code via e2b.
Exa Search
A Model Context Protocol (MCP) server lets AI assistants like Claude use the Exa AI Search API for web searches. This setup allows AI models to get real-time web information in a safe and controlled way.
Qdrant Server
This repository is an example of how to create a MCP server for Qdrant, a vector search engine.