Cloudflare Workers MCP server

Cloudflare Workers-based MCP implementation enables AI assistants to execute custom serverless logic at the edge.
Back to servers
Provider
Glen Maddern
Release date
Dec 13, 2024
Language
TypeScript
Stats
20 stars

The Workers MCP server is a high-performance Model Context Protocol implementation built on Cloudflare Workers. It enables you to serve AI models via MCP, a protocol designed for efficient interaction between clients and model backends with context-aware streaming.

Installation

To get started with the Workers MCP server, follow these steps:

  1. Clone the repository:

    git clone https://github.com/cloudflare/workers-mcp.git
    cd workers-mcp
    
  2. Install dependencies:

    npm install
    
  3. Set up your development environment:

    npm run dev
    

Configuration

Setting up Models

Configure your MCP server by editing the src/models.ts file. This file contains the definitions for all models your server will expose.

Here's an example configuration:

export const models: Model[] = [
  {
    id: "my-model-v1",
    provider: "openai",
    name: "My Custom Model",
    version: "1.0",
    contextWindow: 8192,
    capabilities: ["chat", "embeddings"],
    parameters: {
      apiKey: "YOUR_OPENAI_API_KEY",
      model: "gpt-4-turbo-preview"
    }
  }
]

Environment Variables

Create a .dev.vars file in the project root for local development:

OPENAI_API_KEY=your_openai_api_key
ANTHROPIC_API_KEY=your_anthropic_api_key

Usage

Interacting with the Server

Once your server is running, you can interact with it using any MCP-compatible client. The server exposes several endpoints:

  • /api/providers: Lists all available model providers
  • /api/models: Lists all configured models
  • /api/chat: Endpoint for chat completions
  • /api/embeddings: Endpoint for generating embeddings

Chat Completion Example

Here's how to send a chat completion request:

curl -X POST http://localhost:8787/api/chat \
  -H "Content-Type: application/json" \
  -d '{
    "model": "my-model-v1",
    "messages": [
      {"role": "system", "content": "You are a helpful assistant."},
      {"role": "user", "content": "Hello, how are you?"}
    ]
  }'

Embeddings Example

Generate embeddings with:

curl -X POST http://localhost:8787/api/embeddings \
  -H "Content-Type: application/json" \
  -d '{
    "model": "my-model-v1",
    "input": "The quick brown fox jumps over the lazy dog"
  }'

Deployment

To deploy your MCP server to Cloudflare Workers:

  1. Configure your wrangler.toml file with the appropriate settings:

    name = "mcp-server"
    main = "src/index.ts"
    compatibility_date = "2023-10-30"
    
  2. Set up your environment variables in the Cloudflare dashboard or via Wrangler:

    wrangler secret put OPENAI_API_KEY
    
  3. Deploy to Cloudflare Workers:

    npm run deploy
    

After deployment, your MCP server will be available at your Cloudflare Workers URL.

How to add this MCP server to Cursor

There are two ways to add an MCP server to Cursor. The most common way is to add the server globally in the ~/.cursor/mcp.json file so that it is available in all of your projects.

If you only need the server in a single project, you can add it to the project instead by creating or adding it to the .cursor/mcp.json file.

Adding an MCP server to Cursor globally

To add a global MCP server go to Cursor Settings > MCP and click "Add new global MCP server".

When you click that button the ~/.cursor/mcp.json file will be opened and you can add your server like this:

{
    "mcpServers": {
        "cursor-rules-mcp": {
            "command": "npx",
            "args": [
                "-y",
                "cursor-rules-mcp"
            ]
        }
    }
}

Adding an MCP server to a project

To add an MCP server to a project you can create a new .cursor/mcp.json file or add it to the existing one. This will look exactly the same as the global MCP server example above.

How to use the MCP server

Once the server is installed, you might need to head back to Settings > MCP and click the refresh button.

The Cursor agent will then be able to see the available tools the added MCP server has available and will call them when it needs to.

You can also explictly ask the agent to use the tool by mentioning the tool name and describing what the function does.

Want to 10x your AI skills?

Get a free account and learn to code + market your apps using AI (with or without vibes!).

Nah, maybe later