-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Add MCP Server page to Agents documentation #19686
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
Closed
Closed
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
80edce8
Add MCP Server page to Agents documentation
dinasaur404 2449e35
Update docs with latest changes
dinasaur404 2154e89
Update src/content/docs/agents/capabilities/mcp-server.mdx
dinasaur404 469058b
Update src/content/docs/agents/capabilities/mcp-server.mdx
dinasaur404 1f83624
Update src/content/docs/agents/capabilities/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 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. | ||
| 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. | ||
| - **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. | ||
dinasaur404 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| - **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 | ||
| npm install -g wrangler | ||
| wrangler login | ||
| ``` | ||
|
|
||
| Initialize a new project: | ||
| ```bash | ||
| wrangler init my-mcp-project | ||
| cd my-mcp-project | ||
| ``` | ||
|
|
||
| ### 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. | ||
|
|
||
| ### 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 (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. | ||
dinasaur404 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| - **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.
Uh oh!
There was an error while loading. Please reload this page.