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
[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) or [Cursor](https://www.cursor.com/)) to interact with services directly. If you want users to access your service through an AI assistant, you can spin up an MCP server for your application.
15
21
16
22
### Building an MCP Server on Workers
17
23
18
-
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.
24
+
With Cloudflare Workers, you can turn any API or service into an MCP server with minimal setup.
19
25
20
26
Below we will run you through an example of building an MCP server that fetches weather data from an external API and exposes it as an MCP tool that Claude can call directly:
21
27
22
28
**How it works:**
23
-
***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.
24
-
***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.
25
-
***Build-in MCP compatibility:** The `ProxyToSelf` class translates incoming requests into relevant JS RPC methods
26
-
***Enforced type safety:** Parameter and return types are automatically derived from your TypeScript definitions
29
+
30
+
-**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.
31
+
-**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.
32
+
-**Build-in MCP compatibility:** The `ProxyToSelf` class translates incoming requests into relevant JS RPC methods
33
+
-**Enforced type safety:** Parameter and return types are automatically derived from your TypeScript definitions
27
34
28
35
## Get Started
29
36
30
37
Follow these steps to create and deploy your own MCP server on Cloudflare Workers.
31
38
32
39
<Asidetype="note">
33
-
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.
40
+
Remote MCP servers are not supported yet. The workers-mcp tooling creates a
41
+
local proxy that forwards requests to your Worker, allowing the server to be
42
+
used by an MCP client.
34
43
</Aside>
35
44
36
-
37
-
### Create a new Worker
45
+
### Create a new Worker
38
46
39
47
If you haven't already, install [Wrangler](https://developers.cloudflare.com/workers/wrangler/) and log in:
40
48
@@ -44,6 +52,7 @@ wrangler login
44
52
```
45
53
46
54
Initialize a new project:
55
+
47
56
```bash
48
57
npx create-cloudflare@latest my-mcp-worker
49
58
cd my-mcp-worker
@@ -54,15 +63,12 @@ cd my-mcp-worker
54
63
When you run the setup command, it will build your Worker using the configuration in your wrangler.toml or wrangler.jsonc file. By default, no additional configuration is needed, but if you have multiple Cloudflare accounts, you'll need to specify your account ID as shown below.
55
64
56
65
<WranglerConfig>
57
-
```toml
58
-
name = "my-mcp-worker"
59
-
main = "src/index.ts"
60
-
compatibility_date = "2025-03-03"
61
-
account_id = "your-account-id"
62
-
```
66
+
```toml name = "my-mcp-worker" main = "src/index.ts" compatibility_date =
67
+
"2025-03-03" account_id = "your-account-id"```
63
68
</WranglerConfig>
64
69
65
70
### Install the MCP tooling
71
+
66
72
Inside your project directory, install the [workers-mcp](https://github.com/cloudflare/workers-mcp) package:
67
73
68
74
```bash
@@ -72,105 +78,116 @@ npm install workers-mcp
72
78
This package provides the tools needed to run your Worker as an MCP server.
73
79
74
80
### Configure your Worker to support MCP
81
+
75
82
Run the following setup command:
76
83
77
84
```bash
78
85
npx workers-mcp setup
79
86
```
80
87
81
88
This guided installation process takes a brand new or existing Workers project and adds the required tooling to turn it into an MCP server:
89
+
82
90
- Automatic documentation generation
83
91
- Shared-secret security using Wrangler Secrets
84
92
- Installs a local proxy so you can access it from your MCP desktop clients (like Claude Desktop)
85
93
86
94
### Set up the MCP Server
95
+
87
96
The setup command will automatically update your src/index.ts with the following code:
* A warm, friendly greeting from your new MCP server.
96
-
* @paramname {string} The name of the person to greet.
97
-
* @return{string} The greeting message.
98
-
*/
99
-
sayHello(name:string) {
100
-
return`Hello from an MCP Worker, ${name}!`;
101
-
}
102
-
103
-
/**
104
-
* @ignore
105
-
*/
106
-
async fetch(request:Request):Promise<Response> {
107
-
// ProxyToSelf handles MCP protocol compliance.
108
-
returnnewProxyToSelf(this).fetch(request);
109
-
}
103
+
/**
104
+
* A warm, friendly greeting from your new MCP server.
105
+
* @paramname {string} The name of the person to greet.
106
+
* @return{string} The greeting message.
107
+
*/
108
+
sayHello(name:string) {
109
+
return`Hello from an MCP Worker, ${name}!`;
110
+
}
111
+
112
+
/**
113
+
* @ignore
114
+
*/
115
+
async fetch(request:Request):Promise<Response> {
116
+
// ProxyToSelf handles MCP protocol compliance.
117
+
returnnewProxyToSelf(this).fetch(request);
118
+
}
110
119
}
111
120
```
112
121
113
122
This converts your Cloudflare Worker into an MCP server, enabling interactions with AI assistants. **The key components are:**
123
+
114
124
-**WorkerEntrypoint:** The WorkerEntrypoint class handles all incoming request management and routing. This provides the structure needed to expose MCP tools within the Worker.
115
125
-**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.
116
126
-**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.
117
127
118
-
119
-
**Note:** Every public method that is annotated with JSDoc becomes an MCP tool that is discoverable by AI assistants.
128
+
**Note:** Every public method that is annotated with JSDoc becomes an MCP tool that is discoverable by AI assistants.
120
129
121
130
### Add data fetching to the MCP
122
131
123
-
When building an MCP, in many cases, you will need to connect to a resource or an API to take an action. To do this you can use the `fetch` method to make direct API calls, such as in the example below for grabbing the current wearther:
132
+
When building an MCP, in many cases, you will need to connect to a resource or an API to take an action. To do this you can use the `fetch` method to make direct API calls, such as in the example below for grabbing the current wearther:
Update your wrangler.toml with the appropriate configuration then deploy your Worker:
168
+
158
169
```bash
159
170
npx wrangler deploy
160
171
```
161
172
162
-
Your MCP server is now deployed globally and all your public class methods are exposed as MCP tools that AI assistants can now interact with.
173
+
Your MCP server is now deployed globally and all your public class methods are exposed as MCP tools that AI assistants can now interact with.
163
174
164
175
#### Updating your MCP server
176
+
165
177
When you make changes to your MCP server, run the following command to update it:
178
+
166
179
```bash
167
180
npm run deploy
168
181
```
182
+
169
183
**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.
184
+
170
185
### Connecting MCP clients to your server
186
+
171
187
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.
172
188
173
189
#### Cursor
190
+
174
191
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'.
175
192
176
193
In Cursor, create an MCP server entry with:
@@ -179,33 +196,39 @@ type: command
179
196
command: /path/to/workers-mcp run your-mcp-server-name https://your-server-url.workers.dev /path/to/your/project
180
197
181
198
For example, using the same configuration as above, your Cursor command would be:
199
+
182
200
```
183
201
/Users/username/path/to/my-new-worker/node_modules/.bin/workers-mcp run my-new-worker https://my-new-worker.username.workers.dev /Users/username/path/to/my-new-worker
184
202
```
185
203
186
204
#### Other MCP clients
205
+
187
206
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:
207
+
188
208
```json
189
209
{
190
-
"mcpServers": {
191
-
"your-mcp-server-name": {
192
-
"command": "/path/to/workers-mcp",
193
-
"args": [
194
-
"run",
195
-
"your-mcp-server-name",
196
-
"https://your-server-url.workers.dev",
197
-
"/path/to/your/project"
198
-
],
199
-
"env": {}
200
-
}
201
-
}
210
+
"mcpServers": {
211
+
"your-mcp-server-name": {
212
+
"command": "/path/to/workers-mcp",
213
+
"args": [
214
+
"run",
215
+
"your-mcp-server-name",
216
+
"https://your-server-url.workers.dev",
217
+
"/path/to/your/project"
218
+
],
219
+
"env": {}
220
+
}
221
+
}
202
222
}
203
223
```
204
-
Make sure to replace the placeholders with your actual server name, URL, and project path.
224
+
225
+
Make sure to replace the placeholders with your actual server name, URL, and project path.
226
+
205
227
### Coming soon
228
+
206
229
The Model Context Protocol spec is [actively evolving](https://github.com/modelcontextprotocol/specification/discussions) and we're working on expanding Cloudflare's MCP support. Here's what we're working on:
207
230
208
231
-**Remote MCP support**: Connect to MCP servers directly without requiring a local proxy
209
232
-**Authentication**: OAuth support for secure MCP server connections
210
233
-**Real-time communication**: SSE (Server-Sent Events) and WebSocket support for persistent connections and stateful interactions between clients and servers
211
-
-**Extended capabilities**: Native support for more MCP protocol capabilities like [resources](https://modelcontextprotocol.io/docs/concepts/resources), [prompts](https://modelcontextprotocol.io/docs/concepts/prompts) and [sampling](https://modelcontextprotocol.io/docs/concepts/sampling)
234
+
-**Extended capabilities**: Native support for more MCP protocol capabilities like [resources](https://modelcontextprotocol.io/docs/concepts/resources), [prompts](https://modelcontextprotocol.io/docs/concepts/prompts) and [sampling](https://modelcontextprotocol.io/docs/concepts/sampling)
Copy file name to clipboardExpand all lines: src/content/docs/agents/model-context-protocol/index.mdx
+33-3Lines changed: 33 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,9 +4,39 @@ pcx_content_type: navigation
4
4
sidebar:
5
5
order: 4
6
6
group:
7
-
hideIndex: true
7
+
hideIndex: false
8
8
---
9
9
10
-
import { DirectoryListing } from"~/components";
10
+
# Deploy MCP Servers to Cloudflare
11
11
12
-
<DirectoryListing />
12
+
You can build and deploy MCP servers on Cloudflare, using the [`workers-mcp` package](https://github.com/cloudflare/workers-mcp), which provides an SDK for [authorization](/agents/model-context-protocol/mcp-server/authorization/), [transport](/agents/model-context-protocol/mcp-server/transport/), and [tool definition and discovery](/agents/model-context-protocol/mcp-server/tools/).
13
+
14
+
The [getting started section](/agents/model-context-protocol/mcp-server/getting-started/) will guide you to build and deploy your first MCP server to Cloudflare.
15
+
16
+
### What is the Model Context Protocol (MCP)?
17
+
18
+
[Model Context Protocol (MCP)](https://modelcontextprotocol.io) is an open standard that connects AI systems with external applications. Think of MCP like a USB-C port for AI applications. Just as USB-C provides a standardized way to connect your devices to various accessories, MCP provides a standardized way to connect AI agents to different services.
19
+
20
+
#### MCP Terminology
21
+
22
+
-**MCP Hosts**: AI assistants (like [Claude](http://claude.ai) or [Cursor](http://cursor.com)), AI agents, or applications that need to access external capabilities.
23
+
-**MCP Clients**: Clients embedded within the MCP hosts that connect to MCP servers and invoke tools. Each MCP client instance has a single connection to an MCP server.
24
+
-**MCP Servers**: Applications that expose [tools](/agents/model-context-protocol/mcp-server/tools/), [prompts](https://modelcontextprotocol.io/docs/concepts/prompts), and [resources](https://modelcontextprotocol.io/docs/concepts/resources) that MCP clients can use.
25
+
26
+
#### Remote vs. local MCP connections
27
+
28
+
The MCP standard supports two modes of operation:
29
+
30
+
-**Remote MCP connections**: MCP clients connect to MCP servers over the Internet, establishing a [long-lived connection using HTTP and Server-Sent Events (SSE)](/agents/model-context-protocol/mcp-server/transport/), and authorizing the MCP client access to resources on the user's account using [OAuth](/agents/model-context-protocol/mcp-server/authorization/).
31
+
-**Local MCP connections**: MCP clients connect to MCP servers on the same machine, using [stdio](https://spec.modelcontextprotocol.io/specification/draft/basic/transports/#stdio) as a local transport method.
32
+
33
+
[`workers-mcp`](https://github.com/cloudflare/workers-mcp) is designed to support remote MCP connections. Remote MCP connections allow MCP clients that run in web browsers, mobile apps, and other environments outside of the end-user's machine to connect to your MCP server, such as [Claude.ai](https://www.anthropic.com/claude), and other AI agents.
34
+
35
+
### Why deploy your MCP server to Cloudflare?
36
+
37
+
- Authorization is [built-in](/agents/model-context-protocol/mcp-server/authorization/). Cloudflare handles the hard parts of the OAuth flow for you.
38
+
- Transport over HTTP with Server-Sent Events (SSE) is [built-in](/agents/model-context-protocol/mcp-server/transport/)
39
+
40
+
### Next step — deploy your first MCP server to Cloudflare
41
+
42
+
Go to [getting started](/agents/model-context-protocol/mcp-server/getting-started/) to learn how to build and deploy MCP servers to Cloudflare.
0 commit comments