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/capabilities/mcp-server.mdx
+21-23Lines changed: 21 additions & 23 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,32 +1,32 @@
1
1
---
2
2
pcx_content_type: tutorial
3
-
title: Build MCP Server on Cloudflare Workers
3
+
title: Build an MCP Server
4
4
sidebar:
5
5
order: 1
6
6
group:
7
7
hideIndex: true
8
8
description: Build and deploy an MCP server on Cloudflare Workers
9
9
---
10
+
[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.
10
11
11
-
# Building an MCP Server on Cloudflare Workers
12
+
##Building an MCP Server on Cloudflare Workers
12
13
13
-
Model Context Protocol (MCP) is an open standard for interacting with AI services and applications. MCP is like a standardized interface that LLMs can use to connect to various data sources and tools. With Cloudflare Workers, you can easily build and deploy an MCP server that grants AI assistants access to your service’s functionality.
14
+
Normally, setting up an MCP server requires handling infrastructure, API routing, and understanding 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.
15
+
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.
14
16
15
-
## Why Build an MCP Server on Cloudflare Workers?
17
+
## Benefits
16
18
17
-
Using Cloudflare Workers to run your MCP server offers several key benefits:
18
-
19
-
-**Minimal setup & built-in boilerplate:** Workers automatically handle API routing, server management, and protocol compliance. The [workers-mcp](https://www.npmjs.com/package/workers-mcp) package bootstraps your MCP server, so you can focus on your service logic.
19
+
-**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.
20
20
-**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.
21
-
-**Scalability & performance:** Deploy your MCP server on Cloudflare’s global edge network so that users can make fast, performant requests from their LLMs. Cloudflare automatically handles traffic spikes and high load, ensuring your service remains responsive regardless of demand.
22
-
-**Expand MCP Server capabilities:** Easily connect to Workers AI, D1 databases, Durable Objects, and other Cloudflare services to add more functionality to your MCP Server.
21
+
-**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.
22
+
-**Expand MCP server capabilities:** Easily connect to Workers AI, D1, Durable Objects, and other Cloudflare services to add more functionality to your MCP Server.
23
23
24
24
## Get Started
25
25
26
26
Follow these steps to create and deploy your own MCP server on Cloudflare Workers.
27
27
28
28
29
-
### 1. Create a New Worker Project
29
+
### 1. Create a new Worker
30
30
31
31
If you haven't already, install [Wrangler](https://developers.cloudflare.com/workers/wrangler/) and log in:
32
32
@@ -42,7 +42,7 @@ cd my-mcp-project
42
42
```
43
43
44
44
### 2. Install the MCP Tooling
45
-
Inside your project directory, install the workers-mcp package:
45
+
Inside your project directory, install the [workers-mcp](https://github.com/cloudflare/workers-mcp) package:
46
46
47
47
```bash
48
48
npm install workers-mcp
@@ -51,20 +51,12 @@ npm install workers-mcp
51
51
This package provides the tools needed to run your Worker as an MCP server.
52
52
53
53
### 3. Set up the MCP Server on Workers
54
-
Replace the contents of your src/index.ts (or index.js) with the following boilerplate code:
54
+
Replace the contents of your src/index.ts with the following boilerplate code:
**Note:** Every public method (like sayHello) that is annotated with JSDoc becomes an MCP tool that is discoverable by AI assistants.
80
+
This converts your Cloudflare Worker into an MCP server, enabling interactions with AI assistants. The key components are:
81
+
-**WorkerEntrypoint:** The WorkerEntrypoint class handles all incoming request management and routing. This provides the structure needed to expose MCP tools within the Worker.
82
+
-**Tool Definition:** Methods (e.g. sayHello) are annotated with JSDoc, which automatically registers the method it 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.
83
+
-**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.
84
+
85
+
86
+
**Note:** Every public method that is annotated with JSDoc becomes an MCP tool that is discoverable by AI assistants.
87
+
90
88
91
89
### 4. Deploy the MCP Server
92
90
Update your wrangler.toml with the appropriate configuration then deploy your Worker:
@@ -97,7 +95,7 @@ wrangler deploy
97
95
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.
98
96
99
97
### Expanding the MCP Server capabilities
100
-
Use existing Cloudflare products to expand the functionality of your MCP Server. You can:
98
+
Use existing Cloudflare products to expand the functionality of your MCP server. You can:
101
99
- Send emails using [Email Routing](/email-routing/)
102
100
- Capture and share website previews using [Browser Rendering](/browser-rendering/)
103
101
- Store and manage sessions, user data, or other persistent information with [Durable Objects](/durable-objects/)
0 commit comments