This MCP server implementation allows AI assistants to interact with your services through the Model Context Protocol standard. Cloudflare Workers provide a scalable platform for hosting these MCP servers.
Before starting, ensure you have:
npm install -g wrangler
)First, initialize a new Cloudflare Worker project:
npx create-cloudflare@latest my-mcp-worker
cd my-mcp-worker
Then, authenticate your Cloudflare account:
wrangler login
Update your wrangler.toml
file with the correct account details:
name = "my-mcp-worker"
main = "src/index.ts"
compatibility_date = "2025-03-03"
account_id = "your-account-id"
Install the workers-mcp
package to enable MCP functionality:
npm install workers-mcp
Run the setup command to configure MCP:
npx workers-mcp setup
This will add necessary dependencies, set up a local proxy for testing, and configure the Worker for MCP compliance.
Update your src/index.ts
with the following code to define a basic MCP server:
import { WorkerEntrypoint } from 'cloudflare:workers';
import { ProxyToSelf } from 'workers-mcp';
export default class MyWorker extends WorkerEntrypoint<Env> {
/**
* A friendly greeting from your MCP server.
* @param name {string} The name of the user.
* @return {string} A personalized greeting.
*/
sayHello(name: string) {
return `Hello from an MCP Worker, ${name}!`;
}
/**
* @ignore
*/
async fetch(request: Request): Promise<Response> {
return new ProxyToSelf(this).fetch(request);
}
}
You can extend your MCP server by integrating with external APIs:
export default class WeatherWorker extends WorkerEntrypoint<Env> {
/**
* Fetch weather data for a given location.
* @param location {string} The city or ZIP code.
* @return {object} Weather details.
*/
async getWeather(location: string) {
const response = await fetch(`https://api.weather.example/v1/${location}`);
const data = await response.json();
return {
temperature: data.temp,
conditions: data.conditions,
forecast: data.forecast
};
}
async fetch(request: Request): Promise<Response> {
return new ProxyToSelf(this).fetch(request);
}
}
Deploy your MCP server to Cloudflare:
npx wrangler deploy
After deployment, your Worker is live and AI assistants can discover and use your MCP tools.
To test your MCP setup locally:
npx workers-mcp proxy
This starts a local proxy allowing MCP clients (like Claude Desktop) to connect to your server.
Secure your MCP server by adding a shared secret:
npx wrangler secret put MCP_SECRET
This adds authentication to prevent unauthorized access to your MCP server.
To add this MCP server to Claude Code, run this command in your terminal:
claude mcp add-json "my-mcp-worker" '{"command":"npx","args":["workers-mcp","proxy"]}'
See the official Claude Code MCP documentation for more details.
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.
To add a global MCP server go to Cursor Settings > Tools & Integrations and click "New MCP Server".
When you click that button the ~/.cursor/mcp.json
file will be opened and you can add your server like this:
{
"mcpServers": {
"my-mcp-worker": {
"command": "npx",
"args": [
"workers-mcp",
"proxy"
]
}
}
}
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.
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 explicitly ask the agent to use the tool by mentioning the tool name and describing what the function does.
To add this MCP server to Claude Desktop:
1. Find your configuration file:
~/Library/Application Support/Claude/claude_desktop_config.json
%APPDATA%\Claude\claude_desktop_config.json
~/.config/Claude/claude_desktop_config.json
2. Add this to your configuration file:
{
"mcpServers": {
"my-mcp-worker": {
"command": "npx",
"args": [
"workers-mcp",
"proxy"
]
}
}
}
3. Restart Claude Desktop for the changes to take effect