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
This guide will show you how to deploy a stateless MCP server on Cloudflare Workers using `experimental_createMcpHandler`. This is the simplest way to get started with MCP on Cloudflare.
12
+
This guide will show you how to deploy a stateless Model Context Protocol Server with Cloudflare Workers using the `experimental_createMcpHandler`. This is the simplest way to get started with building MCP servers.
13
13
14
-
Unlike the Durable Object-based approach, stateless MCP servers run as standard Cloudflare Workers, making them simpler to deploy and manage while still providing the majority of MCP functionality.
14
+
Unlike the [`McpAgent`](/agents/guides/remote-mcp-server/) which is backed by a Durable Object, this handler runs MCP servers on standard Cloudflare Workers, making them simpler to deploy and reason about while still providing the majority of MCP functionality.
15
15
16
-
This handler supports MCP Servers with Tools, Prompts and Resources. For more complex capabilities like Elicitations you will need to use the `McpAgent` class.
16
+
The `experimental_createMcpHandler` handler supports MCP Servers with Tools, Prompts and Resources. For more complex capabilities like Elicitations you will need to use the `McpAgent` class.
17
17
18
18
You can start by deploying an **unauthenticated server** where anyone can connect and use the capabilities (no login required), or you can deploy an **authenticated server** where users must sign in.
19
19
20
+
This template includes a basic MCP server implementation that you can customize with your own tools, prompts, and resources. After deploying or creating from the template, you can follow the sections below to understand how to build and customize your stateless MCP server.
21
+
20
22
## Unauthenticated Stateless MCP Server
21
23
22
-
An unauthenticated MCP server is the simplest way to expose MCP tools. This is ideal for public APIs, demonstration purposes, or internal tools that don't require user authentication.
24
+
An unauthenticated MCP server is the simplest way to expose MCP tools. This is ideal for public APIs and demonstration purposes.
25
+
26
+
The fastest way to get started is to use the MCP Worker template from the [cloudflare/agents repository](https://github.com/cloudflare/agents/tree/main/examples/mcp-worker).
27
+
28
+
[](https://deploy.workers.cloudflare.com/?url=https://github.com/cloudflare/agents/tree/main/examples/mcp-worker)
23
29
24
-
### Create your server
30
+
Alternatively you can follow the steps below to create a new MCP server from scratch.
31
+
32
+
### Create your project
25
33
26
34
Create a new directory for your MCP server and initialize it with the necessary files:
Your MCP server is now running on `http://localhost:8787/mcp`.
115
+
Your MCP server is now running on `http://localhost:8787/mcp`.
111
116
112
117
In a new terminal, run the [MCP inspector](https://github.com/modelcontextprotocol/inspector).
113
118
@@ -129,11 +134,15 @@ After deploying, your MCP server will be available at `https://my-stateless-mcp-
129
134
130
135
You can now test your deployed server using the MCP inspector by entering your Worker's URL.
131
136
132
-
## Authenticated stateless MCP server
137
+
## Adding Authentication to your MCP Server
138
+
139
+
OAuth-based user authentication allows you to control access and identify users. It uses the [Cloudflare Workers OAuth Provider](https://github.com/cloudflare/workers-oauth-provider) to handle the OAuth flow. Get started with the Authenticated MCP Worker template from the [cloudflare/agents repository](https://github.com/cloudflare/agents/tree/main/examples/mcp-worker-authenticated).
140
+
141
+
[](https://deploy.workers.cloudflare.com/?url=https://github.com/cloudflare/agents/tree/main/examples/mcp-worker-authenticated)
133
142
134
-
An authenticated MCP server adds OAuth-based user authentication, allowing you to control access and identify users. It uses the [Cloudflare Workers OAuth Provider](https://github.com/cloudflare/workers-oauth-provider)to handle the OAuth flow.
143
+
Alternatively, you follow the steps below to add authentication to your stateless MCP server.
135
144
136
-
### Create your authenticated server
145
+
### Create your project
137
146
138
147
Create a new directory and initialize it:
139
148
@@ -153,7 +162,7 @@ Install the required dependencies:
153
162
154
163
### Set up KV namespace
155
164
156
-
Create a KV namespace to store OAuth tokens and client registrations:
165
+
The OAuth provider requires a KV namespace to store OAuth tokens and client registrations:
157
166
158
167
```bash
159
168
npx wrangler kv:namespace create OAUTH_KV
@@ -189,31 +198,31 @@ Create a `src/auth-handler.ts` file to handle the OAuth flow:
Copy file name to clipboardExpand all lines: src/content/docs/agents/model-context-protocol/index.mdx
+1Lines changed: 1 addition & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -29,6 +29,7 @@ The MCP standard supports two modes of operation:
29
29
-**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.
30
30
31
31
### Best Practices
32
+
32
33
-**Tool design**: Do not treat your MCP server as a wrapper around your full API schema. Instead, build tools that are optimized for specific user goals and reliable outcomes. Fewer, well-designed tools often outperform many granular ones, especially for agents with small context windows or tight latency budgets.
33
34
-**Scoped permissions**: Deploying several focused MCP servers, each with narrowly scoped permissions, reduces the risk of over-privileged access and makes it easier to manage and audit what each server is allowed to do.
34
35
-**Tool descriptions**: Detailed parameter descriptions help agents understand how to use your tools correctly — including what values are expected, how they affect behavior, and any important constraints. This reduces errors and improves reliability.
0 commit comments