MCP Server with Cloudflare Workers
MCP Server with Cloudflare Workers
Introduction
Model Context Protocol (MCP) is an open standard that enables AI agents and assistants to interact with services. By setting up an MCP server, you can allow AI assistants to access your APIs directly.
Cloudflare Workers, combined with the workers-mcp
package, provide a powerful and scalable solution for building MCP servers.
Prerequisites
Before starting, ensure you have:
- A Cloudflare account
- Node.js installed
- Wrangler CLI installed (
npm install -g wrangler
)
Getting Started
Step 1: Create a New Cloudflare Worker
First, initialize a new Cloudflare Worker project:
Then, authenticate your Cloudflare account:
Step 2: Configure Wrangler
Update your wrangler.toml
file with the correct account details:
Installing MCP Tooling
To enable MCP support, install the workers-mcp
package:
Run the setup command to configure MCP:
This will:
- Add necessary dependencies
- Set up a local proxy for testing
- Configure the Worker for MCP compliance
Writing MCP Server Code
Update your src/index.ts
to define your MCP server:
Key Components:
- WorkerEntrypoint: Manages incoming requests and method exposure.
- ProxyToSelf: Ensures MCP protocol compliance.
- sayHello method: An example MCP function that AI assistants can call.
Adding API Calls
You can extend your MCP server by integrating with external APIs. Here's an example of fetching weather data:
Deploying the MCP Server
Once your Worker is set up, deploy it to Cloudflare:
After deployment, your Worker is live and AI assistants can discover and use your MCP tools.
To update your MCP server, redeploy with:
Testing the MCP Server
To test your MCP setup locally:
This command starts a local proxy allowing MCP clients (like Claude Desktop) to connect.
Security
To secure your MCP server, use Wrangler Secrets:
This adds a shared-secret authentication mechanism to prevent unauthorized access.
Conclusion
Congratulations! You have successfully built and deployed an MCP server using Cloudflare Workers. You can now extend it with more features and expose new tools for AI assistants.
For more details, check the Cloudflare MCP documentation.
This server cannot be installed
An open standard server implementation that enables AI assistants to directly access APIs and services through Model Context Protocol, built using Cloudflare Workers for scalability.