Cloud Services·
advanced
·12 min read·Apr 4, 2026

How to Use the Cloudflare MCP Server for Edge Infrastructure

Manage Cloudflare Workers, KV stores, R2 buckets, and DNS settings through your AI agent using the Cloudflare MCP server.

CloudflareWorkersedge computingDNSKV storeR2DevOps

Use the Cloudflare MCP Server for Edge Infrastructure

The Cloudflare MCP server acts as an administrative bridge between your AI agent and your edge infrastructure. It allows the agent to deploy serverless Workers, edit KV storage values, manage R2 blob storage, and configure DNS records across your Cloudflare zones.

Prerequisites

  • A Cloudflare account with active domains/zones.
  • A Cloudflare API Token with appropriate permissions.
  • Node.js 18+ installed locally.

Creating a Scoped API Token

Never give your agent a Global API Key. Create a scoped token:

  1. Go to Cloudflare Dashboard → My Profile → API Tokens.
  2. Click Create TokenCustom Token.
  3. Grant the following Edit permissions depending on your needs:
    • Workers Scripts
    • Workers KV Storage
    • Workers R2 Storage
    • Zone DNS
  4. Restrict the token to specific Account Resources or Zones if possible.
  5. Copy the generated token.

Configuration Setup

json
{
  "mcpServers": {
    "cloudflare": {
      "command": "npx",
      "args": ["-y", "@cloudflare/mcp-server-cloudflare"],
      "env": {
        "CLOUDFLARE_API_TOKEN": "your_api_token_here",
        "CLOUDFLARE_ACCOUNT_ID": "your_account_id_here"
      }
    }
  }
}

Available Tools

Your agent will be equipped with dozens of management tools, categorized below:

Workers

  • list_workers / get_worker / create_worker / delete_worker

KV Store

  • list_kv_namespaces / list_kv_keys / get_kv_value / put_kv_value

R2 Storage

  • list_r2_buckets / list_r2_objects / get_r2_object / put_r2_object

DNS

  • list_zones / list_dns_records / create_dns_record / update_dns_record

Example Workflows

  • Worker Deployment: "Create a new Cloudflare Worker that intercepts traffic to /old-api and returns a 301 redirect to /new-api. Deploy it to my account."
  • KV Data Management: "List all keys in the 'user-preferences' KV namespace and show me the JSON values for the 'admin-theme' key."
  • DNS Management: "Add a CNAME record pointing blog.mywebsite.com to mywebsite.netlify.app."

Best Practices & Troubleshooting

  • KV Propagation Delays: Cloudflare KV is eventually consistent. If your agent uses put_kv_value and immediately uses get_kv_value to verify it, it might receive stale data. Instruct the agent to account for up to 60 seconds of global propagation delay.
  • R2 Payload Limits: Reading a massive multi-gigabyte video file from R2 using get_r2_object will crash the MCP JSON-RPC connection. Instruct the agent to only fetch text-based objects (like JSON or CSVs) or use pre-signed URLs.
  • Unauthorized (Code 10000): Ensure your CLOUDFLARE_ACCOUNT_ID environment variable is correct. You can find your Account ID in the URL of the Cloudflare dashboard.

Using with OpenAI Codex

To use this MCP server with the OpenAI Codex CLI, you can add it to your configuration using the codex mcp add command:

bash
codex mcp add --name cloudflare --command "npx @modelcontextprotocol/server-cloudflare"

(Note: Depending on the server, you may need to append arguments or use --env flags for environment variables as described in the configuration section above)

Related Guides