Skip to main content
Glama

MCP Server with Cloudflare Workers

by sivakumarl

带有 Cloudflare Workers 的 MCP 服务器

介绍

模型上下文协议 (MCP) 是一项开放标准,支持 AI 代理和助手与服务进行交互。通过设置 MCP 服务器,您可以允许 AI 助手直接访问您的 API。

Cloudflare Workers 与workers-mcp包相结合,为构建 MCP 服务器提供了强大且可扩展的解决方案。

先决条件

在开始之前,请确保您已:

  • Cloudflare 帐户
  • Node.js 已安装
  • 已安装 Wrangler CLI( npm install -g wrangler

入门

步骤 1:创建新的 Cloudflare Worker

首先,初始化一个新的 Cloudflare Worker 项目:

npx create-cloudflare@latest my-mcp-worker cd my-mcp-worker

然后,验证您的 Cloudflare 帐户:

wrangler login

步骤2:配置Wrangler

使用正确的帐户详细信息更新您的wrangler.toml文件:

name = "my-mcp-worker" main = "src/index.ts" compatibility_date = "2025-03-03" account_id = "your-account-id"

安装 MCP 工具

要启用 MCP 支持,请安装workers-mcp包:

npm install workers-mcp

运行设置命令来配置 MCP:

npx workers-mcp setup

这将:

  • 添加必要的依赖项
  • 设置本地代理进行测试
  • 配置 Worker 以符合 MCP 要求

编写 MCP 服务器代码

更新您的src/index.ts以定义您的 MCP 服务器:

import { WorkerEntrypoint } from 'cloudflare:workers'; import { ProxyToSelf } from 'workers-mcp'; export default class MyWorker extends WorkerEntrypoint<Env> { /** * A friendly greeting from your MCP server. * @param name {string} The name of the user. * @return {string} A personalized greeting. */ sayHello(name: string) { return `Hello from an MCP Worker, ${name}!`; } /** * @ignore */ async fetch(request: Request): Promise<Response> { return new ProxyToSelf(this).fetch(request); } }

关键组件:

  • WorkerEntrypoint :管理传入的请求和方法暴露。
  • ProxyToSelf :确保符合 MCP 协议。
  • sayHello 方法:AI 助手可以调用的示例 MCP 函数。

添加 API 调用

您可以通过集成外部 API 来扩展您的 MCP 服务器。以下是获取天气数据的示例:

export default class WeatherWorker extends WorkerEntrypoint<Env> { /** * Fetch weather data for a given location. * @param location {string} The city or ZIP code. * @return {object} Weather details. */ async getWeather(location: string) { const response = await fetch(`https://api.weather.example/v1/${location}`); const data = await response.json(); return { temperature: data.temp, conditions: data.conditions, forecast: data.forecast }; } async fetch(request: Request): Promise<Response> { return new ProxyToSelf(this).fetch(request); } }

部署 MCP 服务器

设置好 Worker 后,将其部署到 Cloudflare:

npx wrangler deploy

部署后,您的 Worker 即可上线,并且 AI 助手可以发现并使用您的 MCP 工具。

要更新您的 MCP 服务器,请使用以下命令重新部署:

npm run deploy

测试 MCP 服务器

要在本地测试您的 MCP 设置:

npx workers-mcp proxy

此命令启动本地代理,允许 MCP 客户端(如 Claude Desktop)连接。


安全

为了保护您的 MCP 服务器,请使用 Wrangler Secrets:

npx wrangler secret put MCP_SECRET

这增加了一个共享秘密认证机制来防止未经授权的访问。


结论

恭喜!您已成功使用 Cloudflare Workers 构建并部署 MCP 服务器。现在您可以扩展其功能,并为 AI 助手提供新的工具。

有关更多详细信息,请查看Cloudflare MCP 文档


-
security - not tested
-
license - not tested
-
quality - not tested

remote-capable server

The server can be hosted and run remotely because it primarily relies on remote services or has no dependency on the local environment.

一种开放标准服务器实现,使 AI 助手能够通过模型上下文协议直接访问 API 和服务,使用 Cloudflare Workers 构建以实现可扩展性。

  1. 介绍
    1. 先决条件
      1. 入门
        1. 步骤 1:创建新的 Cloudflare Worker
        2. 步骤2:配置Wrangler
      2. 安装 MCP 工具
        1. 编写 MCP 服务器代码
          1. 关键组件:
        2. 添加 API 调用
          1. 部署 MCP 服务器
            1. 测试 MCP 服务器
              1. 安全
                1. 结论

                  Related MCP Servers

                  • -
                    security
                    F
                    license
                    -
                    quality
                    A Model Context Protocol server that runs on Cloudflare Workers with OAuth login, allowing AI assistants like Claude to execute tools remotely through HTTP connections.
                    Last updated -
                    TypeScript
                  • -
                    security
                    F
                    license
                    -
                    quality
                    A Cloudflare Workers-based implementation of Model Context Protocol (MCP) server that enables AI assistants like Claude to access external tools and capabilities through a standardized interface with OAuth authentication.
                    Last updated -
                    14
                    TypeScript
                  • -
                    security
                    F
                    license
                    -
                    quality
                    A Model Context Protocol server implementation that runs on Cloudflare Workers, providing tool integration for AI assistants like Claude with OAuth login capability.
                    Last updated -
                    14
                    TypeScript
                  • -
                    security
                    F
                    license
                    -
                    quality
                    A Cloudflare Workers-based server implementing the Model Context Protocol that enables AI assistants like Claude to securely access external tools through OAuth authentication.
                    Last updated -
                    14
                    TypeScript

                  View all related MCP servers

                  MCP directory API

                  We provide all the information about MCP servers via our MCP API.

                  curl -X GET 'https://glama.ai/api/mcp/v1/servers/sivakumarl/my-mcp-worker'

                  If you have feedback or need assistance with the MCP directory API, please join our Discord server