Skip to content

Commit 7acd80d

Browse files
committed
Update MCP server documentation based on PR feedback
1 parent 1195e85 commit 7acd80d

File tree

1 file changed

+52
-17
lines changed

1 file changed

+52
-17
lines changed

src/content/docs/agents/examples/build-mcp-server.mdx

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,69 @@ sidebar:
55
order: 100
66
group:
77
hideIndex: true
8-
description: Build and deploy an MCP server on Cloudflare Workers
8+
description: Learn how to create, deploy, and configure an MCP server on Cloudflare Workers to enable AI assistants to interact with your services directly.
99
---
1010

1111
import { MetaInfo, Render, Type, TypeScriptExample, WranglerConfig } from "~/components";
1212
import { Aside } from '@astrojs/starlight/components';
1313

14-
[Model Context Protocol (MCP)](https://modelcontextprotocol.io/introduction) is an open standard that allows AI assistants & LLMs to interact with services directly. If you want users to access your service or product straight from their AI assistant, you can enable this by spinning up an MCP server for your application.
14+
In this guide, you will learn how to build and deploy a Model Context Protocol (MCP) server on Cloudflare Workers.
1515

16-
## Building an MCP Server on Cloudflare Workers
16+
[Model Context Protocol (MCP)](https://modelcontextprotocol.io/introduction) is an open standard that allows AI agents and assistants (like [Claude Desktop](https://claude.ai/download), [Cursor](https://www.cursor.com/), or [Anthropic's Claude](https://www.anthropic.com/claude)) to interact with services directly. If you want users to access your service through an AI agent, you can spin up an MCP server for your application.
1717

18-
Normally, setting up an MCP server requires writing boilerplate code to handle routing, define types, and standing up a server that implements the MCP protocol. But with [Cloudflare Workers](/workers/), all the heavy lifting is done for you, so all you need to do is define your service's functionality as TypeScript methods on your Worker
19-
Once deployed, your Worker becomes an MCP server that AI assistants (as long as they support MCP) can connect to and use to interact with your service.
18+
### Why use Cloudflare Workers for MCP?
2019

21-
<Aside type="caution">
22-
Remote MCP servers are not supported yet. The workers-mcp tooling creates a local proxy that forwards requests to your Worker, allowing the server to be used by an MCP client.
23-
</Aside>
20+
With Cloudflare Workers and the [workers-mcp](https://github.com/cloudflare/workers-mcp/) package, you can turn any API or service into an MCP server with minimal setup. Just define your API methods as TypeScript functions, and workers-mcp takes care of tool discovery, protocol handling, and request routing. Once deployed, MCP clients like Claude can connect and interact with your service automatically.
21+
22+
#### Example: Exposing a Weather API as an MCP server
2423

25-
## Benefits
24+
Here's a Cloudflare Worker that fetches weather data from an external API and exposes it as an MCP tool that Claude can call directly:
25+
26+
```ts
27+
import { WorkerEntrypoint } from 'cloudflare:workers';
28+
import { ProxyToSelf } from 'workers-mcp';
2629

27-
- **Minimal setup & built-in boilerplate:** The Worker automatically handles API routing, server management, and MCP protocol compliance. The [workers-mcp](https://www.npmjs.com/package/workers-mcp) package bootstraps your MCP server, allowing you to focus on your service logic.
28-
- **Automatic documentation:** Public methods annotated with JSDoc are automatically documented and exposed as MCP tools. This means AI assistants can quickly understand how to interact with your service, making tool discovery and integration much easier.
29-
- **Scalability & performance:** Deploy your MCP server on Cloudflare's global edge network so that users can make fast, performant requests from their LLMs. Cloudflare will handle traffic spikes and high load, ensuring your service remains available and responsive.
30-
- **Expand MCP server capabilities:** Easily connect to Workers AI, D1, Durable Objects, and other Cloudflare services to add more functionality to your MCP Server.
30+
export default class WeatherWorker extends WorkerEntrypoint<Env> {
31+
/**
32+
* Get current weather for a location
33+
* @param location {string} City name or zip code
34+
* @return {object} Weather information
35+
*/
36+
async getWeather(location: string) {
37+
// Connect to a weather API
38+
const response = await fetch(`https://api.weather.example/v1/${location}`);
39+
const data = await response.json();
40+
return {
41+
temperature: data.temp,
42+
conditions: data.conditions,
43+
forecast: data.forecast
44+
};
45+
}
46+
47+
/**
48+
* @ignore
49+
*/
50+
async fetch(request: Request): Promise<Response> {
51+
// ProxyToSelf handles MCP protocol compliance
52+
return new ProxyToSelf(this).fetch(request);
53+
}
54+
}
55+
```
56+
57+
**How it works:**
58+
* **TypeScript methods as MCP tools:** Each public method in your class is exposed as an MCP tool that agents can call. In this example, getWeather is the tool that fetches data from an external weather API.
59+
* **Automatic tool documentation:** JSDoc comments define the tool description, parameters, and return values, so Claude knows exactly how to call your method and interpret the response.
60+
* **Build-in MCP compatibility:** The `ProxyToSelf` class handles all MCP protocol requirements
61+
* **Enforced type safety:** Parameter and return types are automatically derived from your TypeScript definitions
3162

3263
## Get Started
3364

3465
Follow these steps to create and deploy your own MCP server on Cloudflare Workers.
3566

67+
<Aside type="note">
68+
Remote MCP servers are not supported yet. The workers-mcp tooling creates a local proxy that forwards requests to your Worker, allowing the server to be used by an MCP client.
69+
</Aside>
70+
3671

3772
### Create a new Worker
3873

@@ -97,7 +132,7 @@ export default class MyWorker extends WorkerEntrypoint<Env> {
97132
}
98133
```
99134

100-
This converts your Cloudflare Worker into an MCP server, enabling interactions with AI assistants. The key components are:
135+
This converts your Cloudflare Worker into an MCP server, enabling interactions with AI assistants. **The key components are:**
101136
- **WorkerEntrypoint:** The WorkerEntrypoint class handles all incoming request management and routing. This provides the structure needed to expose MCP tools within the Worker.
102137
- **Tool Definition:** Methods, for example, sayHello, are annotated with JSDoc, which automatically registers the method as an MCP tool. AI assistants can call this method dynamically, passing a name and receiving a greeting in response. Additional tools can be defined using the same pattern.
103138
- **ProxyToSelf:** MCP servers must follow a specific request/response format. ProxyToSelf ensures that incoming requests are properly routed to the correct MCP tools. Without this, you would need to manually parse requests and validate responses.
@@ -121,10 +156,10 @@ npm run deploy
121156
```
122157
**Note:** If you change method names, parameters, or add/remove methods, Claude and other MCP clients will not see these updates until you restart them. This is because MCP clients cache the tool metadata for performance reasons.
123158
### Connecting MCP clients to your server
124-
The workers-mcp setup command automatically configures Claude Desktop to work with your MCP server. To use your MCP server through other [MCP clients](https://modelcontextprotocol.io/clients), you'll need to configure them manually.
159+
The `workers-mcp setup` command automatically configures Claude Desktop to work with your MCP server. To use your MCP server through other [MCP clients](https://modelcontextprotocol.io/clients), you'll need to configure them manually.
125160

126161
#### Cursor
127-
To get your Cloudflare MCP server working in Cursor, you need to combine the 'command' and 'args' from your config file into a single string and use type 'command'.
162+
To get your Cloudflare MCP server working in [Cursor](https://modelcontextprotocol.io/clients#cursor), you need to combine the 'command' and 'args' from your config file into a single string and use type 'command'.
128163

129164
In Cursor, create an MCP server entry with:
130165

@@ -137,7 +172,7 @@ For example, using the same configuration as above, your Cursor command would be
137172
```
138173

139174
#### Other MCP clients
140-
For Windsurf and other MCP clients, update your configuration file to include your worker using the same format as Claude Desktop:
175+
For [Windsurf](https://modelcontextprotocol.io/clients#windsurf-editor) and other [MCP clients](https://modelcontextprotocol.io/clients#client-details), update your configuration file to include your worker using the same format as Claude Desktop:
141176
```json
142177
{
143178
"mcpServers": {

0 commit comments

Comments
 (0)