MCP服务器Bearer认证
基于 Cloudflare Workers 的 MCP 服务器实现,支持 OAuth 登录和承载令牌认证,允许像 Claude Desktop 和 MCP Inspector 这样的 MCP 客户端进行安全连接。
服务介绍
在 Cloudflare 上运行远程 MCP 服务器
让我们在 Cloudflare Workers 上搭建一个带有 OAuth 登录功能的远程 MCP 服务器!
本地开发
# clone the repository
git clone git@github.com:cloudflare/ai.git
# install dependencies
cd ai
npm install
# run locally
npx nx dev remote-mcp-server-bearer-auth
你应该能够在浏览器中打开 http://localhost:8787/
将 MCP 检查器连接到你的服务器
要探索新的 MCP API,你可以使用 MCP Inspector。
- 使用
npx @modelcontextprotocol/inspector启动它 - 在检查器内,将传输类型切换为
SSE并输入http://localhost:8787/sse作为要连接的 MCP 服务器的 URL。 - 添加一个承载令牌并点击“连接”
- 点击“列出工具”
- 运行 "getToken" 工具,该工具应返回你在检查器中设置的 Authorization 标头
将 Claude Desktop 连接到你的本地 MCP 服务器
"remote-example": {
"command": "npx",
"args": [
"mcp-remote",
"http://localhost:8787/sse",
"--header",
"Authorization: Bearer {token}"
]
}
部署到 Cloudflare
npm run deploy
从远程 MCP 客户端调用你新部署的远程 MCP 服务器
就像在上面的“本地开发”部分所做的那样,运行 MCP 检查器:
npx @modelcontextprotocol/inspector@latest
然后在检查器中输入 Worker 的 workers.dev URL(例如:worker-name.account-name.workers.dev/sse)作为要连接的 MCP 服务器的 URL,并点击“连接”。
现在你已经从远程 MCP 客户端连接到了你的 MCP 服务器。你可以像上面提到的那样传递一个承载令牌。
将 Claude Desktop 连接到你的远程 MCP 服务器
TODO: 我们需要支持向 mcp-remote 代理发送任意标头
调试
如果遇到任何问题,重新启动 Claude 或尝试通过以下命令直接在命令行上连接到你的 MCP 服务器可能会有所帮助。
npx mcp-remote http://localhost:8787/sse
在某些罕见的情况下,清除添加到 ~/.mcp-auth 中的文件可能有帮助。
rm -rf ~/.mcp-auth