You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/docs/agents/examples/build-mcp-server.mdx
+52-17Lines changed: 52 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,34 +5,69 @@ sidebar:
5
5
order: 100
6
6
group:
7
7
hideIndex: true
8
-
description: Build and deployan 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.
[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.
15
15
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.
17
17
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?
20
19
21
-
<Asidetype="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
24
23
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:
-**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.
***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
31
62
32
63
## Get Started
33
64
34
65
Follow these steps to create and deploy your own MCP server on Cloudflare Workers.
35
66
67
+
<Asidetype="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.
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:**
101
136
-**WorkerEntrypoint:** The WorkerEntrypoint class handles all incoming request management and routing. This provides the structure needed to expose MCP tools within the Worker.
102
137
-**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.
103
138
-**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
121
156
```
122
157
**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.
123
158
### 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.
125
160
126
161
#### 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'.
128
163
129
164
In Cursor, create an MCP server entry with:
130
165
@@ -137,7 +172,7 @@ For example, using the same configuration as above, your Cursor command would be
137
172
```
138
173
139
174
#### 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:
0 commit comments