|
| 1 | +--- |
| 2 | +pcx_content_type: concept |
| 3 | +title: Getting Started |
| 4 | +sidebar: |
| 5 | + order: 2 |
| 6 | +--- |
| 7 | + |
| 8 | +import { Details, Render, PackageManagers } from "~/components"; |
| 9 | + |
| 10 | +# Deploy an MCP Server to Cloudflare |
| 11 | + |
| 12 | +You can build and deploy MCP servers on Cloudflare, using the `@cloudflare/model-context-protocol` package, which provides an SDK for [authorization](/agents/model-context-protocol/mcp-server/authorization/), [transport](/agents/model-context-protocol/mcp-server/transport/), and [tool definition and discovery](/agents/model-context-protocol/mcp-server/tools/). |
| 13 | + |
| 14 | +This guide and the accompanying [examples](/agents/model-context-protocol/mcp-server/examples/) will help you get started. |
| 15 | + |
| 16 | +### What is the Model Context Protocol (MCP)? |
| 17 | + |
| 18 | +[Model Context Protocol (MCP)](https://modelcontextprotocol.io) is an open standard that connects AI systems with external applications. Think of MCP like a USB-C port for AI applications. Just as USB-C provides a standardized way to connect your devices to various accessories, MCP provides a standardized way to connect AI agents to different services. |
| 19 | + |
| 20 | +#### MCP Terminology |
| 21 | + |
| 22 | +- **MCP Hosts**: AI assistants (like [Claude](http://claude.ai) or [Cursor](http://cursor.com)), AI agents, or applications that need to access external capabilities. |
| 23 | +- **MCP Clients**: Clients embedded within the MCP hosts that connect to MCP servers and invoke tools. Each MCP client instance has a single connection to an MCP server. |
| 24 | +- **MCP Servers**: Applications that expose [tools](/agents/model-context-protocol/mcp-server/tools/), [prompts](https://modelcontextprotocol.io/docs/concepts/prompts), and [resources](https://modelcontextprotocol.io/docs/concepts/resources) that MCP clients can use. |
| 25 | + |
| 26 | +#### Remote vs. local MCP connections |
| 27 | + |
| 28 | +The MCP standard supports two modes of operation: |
| 29 | + |
| 30 | +- **Remote MCP connections**: MCP clients connect to MCP servers over the Internet, establishing a [long-lived connection using HTTP and Server-Sent Events (SSE)](/agents/model-context-protocol/mcp-server/transport/), and authorizing the MCP client access to resources on the user's account using [OAuth](/agents/model-context-protocol/mcp-server/authorization/). |
| 31 | +- **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. |
| 32 | + |
| 33 | +Cloudflare's MCP Server SDK, is designed to support remote MCP connections. Remote MCP connections allow MCP clients that run in web browsers, mobile apps, and other environments outside of the end-user's machine to connect to your MCP server, such as [Claude.ai](https://www.anthropic.com/claude), and other AI agents. |
| 34 | + |
| 35 | +## Deploy your first MCP server |
| 36 | + |
| 37 | +{/* TODO: Update deploy to Workers button */} |
| 38 | +The best way to get started is to deploy an [example MCP server](https://github.com/geelen/mcp-remote-examples/tree/main/02-user-password) to your Cloudflare account. You can then customize this example to suit your needs. |
| 39 | + |
| 40 | +### Option 1: Deploy your MCP server first, then develop locally |
| 41 | + |
| 42 | +{/* TODO: Update link to example */} |
| 43 | +The link below will guide you through everything you need to do to deploy an [example MCP server](https://github.com/geelen/mcp-remote-examples/tree/main/02-user-password) to your Cloudflare account: |
| 44 | + |
| 45 | +{/* TODO: Update deploy to Workers button */} |
| 46 | +[](https://deploy.workers.cloudflare.com/?url=https://github.com/cloudflare/mcp-server-example) |
| 47 | + |
| 48 | +At the end of this process, you will have a new git repository on your GitHub or GitLab account for your MCP server that is configured to automatically deploy Cloudflare each time you push a change or merge a pull request to the main branch of the repository. You can then clone this repository, [develop locally](/agents/model-context-protocol/mcp-server/getting-started/#local-development), and customize the code to suit your needs. |
| 49 | + |
| 50 | +### Option 2: Create a new MCP Server via the CLI |
| 51 | + |
| 52 | +Alternatively, you can use the command line as shown below to create a new MCP Server on your local machine. |
| 53 | + |
| 54 | +{/* TODO: Update path to example */} |
| 55 | + |
| 56 | +<PackageManagers |
| 57 | + type="create" |
| 58 | + pkg="cloudflare@latest" |
| 59 | + args={"my-mcp-server --template=geelen/mcp-remote-examples/02-user-password"} |
| 60 | +/> |
| 61 | + |
| 62 | +Now, you have the MCP server setup, with dependencies installed. Move into that project folder: |
| 63 | + |
| 64 | +```sh |
| 65 | +cd my-mcp-server |
| 66 | +``` |
| 67 | + |
| 68 | +### Local development |
| 69 | + |
| 70 | +In the directory of your new project, run the following command to start the development server: |
| 71 | + |
| 72 | +```sh |
| 73 | +npm start |
| 74 | +``` |
| 75 | + |
| 76 | +Your MCP server is now running on `http://localhost:8787/sse`. |
| 77 | + |
| 78 | +In a new terminal, run the [MCP inspector](https://github.com/modelcontextprotocol/mcp-inspector). The MCP inspector is an interactive MCP client that allows you to connect to your MCP server and invoke tools from a web browser. |
| 79 | + |
| 80 | +```sh |
| 81 | +npx @modelcontextprotocol/inspector@latest |
| 82 | +``` |
| 83 | + |
| 84 | +Open the MCP inspector in your web browser: |
| 85 | + |
| 86 | +```sh |
| 87 | +open http://localhost:5173 |
| 88 | +``` |
| 89 | + |
| 90 | +In the inspector, enter the URL of your MCP server, `http://localhost:8787/sse`, and click **Connect**: |
| 91 | + |
| 92 | + |
| 93 | + |
| 94 | +You will be redirected to an example OAuth login page. Enter any username and password and click "Log in and approve" to continue. (you can add your own authentication and/or authorization provider to replace this. Refer to the [authorization](/agents/model-context-protocol/mcp-server/authorization/) section for details on how to do this.) |
| 95 | + |
| 96 | + |
| 97 | + |
| 98 | +Once you have logged in, you will be redirected back to the inspector. You should see the "List Tools" button, which will list the tools that your MCP server exposes. |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | +### Deploy your MCP server |
| 103 | + |
| 104 | +You can deploy your MCP server to Cloudflare using the following [Wrangler CLI command](/workers/wrangler) within the example project: |
| 105 | + |
| 106 | +```sh |
| 107 | +npx wrangler@latest deploy |
| 108 | +``` |
| 109 | + |
| 110 | +If you have already [connected a git repository](/workers/ci-cd/builds/) to the Worker with your MCP server, you can deploy your MCP server by pushing a change or merging a pull request to the main branch of the repository. |
| 111 | + |
| 112 | +### Next Steps |
| 113 | + |
| 114 | +- Add [tools](/agents/model-context-protocol/mcp-server/tools/) to your MCP server. |
| 115 | +- Customize your MCP Server's [authentication and authorization](/agents/model-context-protocol/mcp-server/authorization/). |
| 116 | +- Try other [example MCP servers](/agents/model-context-protocol/mcp-server/examples/) |
0 commit comments