mcpField
B
Building a Remote MCP Server on Cloudflare (Without Auth)

Building a Remote MCP Server on Cloudflare (Without Auth)

本地服务
github.com/jingwei950开发工具077 周前更新

关于 Building a Remote MCP Server on Cloudflare (Without Auth)

该示例展示如何在 Cloudflare Workers 上部署无需认证的远程 MCP 服务,面向希望创建和定制服务、管理密钥并连接 Cloudflare AI Playground 或 Claude Desktop 等 MCP 客户端的开发者。

主要功能

  1. 01部署无需认证的远程服务
  2. 02从 Cloudflare 模板创建项目
  3. 03使用 TypeScript 添加自定义工具
  4. 04管理生产环境和本地密钥
  5. 05连接 Cloudflare AI Playground 或 Claude Desktop

适用场景

  1. 01在 Cloudflare Workers 上发布 MCP 服务
  2. 02基于模板搭建服务原型
  3. 03添加使用受保护密钥的工具
  4. 04将已部署服务连接到 MCP 客户端

MCP 相关

这是一个部署在 Cloudflare Worker 上、无需认证的远程 MCP 服务示例。可向 MCP 客户端提供自定义工具,包括 Cloudflare AI Playground 和 Claude Desktop。

配置

本地服务的配置在各客户端中通用,复制后粘贴到客户端的 MCP 配置文件即可。

mcp.json · 通用配置
{
"mcpServers": {
"enterprise-clickup": {
"command": "npx",
"args": [
"-y",
"wrangler"
]
}
}
}
启动命令npx -y wrangler

本地服务需要在你的电脑上安装运行环境(如 Node.js 18+)。各客户端的配置文件位置不同,请参考客户端文档。

安装包
npmwrangler

工具

还没有获取到工具列表

本地服务需要在你的电脑上运行后才能读取工具,维护者暂未提供工具列表。

详细介绍

在 Cloudflare 上构建远程 MCP 服务(无需身份验证)

此示例可让你在 Cloudflare Workers 上部署无需身份验证的远程 MCP 服务。

开始使用:

部署到 Workers

这会将你的 MCP 服务部署到类似这样的 URL:remote-mcp-server-authless.<your-account>.workers.dev/sse

或者,你也可以使用下面的命令行,在本地计算机上创建远程 MCP 服务:

npm create cloudflare@latest -- my-mcp-server --template=cloudflare/ai/demos/remote-mcp-authless

部署到你自己的 Cloudflare Worker

你可以克隆此仓库并手动部署到你的 Cloudflare 账户,而不使用一键部署按钮。

  1. 克隆仓库:
    git clone <repository-url>
    cd <repository-directory>
    
  2. 安装依赖项:
    npm install
    
  3. 登录 Cloudflare:
    npx wrangler login
    
  4. 部署:
    npx wrangler deploy
    
    此命令会构建并部署你的 Worker。Wrangler 会提供 Worker 的部署 URL(例如,my-mcp-server.<your-account>.workers.dev/sse)。

管理密钥(API 密钥)

如果自定义工具需要 API 密钥或其他密钥,你需要安全地进行配置。

生产环境密钥(Cloudflare 控制面板/Wrangler)

对于已部署的 Worker,请使用 Cloudflare 的密钥管理功能:

  1. 使用 Wrangler:

    npx wrangler secret put YOUR_SECRET_NAME
    

    Wrangler 会提示你输入密钥值。该值会经过加密并安全存储,只有你的 Worker 代码可以访问。

  2. 使用 Cloudflare 控制面板: 在 Cloudflare 控制面板中,前往你的 Worker -> Settings -> Variables -> Environment Variables -> Add Variable。请务必点击“Encrypt”以安全存储密钥。

本地开发密钥(.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 文件中,你可以通过传递给 fetch 处理程序的 env 对象访问这些密钥;如果在初始化时传入 env 对象,也可以在 init() 方法中访问:

// Example in src/index.ts
export default {
  async fetch(
    request: Request,
    env: Env,
    ctx: ExecutionContext
  ): Promise<Response> {
    // 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<Response> {
    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 服务添加自己的工具,请在 src/index.ts 的 init() 方法中使用 this.server.tool(...) 定义每个工具。

连接到 Cloudflare AI Playground

你可以从 Cloudflare AI Playground 连接到 MCP 服务。它是一个远程 MCP 客户端:

  1. 前往 https://playground.ai.cloudflare.com/
  2. 输入已部署的 MCP 服务 URL(remote-mcp-server-authless.<your-account>.workers.dev/sse)
  3. 现在,你可以直接在 playground 中使用 MCP 工具了!

将 Claude Desktop 连接到你的 MCP 服务

你也可以通过使用 mcp-remote 代理,从本地 MCP 客户端连接到远程 MCP 服务。

要从 Claude Desktop 连接到 MCP 服务,请按照 Anthropic 的快速入门指南操作,并在 Claude Desktop 中前往 Settings > Developer > Edit Config。

使用以下配置进行更新:

{
  "mcpServers": {
    "calculator": {
      "command": "npx",
      "args": [
        "mcp-remote",
        "http://localhost:8787/sse" // or remote-mcp-server-authless.your-account.workers.dev/sse
      ]
    }
  }
}

重启 Claude 后,你应该就能看到可用的工具了。

评论

支持 Markdown

第一次接触 MCP?

从协议是什么,到在客户端里装上第一个服务,这几条回答覆盖了大多数入门问题。

一个开放协议,规定了 AI 应用如何以统一方式连接外部工具与数据源。客户端负责与模型对话,MCP 服务负责提供工具、资源和提示模板,两者通过标准消息通信,同一个服务可以被不同客户端复用。