让我们在Cloudflare Workers上启动并运行一个带有OAuth登录功能的远程MCP服务器!
# 克隆仓库
git clone git@github.com:cloudflare/ai.git
# 安装依赖
cd ai
npm install
# 本地运行
npx nx dev remote-mcp-server
你应该能够在浏览器中打开http://localhost:8787/。
为了探索新的MCP API,你可以使用MCP检查器。
npx @modelcontextprotocol/inspector启动它SSE,并在MCP服务器URL中输入http://localhost:8787/sse,然后点击“连接”虽然MCP检查器很棒,但我们真正想要的是将其连接到Claude!请遵循Anthropic的快速入门指南,并在Claude Desktop中前往设置 > 开发者 > 编辑配置以找到你的配置文件。
在文本编辑器中打开该文件,并替换为以下配置:
{
"mcpServers": {
"math": {
"command": "npx",
"args": [
"mcp-remote",
"http://localhost:8787/sse"
]
}
}
}
这将运行一个本地代理,让Claude通过HTTP与你的MCP服务器通信。
当你打开Claude时,应该会有一个浏览器窗口弹出并允许你登录。你应该能在右下角看到可用的工具。给定正确的提示,Claude应该会请求调用工具。
<div align="center"> <img src="img/available-tools.png" alt="点击锤子图标显示可用工具列表" width="600"/> </div> <div align="center"> <img src="img/claude-does-math-the-fancy-way.png" alt="Claude回答提示'I seem to have lost my calculator and have run out of fingers. Could you use the math tool to add 23 and 19?'通过调用MCP加法工具" width="600"/> </div>npx wrangler kv namespace create OAUTH_KVwrangler.jsoncnpm run deploy就像你在“本地开发”部分所做的那样,运行MCP检查器:
npx @modelcontextprotocol/inspector@latest
然后在检查器中输入你的Worker的workers.dev URL(例如:worker-name.account-name.workers.dev/sse)作为MCP服务器的URL,点击“连接”。
你现在已经从远程MCP客户端连接到了你的MCP服务器。
更新Claude配置文件,使其指向你的workers.dev URL(例如:worker-name.account-name.workers.dev/sse),并重启Claude
{
"mcpServers": {
"math": {
"command": "npx",
"args": [
"mcp-remote",
"https://worker-name.account-name.workers.dev/sse"
]
}
}
}
如果遇到问题,重新启动Claude可能会有所帮助,或者尝试直接在命令行中连接到你的MCP服务器,使用以下命令:
npx mcp-remote http://localhost:8787/sse
在某些罕见情况下,清除添加到~/.mcp-auth的文件可能有帮助:
rm -rf ~/.mcp-auth