在Cloudflare上构建远程MCP服务器(无需身份验证)
此示例允许您部署一个不需要Cloudflare Workers身份验证的远程MCP服务器。
开始:

这将把您的MCP服务器部署到以下URL: remote-mcp-server-authless..workers.dev/sse
或者,您可以使用下面的命令行在本地计算机上创建远程MCP服务器:
npm create cloudflare@latest -- my-mcp-server --template=cloudflare/ai/demos/remote-mcp-authless部署到您自己的Cloudflare Worker
您可以克隆此存储库并将其手动部署到您的Cloudflare帐户,而不是使用一键部署按钮。
- 克隆存储库:
git clone
cd - 安装依赖项:
npm install- 登录Cloudflare:
npx wrangler login- 部署:
npx wrangler deploy此命令将构建和部署您的worker。Wrangler将为您提供部署工作人员的URL(例如。, my-mcp-server..workers.dev/sse).
管理机密(API密钥)
如果您的自定义工具需要API密钥或其他机密,则需要安全地配置它们。
生产秘密(Cloudflare Dashboard/牧马人)
对于您部署的员工,请使用Cloudflare的秘密管理:
- 使用牧马人:
npx wrangler secret put YOUR_SECRET_NAME牧马人会提示您输入秘密值。此值将被加密并安全存储,只能由您的工作代码访问。
- 使用Cloudflare仪表板:
转到Cloudflare仪表板中的Worker->设置->变量->环境变量->添加变量。确保点击“加密”以安全存储。
地方发展秘密(.dev.vars)
用于本地测试,使用 npx wrangler dev,创建一个名为的文件 .dev.vars 在项目目录的根目录中。 不要将此文件提交到Git。 将您的机密添加到此文件中,如下所示:
# .dev.vars - For local development only!
YOUR_SECRET_NAME="your_secret_value_here"
ANOTHER_SECRET="another_value"访问代码中的秘密
在你的 src/index.ts 或者其他TypeScript文件,你可以通过 env 对象传递给 fetch 处理程序或内部 init() 方法如果你通过 env 初始化过程中的对象:
// Example in src/index.ts
export default {
async fetch(
request: Request,
env: Env,
ctx: ExecutionContext
): Promise {
// Access secrets like this:
const apiKey = env.YOUR_SECRET_NAME;
// ... rest of your code
},
};
// Example structure if you need env in init()
class MyMCPImplementation {
server: MCP;
env: Env; // Store env if needed
constructor(env: Env) {
this.env = env;
this.server = new MCP();
this.init();
}
init() {
// Access secrets here:
const apiKey = this.env.YOUR_SECRET_NAME;
// Define tools using the secrets if necessary
this.server.tool(/* ... */);
}
// ... rest of the class
}
export default {
async fetch(
request: Request,
env: Env,
ctx: ExecutionContext
): Promise {
const mcpImplementation = new MyMCPImplementation(env);
return mcpImplementation.server.handle(request);
},
};
// Define the Env interface (if not already defined)
interface Env {
YOUR_SECRET_NAME: string;
ANOTHER_SECRET: string;
// Add other secrets/bindings here
}记得在 Env 接口,通常在以下位置找到或需要 src/index.ts.
自定义您的MCP服务器
添加您自己的 工具 在MCP服务器中,定义 init() 方法 src/index.ts 使用 this.server.tool(...).
连接到Cloudflare AI游乐场
您可以从Cloudflare AI Playground连接到您的MCP服务器,这是一个远程MCP客户端:
- 首选https://playground.ai.cloudflare.com/
- 输入您部署的MCP服务器URL(
remote-mcp-server-authless..workers.dev/sse) - 现在,您可以直接在操场上使用MCP工具!
将Claude Desktop连接到您的MCP服务器
您还可以使用以下命令从本地MCP客户端连接到远程MCP服务器 mcp远程代理.
要从Claude Desktop连接到MCP服务器,请按照以下步骤操作 Anthropic的快速入门 在Claude Desktop中,转到“设置”>“开发人员”>“编辑配置”。
使用此配置进行更新:
{
"mcpServers": {
"calculator": {
"command": "npx",
"args": [
"mcp-remote",
"http://localhost:8787/sse" // or remote-mcp-server-authless.your-account.workers.dev/sse
]
}
}
}重新启动Claude,您应该会看到工具可用。
