Cloudflare Analytics MCP Server

Cloudflare Analytics MCP Server

MCP (Model Context Protocol) server for accessing Cloudflare Analytics API, providing website traffic, security, and performance metrics.

Purpose

This MCP server provides standardized access to:

  • Cloudflare Analytics API ✅
    • Website traffic metrics (requests, bandwidth, cached requests)
    • Top pages/paths by traffic
    • Geographic visitor data
    • Security metrics (threats blocked, attacks)
    • Performance metrics (cache hit rate, response times)
    • Detailed HTTP request data

Benefits

  • ✅ Programmatic access to Cloudflare Analytics data
  • ✅ Reusable by AI assistants for on-demand analytics queries
  • ✅ Centralized API key management
  • ✅ Built-in rate limiting and error handling
  • ✅ Consistent data format
  • ✅ Can be used by existing Python scripts

Setup

1. Install Dependencies

cd mcp-servers/cloudflare-analytics
pip install -r requirements.txt

Note: The MCP Python SDK package name may vary. If pip install mcp doesn’t work, try:

  • pip install mcp-python
  • Or install from: https://github.com/modelcontextprotocol/python-sdk

2. Configure API Credentials

Create a .env file in the mcp-servers/cloudflare-analytics/ directory:

CLOUDFLARE_API_TOKEN=your_cloudflare_api_token_here
CLOUDFLARE_ZONE_ID=your_zone_id_here

Or set as environment variables:

export CLOUDFLARE_API_TOKEN=your_cloudflare_api_token_here
export CLOUDFLARE_ZONE_ID=your_zone_id_here

Get your credentials from:

  • API Token: https://dash.cloudflare.com/profile/api-tokens
    • Create API token with Zone:Read and Analytics:Read permissions
  • Zone ID: Cloudflare Dashboard → Your Domain → Overview → Zone ID

3. Test the Server

python server.py

The server should start and wait for MCP protocol messages via stdio.

4. Configure in Cursor

Add to your Cursor MCP settings (usually in ~/.cursor/mcp.json or Cursor settings):

{
  "mcpServers": {
    "cloudflare-analytics": {
      "command": "python3",
      "args": [
        "/Users/nickjackolaisen/Documents/GitHub/BI/mcp-servers/cloudflare-analytics/server.py"
      ],
      "env": {
        "CLOUDFLARE_API_TOKEN": "your_api_token_here",
        "CLOUDFLARE_ZONE_ID": "your_zone_id_here"
      }
    }
  }
}

Available Tools

Once configured, AI assistants can use these tools:

Cloudflare Analytics APIs:

  • fetch_cloudflare_traffic_metrics - Traffic overview (requests, bandwidth, cached requests) ✅
  • fetch_cloudflare_top_pages - Most visited pages/paths ✅
  • fetch_cloudflare_geographic_data - Visitor data by country/region ✅
  • fetch_cloudflare_security_metrics - Security events (threats blocked, attacks) ✅
  • fetch_cloudflare_performance_metrics - Performance data (cache hit rate, response times) ✅
  • fetch_cloudflare_http_requests - Detailed HTTP request data ✅

Usage Examples

From AI Assistant

“Get Cloudflare traffic metrics for the last 7 days” “Show me the top 10 pages on my website from Cloudflare Analytics” “What are my security metrics for the last 30 days?”

From Python Scripts

from cloudflare_client import CloudflareAPIClient

client = CloudflareAPIClient()

# Get traffic metrics for last 7 days
traffic = client.get_traffic_metrics(time_range="last_7d")

# Get top 10 pages
top_pages = client.get_top_pages(limit=10, time_range="last_30d")

# Get geographic data
geo_data = client.get_geographic_data(since="2024-01-01", until="2024-01-31")

Architecture

mcp-servers/cloudflare-analytics/
├── server.py              # Main MCP server
├── cloudflare_client.py   # Cloudflare Analytics API client with rate limiting
├── requirements.txt       # Python dependencies
├── .gitignore            # Git ignore file
└── README.md             # This file

Notes

  • Rate limiting: 0.25 seconds between requests (1200 requests per 5 minutes limit)
  • Uses GraphQL API: Requires API token with Zone:Read and Analytics:Read permissions
  • Error handling: All tools return structured error responses
  • Time ranges: Supports preset ranges (“last_24h”, “last_7d”, “last_30d”) or custom date ranges