-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Update Agents dev docs to add MCP server page #19689
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
372d342
Update Agents dev docs and add MCP server page
dinasaur404 7fdcc47
Update src/content/docs/agents/capabilities/mcp-server.mdx
dinasaur404 caf42d3
Update src/content/docs/agents/capabilities/mcp-server.mdx
dinasaur404 a497fdb
Update src/content/docs/agents/capabilities/mcp-server.mdx
dinasaur404 f89fd50
Update src/content/docs/agents/capabilities/mcp-server.mdx
dinasaur404 b5b5e3d
Update mcp-server.mdx
dinasaur404 bd9711d
Update mcp-server.mdx
dinasaur404 6c3dcb1
Update mcp-server.mdx
dinasaur404 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| --- | ||
| pcx_content_type: tutorial | ||
| title: Build an MCP Server | ||
| sidebar: | ||
| order: 1 | ||
| group: | ||
| hideIndex: true | ||
| description: Build and deploy an MCP server on Cloudflare Workers | ||
| --- | ||
| [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. | ||
|
|
||
| ## 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.
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. | ||
|
|
||
|
|
||
| ### 1. Create a new Worker | ||
|
|
||
| If you haven't already, install [Wrangler](https://developers.cloudflare.com/workers/wrangler/) and log in: | ||
|
|
||
| ```bash | ||
dinasaur404 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| npm install wrangler | ||
| wrangler login | ||
| ``` | ||
|
|
||
| Initialize a new project: | ||
| ```bash | ||
| npx create-cloudflare@latest my-mcp-worker | ||
| cd my-mcp-worker | ||
| ``` | ||
|
|
||
| ### 2. 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. | ||
|
|
||
dinasaur404 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ### 3. Set up the MCP Server on Workers | ||
| 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}!`; | ||
| } | ||
|
|
||
| /** | ||
| * @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. | ||
|
|
||
|
|
||
| ### 4. Deploy the MCP Server | ||
| Update your wrangler.toml with the appropriate configuration then deploy your Worker: | ||
| ```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. | ||
|
|
||
| ### Expanding the MCP Server Capabilities | ||
| Use existing Cloudflare products to expand the functionality of your MCP server. You can: | ||
| - Send emails using [Email Routing](/email-routing/) | ||
| - Capture and share website previews using [Browser Rendering](/browser-rendering/) | ||
| - Store and manage sessions, user data, or other persistent information with [Durable Objects](/durable-objects/) | ||
| - Query and update data using a [D1 database](/d1/) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Do we need to caveat local only somewhere early?
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.
There are currently only local MCP clients, so it's not really necessary to specify right now. But as long as we update the
workers-mcppackage when remote MCP drops I think this documentation can stay unchanged?