AI & Agents

How to Deploy an MCP Server on Cloudflare Workers

Deploying an MCP server on Cloudflare Workers lets you run AI tools at the edge without managing infrastructure.

Fast.io Editorial Team 8 min read
Run your MCP servers globally with Cloudflare Workers.

What is an MCP Server on Cloudflare Workers?

Running an MCP server on Cloudflare Workers puts your Model Context Protocol server at the edge. This gives AI agents fast tool access without managing servers. The Model Context Protocol (MCP) standardizes how AI models interact with tools and data. While it started with local desktop environments, the need for shared, remote tools has led to cloud-based deployments. Most MCP servers run locally using standard input/output (stdio) for tools like Claude Desktop. To serve remote agents or build shared tools, you need a network protocol. Cloudflare Workers fits this well because it supports Server-Sent Events (SSE), the standard for remote MCP connections, and scales to zero when idle. You only pay for the time your agent uses the tools, rather than paying for a virtual machine to sit idle.

Key Benefits:

  • Speed: Runs in 300+ cities globally, reducing latency between the model and the tool execution environment.
  • Cost Efficiency: No minimum monthly fee; you pay only for the compute cycles used during request processing.
  • Native Support: Cloudflare Workers support MCP servers via Server-Sent Events (SSE) with optimized routing.
  • Security: Use Cloudflare's DDoS protection and web application firewall (WAF) infrastructure.

Helpful references: Fast.io Workspaces, Fast.io Collaboration, and Fast.io AI.

Why Deploy MCP at the Edge?

Latency hurts AI agent performance. When an agent like Claude or GPT-4 uses a tool, it waits for that tool to execute and return data before it continues. If your MCP server is on a single machine in Virginia and your agent's compute is in Tokyo, every tool call adds hundreds of milliseconds of round-trip time. Cloudflare Workers run in over 300 cities globally. Your MCP server executes close to the LLM's inference point. Beyond speed, edge deployment improves reliability. Since Workers are distributed across a global network, you avoid single points of failure. If one data center has an issue, traffic routes to the next nearest location. For developers building multi-agent systems, the edge works as a centralized "tool hub." Instead of each agent having its own local set of tools, they can all connect to a single edge-hosted MCP server. This allows for shared state, unified logging, and centralized security policies.

Prerequisites

Before you start, make sure you have these installed:

  • Node.js & npm: You need a recent version of Node.js (latest LTS) to run the development tools.
  • Wrangler CLI: This is Cloudflare's command-line tool for managing Workers. Install it globally using npm install -g wrangler.
  • Cloudflare Account: You can start with a free account, which includes a generous daily request limit.
  • Fast.io Account: Needed for persistent file storage. Since Workers are stateless, you need a place to store agent artifacts, documents, and logs that survives between execution cycles. Familiarity with TypeScript helps. The MCP SDK uses TypeScript, which makes it easier to define the strict JSON-RPC schemas required for tool definitions.

Initialize the Worker Project

Create a new Cloudflare Worker project with Wrangler using a minimal template. ```bash

Create a new project named 'my-mcp-server'

npm create cloudflare@latest my-mcp-server -- --type=hello-world --ts

Navigate into the directory

cd my-mcp-server

Install the official MCP SDK

npm install @modelcontextprotocol/sdk


This sets up a basic `src/index.ts` file and a `wrangler.toml` configuration file. Consider how this fits into your broader workflow and what matters most for your team. The right choice depends on your specific requirements: file types, team size, security needs, and how you collaborate with external partners. Testing with a free account is the fast way to know if a tool works for you.

Implement the MCP Server with SSE

Remote MCP servers usually use Server-Sent Events (SSE) for server-to-client updates and HTTP POST for client messages. Cloudflare Workers handles this with the standard Response object and the ReadableStream API. The MCP SDK provides an SSEServerTransport class for this. When an agent connects, it opens a long-lived GET request to your /sse endpoint. Your server sends events down this stream. To send messages back to the server, the agent makes separate POST requests to a /message endpoint. Here is a detailed implementation for src/index.ts:

import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { SSEServerTransport } from "@modelcontextprotocol/sdk/server/sse.js";
import { z } from "zod";

// Initialize the server with metadata
const server = new McpServer({
  name: "cloudflare-worker-mcp",
  version: "1.0.0",
});

// Define a tool that the AI agent can call
server.tool(
  "get-worker-info",
  { city: z.string().optional() },
  async ({ city }) => {
    const location = city || "the edge";
    return { 
      content: [{ 
        type: "text", 
        text: `This MCP server is running on Cloudflare Workers at ${location}.` 
      }] 
    };
  }
);

export default {
  async fetch(request: Request): Promise<Response> {
    const url = new URL(request.url);

// 1. Handle the SSE connection initiation
    if (url.pathname === "/sse") {
      // The transport needs a URL for the client to send POST messages back to
      const transport = new SSEServerTransport("/message", response);
      
      // Connect the server logic to this specific transport instance
      await server.connect(transport);
      
      // Return the stream as the response
      return transport.response;
    }

// 2. Handle incoming JSON-RPC messages from the agent
    if (url.pathname === "/message" && request.method === "POST") {
      const message = await request.json();
      await server.handleMessage(message);
      return new Response("Accepted", { status: 202 });
    }

return new Response("Not Found", { status: 404 });
  },
};

The SSEServerTransport formats messages into the correct SSE event format. This keeps your server compliant with the Model Context Protocol without manual stream header or event type management.

Connect Persistent Storage via Fast.io

Cloudflare Workers are stateless. The file system provided to a running worker is temporary. Any file you create or data you store in memory disappears when the worker stops, which usually happens seconds after the last request finishes. For an AI agent, this memory loss is a problem. If an agent generates a report, processes a large dataset, or needs to maintain a knowledge base, that data must live somewhere persistent. Fast.io works well as the storage layer for edge-based MCP servers.

Why Fast.io for MCP Workers:

  • Persistence: Files survive worker restarts and deployments.
  • Agent Access: Fast.io lets you create public or private shares, so your agent can generate a file and give the user a direct link.
  • Zero Configuration: Unlike S3, Fast.io doesn't require complex IAM roles or bucket policies. It works over standard HTTP. Here is how to implement a "save-memory" tool that uses Fast.io to persist data:
server.tool(
  "save-agent-memory",
  { key: z.string(), content: z.string() },
  async ({ key, content }, { env }) => {
    const filename = `${key}.txt`;
    
    // Use fetch to talk directly to the Fast.io API
    const response = await fetch(`https://api.fast.io/v1/workspaces/my-agent-data/files/${filename}`, {
      method: "PUT",
      headers: {
        "Authorization": `Bearer ${env.FASTIO_TOKEN}`,
        "Content-Type": "text/plain",
      },
      body: content,
    });
    
    if (!response.ok) {
      return { content: [{ type: "text", text: "Failed to save memory." }] };
    }
    
    return { content: [{ type: "text", text: `Memory saved as ${filename}.` }] };
  }
);

Offloading storage to Fast.io keeps your worker lightweight while giving your agent reliable memory that spans sessions.

Managing Session State and Durable Objects

While Fast.io handles file storage, you may sometimes need to manage "hot" session state. For example, if you are building an MCP server that manages a multi-turn conversation or needs to maintain a lock on a resource, consider Cloudflare Durable Objects. Durable Objects let you maintain state for a specific ID across multiple requests. Combined with Fast.io, Durable Objects handle the real-time coordination and active state, while Fast.io handles large file storage and archival. For most MCP implementations, stateless Workers combined with Fast.io storage work fine. Use Durable Objects only if your tools require strict consistency across multiple concurrent agent interactions.

Fast.io features

Give Your AI Agents Persistent Storage

Stop losing data when your worker spins down. Get 50GB of free, high-speed storage optimized for AI agents.

Deploy and Test

When your code is ready, deploy with one command. ```bash npx wrangler deploy


Wrangler will output your worker's URL (e.g., `https://my-mcp-server.user.workers.dev`).

**To test your deployed server:**
1. Open the **Claude Desktop** configuration file (`claude_desktop_config.json`). 2. Add your new server under `mcpServers`. Note that you cannot point Claude Desktop directly to a remote URL natively without a local bridge, or by using an agent that supports remote MCP directly (like OpenClaw or Fast.io's hosted agents). For a pure cloud setup, you can use the **MCP Inspector** or connect it to an agent orchestration platform that supports remote SSE connections. Consider how this fits into your broader workflow and what matters most for your team. The right choice depends on your specific requirements: file types, team size, security needs, and how you collaborate with external partners. Testing with a free account is the fast way to know if a tool works for you.

Securing Your Edge MCP Server

Putting an MCP server on the public internet requires security. Unlike local servers, anyone with the URL can access your Worker.

Recommended Security Practices:

  • Secret Token Auth: Require a specific header (e.g., X-MCP-Access-Token) in your fetch handler before processing requests.
  • Cloudflare Access: Use Cloudflare Zero Trust to put your worker behind an authentication screen (Google Auth, GitHub Auth).
  • Validation: Validate all tool inputs using Zod schemas to prevent injection attacks. Consider how this fits into your broader workflow and what matters most for your team. The right choice depends on your specific requirements: file types, team size, security needs, and how you collaborate with external partners. Testing with a free account is the fast way to know if a tool works for you.

Frequently Asked Questions

How do I deploy an MCP server on Cloudflare Workers?

You deploy an MCP server on Cloudflare Workers by creating a project with Wrangler, installing the `@modelcontextprotocol/sdk`, implementing an SSEServerTransport in your fetch handler, and running `wrangler deploy`. The server communicates via Server-Sent Events (SSE) over HTTP.

Can Cloudflare Workers host MCP servers?

Yes. Since Workers support the long-lived HTTP connections needed for SSE, they work well for running lightweight, globally distributed MCP tools.

How do I add file storage to a Cloudflare Workers MCP server?

Because Cloudflare Workers are stateless, use an external HTTP-based storage API like Fast.io. You can use standard `fetch` requests in your worker to read and write files to your Fast.io workspace, so data stays safe.

What are the limitations of MCP on Cloudflare Workers?

The main limitations are execution time limits (CPU time) and the lack of a persistent local file system. Complex, long-running processing tasks might time out on the standard Workers plan, and file operations must be offloaded to external storage services.

Related Resources

Fast.io features

Give Your AI Agents Persistent Storage

Stop losing data when your worker spins down. Get 50GB of free, high-speed storage optimized for AI agents.