Skip to content

Commit 669184c

Browse files
committed
feat: add workers bindings mcp server
1 parent 4c7ef2f commit 669184c

21 files changed

+11177
-24
lines changed

apps/workers-bindings/.gitignore

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
node_modules
2+
3+
.nx
4+
.idea
5+
.vscode
6+
.zed
7+
# Logs
8+
9+
logs
10+
_.log
11+
npm-debug.log_
12+
yarn-debug.log*
13+
yarn-error.log*
14+
lerna-debug.log*
15+
.pnpm-debug.log*
16+
17+
# Diagnostic reports (https://nodejs.org/api/report.html)
18+
19+
report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
20+
21+
# Runtime data
22+
23+
pids
24+
_.pid
25+
_.seed
26+
\*.pid.lock
27+
28+
# Directory for instrumented libs generated by jscoverage/JSCover
29+
30+
lib-cov
31+
32+
# Coverage directory used by tools like istanbul
33+
34+
coverage
35+
\*.lcov
36+
37+
# nyc test coverage
38+
39+
.nyc_output
40+
41+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
42+
43+
.grunt
44+
45+
# Bower dependency directory (https://bower.io/)
46+
47+
bower_components
48+
49+
# node-waf configuration
50+
51+
.lock-wscript
52+
53+
# Compiled binary addons (https://nodejs.org/api/addons.html)
54+
55+
build/Release
56+
57+
# Dependency directories
58+
59+
node_modules/
60+
jspm_packages/
61+
62+
# Snowpack dependency directory (https://snowpack.dev/)
63+
64+
web_modules/
65+
66+
# TypeScript cache
67+
68+
\*.tsbuildinfo
69+
70+
# Optional npm cache directory
71+
72+
.npm
73+
74+
# Optional eslint cache
75+
76+
.eslintcache
77+
78+
# Optional stylelint cache
79+
80+
.stylelintcache
81+
82+
# Microbundle cache
83+
84+
.rpt2_cache/
85+
.rts2_cache_cjs/
86+
.rts2_cache_es/
87+
.rts2_cache_umd/
88+
89+
# Optional REPL history
90+
91+
.node_repl_history
92+
93+
# Output of 'npm pack'
94+
95+
\*.tgz
96+
97+
# Yarn Integrity file
98+
99+
.yarn-integrity
100+
101+
# dotenv environment variable files
102+
103+
.env
104+
.env.development.local
105+
.env.test.local
106+
.env.production.local
107+
.env.local
108+
109+
# parcel-bundler cache (https://parceljs.org/)
110+
111+
.cache
112+
.parcel-cache
113+
114+
# Next.js build output
115+
116+
.next
117+
out
118+
119+
# Nuxt.js build / generate output
120+
121+
.nuxt
122+
dist
123+
124+
# Gatsby files
125+
126+
.cache/
127+
128+
# Comment in the public line in if your project uses Gatsby and not Next.js
129+
130+
# https://nextjs.org/blog/next-9-1#public-directory-support
131+
132+
# public
133+
134+
# vuepress build output
135+
136+
.vuepress/dist
137+
138+
# vuepress v2.x temp and cache directory
139+
140+
.temp
141+
.cache
142+
143+
# Docusaurus cache and generated files
144+
145+
.docusaurus
146+
147+
# Serverless directories
148+
149+
.serverless/
150+
151+
# FuseBox cache
152+
153+
.fusebox/
154+
155+
# DynamoDB Local files
156+
157+
.dynamodb/
158+
159+
# TernJS port file
160+
161+
.tern-port
162+
163+
# Stores VSCode versions used for testing VSCode extensions
164+
165+
.vscode-test
166+
167+
# yarn v2
168+
169+
.yarn/cache
170+
.yarn/unplugged
171+
.yarn/build-state.yml
172+
.yarn/install-state.gz
173+
.pnp.\*
174+
175+
# wrangler project
176+
177+
.dev.vars
178+
.wrangler/

apps/workers-bindings/README.md

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
# Remote MCP Server on Cloudflare
2+
3+
Let's get a remote MCP server up-and-running on Cloudflare Workers complete with OAuth login!
4+
5+
## Develop locally
6+
7+
```bash
8+
# clone the repository
9+
git clone [email protected]:cloudflare/ai.git
10+
11+
# install dependencies
12+
cd ai
13+
npm install
14+
15+
# run locally
16+
npx nx dev remote-mcp-server
17+
```
18+
19+
You should be able to open [`http://localhost:8787/`](http://localhost:8787/) in your browser
20+
21+
## Connect the MCP inspector to your server
22+
23+
To explore your new MCP api, you can use the [MCP Inspector](https://modelcontextprotocol.io/docs/tools/inspector).
24+
25+
- Start it with `npx @modelcontextprotocol/inspector`
26+
- [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"
27+
- You will navigate to a (mock) user/password login screen. Input any email and pass to login.
28+
- You should be redirected back to the MCP Inspector and you can now list and call any defined tools!
29+
30+
<div align="center">
31+
<img src="img/mcp-inspector-sse-config.png" alt="MCP Inspector with the above config" width="600"/>
32+
</div>
33+
34+
<div align="center">
35+
<img src="img/mcp-inspector-successful-tool-call.png" alt="MCP Inspector with after a tool call" width="600"/>
36+
</div>
37+
38+
## Connect Claude Desktop to your local MCP server
39+
40+
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.
41+
42+
Open the file in your text editor and replace it with this configuration:
43+
44+
```json
45+
{
46+
"mcpServers": {
47+
"math": {
48+
"command": "npx",
49+
"args": [
50+
"mcp-remote",
51+
"http://localhost:8787/sse"
52+
]
53+
}
54+
}
55+
}
56+
```
57+
58+
This will run a local proxy and let Claude talk to your MCP server over HTTP
59+
60+
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.
61+
62+
<div align="center">
63+
<img src="img/available-tools.png" alt="Clicking on the hammer icon shows a list of available tools" width="600"/>
64+
</div>
65+
66+
<div align="center">
67+
<img src="img/claude-does-math-the-fancy-way.png" alt="Claude answers the prompt 'I seem to have lost my calculator and have run out of fingers. Could you use the math tool to add 23 and 19?' by invoking the MCP add tool" width="600"/>
68+
</div>
69+
70+
## Deploy to Cloudflare
71+
72+
1. `npx wrangler kv namespace create OAUTH_KV`
73+
2. Follow the guidance to add the kv namespace ID to `wrangler.jsonc`
74+
3. `npm run deploy`
75+
76+
## Call your newly deployed remote MCP server from a remote MCP client
77+
78+
Just like you did above in "Develop locally", run the MCP inspector:
79+
80+
`npx @modelcontextprotocol/inspector@latest`
81+
82+
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".
83+
84+
You've now connected to your MCP server from a remote MCP client.
85+
86+
## Connect Claude Desktop to your remote MCP server
87+
88+
Update the Claude configuration file to point to your `workers.dev` URL (ex: `worker-name.account-name.workers.dev/sse`) and restart Claude
89+
90+
```json
91+
{
92+
"mcpServers": {
93+
"math": {
94+
"command": "npx",
95+
"args": [
96+
"mcp-remote",
97+
"https://worker-name.account-name.workers.dev/sse"
98+
]
99+
}
100+
}
101+
}
102+
```
103+
104+
## Debugging
105+
106+
Should anything go wrong it can be helpful to restart Claude, or to try connecting directly to your
107+
MCP server on the command line with the following command.
108+
109+
```bash
110+
npx mcp-remote http://localhost:8787/sse
111+
```
112+
113+
In some rare cases it may help to clear the files added to `~/.mcp-auth`
114+
115+
```bash
116+
rm -rf ~/.mcp-auth
117+
```

apps/workers-bindings/biome.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"$schema": "https://biomejs.dev/schemas/1.6.2/schema.json",
3+
"organizeImports": {
4+
"enabled": true
5+
},
6+
"files": {
7+
"ignore": ["worker-configuration.d.ts"]
8+
},
9+
"vcs": {
10+
"enabled": true,
11+
"clientKind": "git",
12+
"useIgnoreFile": true
13+
},
14+
"linter": {
15+
"enabled": true,
16+
"rules": {
17+
"recommended": true,
18+
"suspicious": {
19+
"noExplicitAny": "off",
20+
"noDebugger": "off",
21+
"noConsoleLog": "off",
22+
"noConfusingVoidType": "off"
23+
},
24+
"style": {
25+
"noNonNullAssertion": "off"
26+
}
27+
}
28+
},
29+
"formatter": {
30+
"enabled": true,
31+
"indentWidth": 4,
32+
"lineWidth": 100
33+
}
34+
}
91.5 KB
Loading
466 KB
Loading
403 KB
Loading
333 KB
Loading
487 KB
Loading
426 KB
Loading

0 commit comments

Comments
 (0)