|
| 1 | +# Remote MCP Server on Cloudflare with Stainless |
| 2 | + |
| 3 | +Remote MCP servers require OAuth, so this flow implements a local version of the OAuth redirects, but instead accepts the |
| 4 | +API token and any other client configuration options that you'd need to instantiate your TypeScript client. |
| 5 | + |
| 6 | +## Usage |
| 7 | + |
| 8 | +The recommended way to use this project is to use the below "deploy to cloudflare" button to use this repo as a template for generating a server. |
| 9 | + |
| 10 | +[](https://deploy.workers.cloudflare.com/?url=https://github.com/muxinc/mux-node-sdk/tree/main/packages/mcp-server/cloudflare-worker) |
| 11 | + |
| 12 | +## Develop locally |
| 13 | + |
| 14 | +```bash |
| 15 | +# install dependencies |
| 16 | +npm install |
| 17 | + |
| 18 | +# run locally |
| 19 | +npm run dev |
| 20 | +``` |
| 21 | + |
| 22 | +You should be able to open [`http://localhost:8787/`](http://localhost:8787/) in your browser |
| 23 | + |
| 24 | +## Connect the MCP inspector to your server |
| 25 | + |
| 26 | +To explore your new MCP api, you can use the [MCP Inspector](https://modelcontextprotocol.io/docs/tools/inspector). |
| 27 | + |
| 28 | +- Start it with `npx @modelcontextprotocol/inspector` |
| 29 | +- [Within the inspector](http://localhost:5173), switch the Transport Type to `SSE` and enter `http://localhost:8787/sse` as the URL of the MCP server to connect to, and click "Connect" |
| 30 | +- You will navigate to a (mock) user/password login screen. Input any email and pass to login. |
| 31 | +- You should be redirected back to the MCP Inspector and you can now list and call any defined tools! |
| 32 | + |
| 33 | +## Connect Claude Desktop to your local MCP server |
| 34 | + |
| 35 | +The MCP inspector is great, but we really want to connect this to Claude! Follow [Anthropic's Quickstart](https://modelcontextprotocol.io/quickstart/user) and within Claude Desktop go to Settings > Developer > Edit Config to find your configuration file. |
| 36 | + |
| 37 | +Open the file in your text editor and replace it with this configuration: |
| 38 | + |
| 39 | +```json |
| 40 | +{ |
| 41 | + "mcpServers": { |
| 42 | + "mux_mux_node_api": { |
| 43 | + "command": "npx", |
| 44 | + "args": ["mcp-remote", "http://localhost:8787/sse"] |
| 45 | + } |
| 46 | + } |
| 47 | +} |
| 48 | +``` |
| 49 | + |
| 50 | +This will run a local proxy and let Claude talk to your MCP server over HTTP |
| 51 | + |
| 52 | +When you open Claude a browser window should open and allow you to login. You should see the tools available in the bottom right. Given the right prompt Claude should ask to call the tool. |
| 53 | + |
| 54 | +## Deploy to Cloudflare |
| 55 | + |
| 56 | +If you want to manually deploy this server (e.g. without the "deploy to cloudflare" button) |
| 57 | + |
| 58 | +1. `npx wrangler@latest kv namespace create remote-mcp-server-oauth-kv` |
| 59 | +2. Follow the guidance to add the kv namespace ID to `wrangler.jsonc` |
| 60 | +3. `npm run deploy` |
| 61 | + |
| 62 | +## Call your newly deployed remote MCP server from a remote MCP client |
| 63 | + |
| 64 | +Just like you did above in "Develop locally", run the MCP inspector: |
| 65 | + |
| 66 | +`npx @modelcontextprotocol/inspector@latest` |
| 67 | + |
| 68 | +Then enter the `workers.dev` URL (ex: `worker-name.account-name.workers.dev/sse`) of your Worker in the inspector as the URL of the MCP server to connect to, and click "Connect". |
| 69 | + |
| 70 | +You've now connected to your MCP server from a remote MCP client. |
| 71 | + |
| 72 | +## Connect Claude Desktop to your remote MCP server |
| 73 | + |
| 74 | +Update the Claude configuration file to point to your `workers.dev` URL (ex: `worker-name.account-name.workers.dev/sse`) and restart Claude |
| 75 | + |
| 76 | +```json |
| 77 | +{ |
| 78 | + "mcpServers": { |
| 79 | + "mux_mux_node_api": { |
| 80 | + "command": "npx", |
| 81 | + "args": ["mcp-remote", "https://worker-name.account-name.workers.dev/sse"] |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +## Debugging |
| 88 | + |
| 89 | +Should anything go wrong it can be helpful to restart Claude, or to try connecting directly to your |
| 90 | +MCP server on the command line with the following command. |
| 91 | + |
| 92 | +```bash |
| 93 | +npx mcp-remote http://localhost:8787/sse |
| 94 | +``` |
| 95 | + |
| 96 | +In some rare cases it may help to clear the files added to `~/.mcp-auth` |
| 97 | + |
| 98 | +```bash |
| 99 | +rm -rf ~/.mcp-auth |
| 100 | +``` |
0 commit comments