-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Add MCP server documentation #20634
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add MCP server documentation #20634
Changes from 1 commit
1195e85
7acd80d
b6df6ac
9e33d5e
af2e177
c58405c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| --- | ||
| pcx_content_type: tutorial | ||
| title: Build an MCP Server | ||
| sidebar: | ||
| order: 100 | ||
| group: | ||
| hideIndex: true | ||
| description: Build and deploy an MCP server on Cloudflare Workers | ||
| --- | ||
|
|
||
| import { MetaInfo, Render, Type, TypeScriptExample, WranglerConfig } from "~/components"; | ||
| import { Aside } from '@astrojs/starlight/components'; | ||
|
|
||
| [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. | ||
|
||
|
|
||
| ## Building an MCP Server on Cloudflare Workers | ||
|
|
||
| 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 | ||
|
||
| 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. | ||
|
||
|
|
||
| <Aside type="caution"> | ||
| 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. | ||
dinasaur404 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| </Aside> | ||
|
|
||
| ## Benefits | ||
|
|
||
| - **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. | ||
dinasaur404 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| - **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. | ||
| - **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. | ||
| - **Expand MCP server capabilities:** Easily connect to Workers AI, D1, Durable Objects, and other Cloudflare services to add more functionality to your MCP Server. | ||
|
|
||
| ## Get Started | ||
|
|
||
| Follow these steps to create and deploy your own MCP server on Cloudflare Workers. | ||
|
|
||
|
|
||
| ### Create a new Worker | ||
|
|
||
| If you haven't already, install [Wrangler](https://developers.cloudflare.com/workers/wrangler/) and log in: | ||
|
|
||
| ```bash | ||
| npm install wrangler | ||
| wrangler login | ||
| ``` | ||
|
|
||
| Initialize a new project: | ||
| ```bash | ||
| npx create-cloudflare@latest my-mcp-worker | ||
| cd my-mcp-worker | ||
| ``` | ||
|
|
||
| ### Install the MCP tooling | ||
| Inside your project directory, install the [workers-mcp](https://github.com/cloudflare/workers-mcp) package: | ||
|
|
||
| ```bash | ||
| npm install workers-mcp | ||
| ``` | ||
|
|
||
| This package provides the tools needed to run your Worker as an MCP server. | ||
|
|
||
| ### Configure your Worker to support MCP | ||
| Run the following setup command: | ||
|
|
||
| ```bash | ||
| npx workers-mcp setup | ||
| ``` | ||
|
|
||
| This guided installation process takes a brand new or existing Workers project and adds the required tooling to turn it into an MCP server: | ||
| - Automatic documentation generation | ||
| - Shared-secret security using Wrangler Secrets | ||
| - Installs a local proxy so you can access it from your MCP desktop clients (like Claude Desktop) | ||
|
|
||
| ### Set up the MCP Server | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would help to find a way to bring the code way up to the top as a kind of:
Think faster we can show "here is code" and get someone saying "okay i get it, that is simple" — then after doing the step by step — then we don't bury the example |
||
| Replace the contents of your src/index.ts with the following boilerplate code: | ||
|
|
||
| ```ts | ||
| import { WorkerEntrypoint } from 'cloudflare:workers'; | ||
| import { ProxyToSelf } from 'workers-mcp'; | ||
|
|
||
| export default class MyWorker extends WorkerEntrypoint<Env> { | ||
| /** | ||
| * A warm, friendly greeting from your new MCP server. | ||
| * @param name {string} The name of the person to greet. | ||
| * @return {string} The greeting message. | ||
| */ | ||
| sayHello(name: string) { | ||
| return `Hello from an MCP Worker, ${name}!`; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be really good to have some other step that allows you to call an API. even like the weather one that's in the MCP docs. but really showing what this allows you to do which is integrate with stuff
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Think this is worthwhile |
||
| } | ||
|
|
||
| /** | ||
| * @ignore | ||
| */ | ||
| async fetch(request: Request): Promise<Response> { | ||
| // ProxyToSelf handles MCP protocol compliance. | ||
| return new ProxyToSelf(this).fetch(request); | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| This converts your Cloudflare Worker into an MCP server, enabling interactions with AI assistants. The key components are: | ||
| - **WorkerEntrypoint:** The WorkerEntrypoint class handles all incoming request management and routing. This provides the structure needed to expose MCP tools within the Worker. | ||
| - **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. | ||
| - **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. | ||
|
|
||
|
|
||
| **Note:** Every public method that is annotated with JSDoc becomes an MCP tool that is discoverable by AI assistants. | ||
|
|
||
|
|
||
| ### Deploy the MCP server | ||
| Update your wrangler.toml with the appropriate configuration then deploy your Worker: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if there's anything you actually need in your wrangler.toml can you include it here and have wrangler.jsonc and wrangler.toml such as here: |
||
| ```bash | ||
| wrangler deploy | ||
| ``` | ||
|
|
||
| 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. | ||
|
|
||
| #### Updating your MCP server | ||
| When you make changes to your MCP server, run the following command to update it: | ||
| ```bash | ||
| npm run deploy | ||
| ``` | ||
| **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. | ||
| ### Connecting MCP clients to your server | ||
| 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. | ||
dinasaur404 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| #### Cursor | ||
| 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'. | ||
|
|
||
| In Cursor, create an MCP server entry with: | ||
|
|
||
| type: command | ||
| command: /path/to/workers-mcp run your-mcp-server-name https://your-server-url.workers.dev /path/to/your/project | ||
|
|
||
| For example, using the same configuration as above, your Cursor command would be: | ||
| ``` | ||
| /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 | ||
| ``` | ||
|
|
||
| #### Other MCP clients | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| For Windsurf and other MCP clients, update your configuration file to include your worker using the same format as Claude Desktop: | ||
dinasaur404 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ```json | ||
| { | ||
| "mcpServers": { | ||
| "your-mcp-server-name": { | ||
| "command": "/path/to/workers-mcp", | ||
| "args": [ | ||
| "run", | ||
| "your-mcp-server-name", | ||
| "https://your-server-url.workers.dev", | ||
| "/path/to/your/project" | ||
| ], | ||
| "env": {} | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
| Make sure to replace the placeholders with your actual server name, URL, and project path. | ||
| ### Coming soon | ||
| 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: | ||
|
|
||
| - **Remote MCP support**: Connect to MCP servers directly without requiring a local proxy | ||
| - **Authentication**: OAuth support for secure MCP server connections | ||
| - **Real-time communication**: SSE (Server-Sent Events) and WebSocket support for persistent connections and stateful interactions between clients and servers | ||
| - **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) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we name some to orient people to what we mean?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah i think some examples would be useful.