跳转到内容
本站为社区维护的非官方中文翻译,与 Cloudflare, Inc. 无关联。本站不提供账户登录,请勿在任何非官方站点输入您的 Cloudflare 账户信息。官方文档:developers.cloudflare.com
所有 Agent
Visual Studio Code iconVisual Studio Code icon

Visual Studio Code + Cloudflare

Microsoft

Microsoft 推出的免费开源代码编辑器,原生支持 Model Context Protocol(MCP)客户端并集成 Copilot Chat。

IDE终端独立运行扩展开源

快速入门

  1. 安装 Visual Studio Code 和 GitHub Copilot

    安装 Visual Studio Code ↗ 并确保其已完全更新。然后安装 GitHub Copilot 扩展 ↗,并使用 GitHub 账户登录。Copilot 免费层级足以试用此功能。

  2. 配置 Cloudflare MCP 服务器

    在工作区文件夹内创建 .vscode/mcp.json:

    {
      "servers": {
        "cloudflare-api": {
          "type": "http",
          "url": "https://mcp.cloudflare.com/mcp"
        }
      }
    }

    Visual Studio Code 使用 servers 作为根键。从 Cursor 或 Claude Desktop 复制的配置使用 mcpServers,在此处不会有任何效果。有关针对特定领域的 MCP 服务器,请参阅 mcp-server-cloudflare ↗。有关覆盖完整 Cloudflare API 的 MCP 服务器(Code Mode),请参阅 cloudflare/mcp ↗。

  3. 启动服务器并授权

    保存 mcp.json 后,Visual Studio Code 会在 cloudflare-api 服务器定义上显示 Start(启动) CodeLens。选择它并完成 OAuth 流程,以针对你的账户授权 Cloudflare MCP 服务器。在熟悉你授予的范围之前,请先从演示账户开始。

  4. 尝试提示词

    打开 Copilot Chat(Ctrl+Shift+I / Cmd+Shift+I),切换到 Agent 模式,然后尝试提示词 — 例如:

    使用 Cloudflare Agents SDK 构建一个 AI 聊天 Agent,并将持久对话历史存储在 D1 中。

如需逐张截图的演练 — 包括 OAuth 授权页面、确认服务器正在运行,以及使用自然语言创建和删除 DNS 记录 — 请参阅详细演练。

Cloudflare 平台访问权限

展开任意部分以了解更多。

Cloudflare Skills

持久的平台上下文,教会 Agent Cloudflare 如何工作。

Skills 是 Agent 按需加载的说明。 cloudflare/skills 包覆盖平台的每一层 — 因此 Agent 了解你的约定,无需你反复解释。

  • agents-sdkBuild, debug, or review Cloudflare Agents SDK applications using the agents package.
  • basinBuild and troubleshoot Cloudflare Basin analytics workflows with Basin Pipelines, Basin Catalog, and Basin SQL. Use for streaming data into R2 Iceberg tables, managing catalogs, or querying those tables; also use for requests using the former Data Platform, Pipelines, R2 Data Catalog, or R2 SQL names.
  • cf-cliCloudflare's official cf CLI (beta) for the public Cloudflare API and Workers projects. Use for finding and running Cloudflare commands from the terminal — sign-in and profiles, cf cli search, cf schema, cf deploy/dev/build, resource commands for DNS, zones, D1, KV, and R2 — and for choosing between cf and Wrangler in a project.
  • cloudflareDiscover and choose Cloudflare products for apps, APIs, AI agents, storage, networking, and security. Use for architecture and product selection, including when the user describes a need without naming a Cloudflare product; then find the relevant skill or documentation. Also use when a Cloudflare-proxied site shows a Cloudflare error page, a `52x` status, a 1xxx error code, or a Cloudflare `403`, to decide whether to fix Cloudflare configuration or the origin.
  • cloudflare-app-bootstrapGet a Cloudflare Workers app to a working local request by connecting its bindings, secrets, and initial data, then verifying real application behavior. Use for app setup or onboarding after the project exists; use frameworks-on-cloudflare or cf-cli to create or configure the project, and wrangler or cf-cli for isolated commands.
  • cloudflare-email-serviceImplement or troubleshoot Cloudflare Email Sending and Email Routing integrations and their delivery configuration.
  • cloudflare-oneDesign, configure, troubleshoot, or review Cloudflare One Zero Trust and SASE deployments. Use cloudflare-one-migrations for migration planning from other vendors. Also use when an agent, script, crawler, or other non-browser client needs to reach a webpage or API protected by Cloudflare Access.
  • cloudflare-one-migrationsAssess and plan migrations from existing VPN, SWG, or SASE platforms to Cloudflare One, including policy mapping, parity gaps, and rollout.
  • durable-objectsBuild, debug, or review Cloudflare Durable Objects code for persistent state and coordination.
  • frameworks-on-cloudflareBuild, migrate, and deploy web-framework applications on Cloudflare Workers. Use when a framework project such as Astro, SvelteKit, React Router, TanStack Start, Nuxt, Hono, or a React or Vue SPA targets Cloudflare, or when a framework repository contains Workers deployment configuration.
  • gateway-workersBuild, debug, or review Cloudflare Workers invoked by the Forward action in Gateway HTTP policies, including the GatewayMiddleware entrypoint, Gateway types, context, and continuation.
  • k2Build and troubleshoot Cloudflare K2 or K2 Streams durable logs. Use for stream setup, producing from Workers or HTTP, configuring retention and inputs, and consuming through subscriptions.
  • nextjs-on-cloudflareBuild, migrate, and deploy Next.js apps on Cloudflare Workers with vinext. Use when starting a Next.js project on Cloudflare, moving an existing app to Workers, choosing between vinext and OpenNext, or setting up vinext for Workers. For setup, migration, or deployment, install vinext's upstream skills with `npx skills add cloudflare/vinext` if missing, then read and follow the applicable skill and docs.
  • sandbox-migrate-to-nextMigrate Cloudflare Sandbox apps from stable @cloudflare/sandbox to @cloudflare/sandbox@next (SDK 1.0 preview). Use sandbox-next for apps already on the preview.
  • sandbox-nextBuild or maintain Cloudflare Sandbox apps on @cloudflare/sandbox@next (SDK 1.0 preview). Use sandbox-migrate-to-next when porting a stable app.
  • sandbox-stableBuild or maintain Cloudflare Sandbox apps on the stable @cloudflare/sandbox package. Use sandbox-next for preview apps and sandbox-migrate-to-next for stable-to-preview migrations.
  • turnstile-spinSet up, repair, or migrate to Cloudflare Turnstile bot verification in an existing frontend and backend, including server-side Siteverify.
  • web-perfAudit, diagnose, or optimize website loading and interaction performance, Core Web Vitals, and Lighthouse performance scores.
  • workers-best-practicesCloudflare Workers best practices for production applications. Use when writing, reviewing, or configuring Workers.
  • workers-developmentRun, inspect, and debug Cloudflare Workers during local development with cf dev, wrangler dev, or the Cloudflare Vite plugin, using Local Explorer to read and seed binding data and query local traces and logs. Also routes production debugging to Workers observability. Use for debugging requests whether or not the project uses Wrangler.
  • workers-profilingProfile or debug CPU usage and memory allocation in deployed Cloudflare Workers and Durable Objects. Then use this information to optimize your code.
  • wranglerRun or troubleshoot Wrangler CLI commands in projects that have a wrangler.jsonc, wrangler.json, or wrangler.toml file, including local development, Previews, deployment, and resource management. For new projects and projects without a Wrangler configuration file, use the cf CLI instead.
MCP 服务器

实时访问 Cloudflare API、文档和可观测性。

MCP 服务器提供类型化工具,以便在运行时调用 Cloudflare。有两种选择:Code Mode — 一台覆盖整个 Cloudflare API 的服务器(约 1,000 个 token 覆盖 2,500+ 个端点)— 或者一组聚焦特定领域的服务器,托管在 cloudflare/mcp-server-cloudflare 仓库中。完整目录也在 Cloudflare 的 MCP 服务器 文档中。

  • Cloudflare API MCP ServerCode ModeBest when you want broad access across Cloudflare's APIs through code executionhttps://mcp.cloudflare.com/mcp
  • Code mode APICode ModeBroad access to the full Cloudflare API via code execution, with minimal token overheadhttps://mcp.cloudflare.com/mcp
  • Browser Run serverFetch web pages, convert them to markdown and take screenshotshttps://browser.mcp.cloudflare.com/mcp
  • Cloudflare Blog serverSearch and read posts from the Cloudflare Bloghttps://blog.mcp.cloudflare.com/mcp
  • Cloudflare One CASB serverQuickly identify any security misconfigurations for SaaS applications to safeguard users & datahttps://casb.mcp.cloudflare.com/mcp
  • Container serverSpin up a sandbox development environmenthttps://containers.mcp.cloudflare.com/mcp
  • Demo Day serverDemonstrate a minimal Cloudflare MCP serverhttps://demo-day.mcp.cloudflare.com/mcp
  • Digital Experience Monitoring serverGet quick insight on critical applications for your organizationhttps://dex.mcp.cloudflare.com/mcp
  • Documentation serverGet up-to-date reference information on Cloudflarehttps://docs.mcp.cloudflare.com/mcp
Wrangler CLI

本地开发、部署和 Workers 专用命令。

使用 Wrangler 进行本地开发、部署以及运行产品专用命令,例如 wrangler d1 migrations apply 或 wrangler tail。 捆绑的 wrangler Skill 会教 Agent 何时使用它。

面向 Agent 的文档

为 Agent 优化的、节省 token 的参考资料。

在任意 Cloudflare 文档 URL 后追加 /index.md 即可获得干净的 Markdown 版本。每个顶级产品部分也有自己的 llms.txt — 一份适合单个上下文窗口的页面索引。常用文件包括:

关于这些文档如何面向 Agent 组织的完整概览,请参阅面向 Agent 的文档指南。

示例提示词

技巧与建议

  • Cloudflare API MCP 服务器使用 Code Mode — Agent 针对类型化的 API 编写 JavaScript,在约 1,000 个 token 内访问 2,500+ 个端点中的任何一个。这就是你只会看到三个已注册工具、而不是成千上万个的原因。
  • Visual Studio Code 的 MCP 配置使用 servers 作为根键。从其他 Agent(Cursor、Claude Desktop)复制的配置使用 mcpServers,在 Visual Studio Code 中无法工作。
  • 将 OAuth 授权范围限定到单个账户,并在授予 Full access(完全访问) 之前,先从 Read only(只读) 或 Custom(自定义) 权限集开始。
  • 随时可在 Cloudflare 仪表板的 My Profile → Access Management → Connected Applications(我的个人资料 → 访问管理 → 已连接的应用程序) 下撤销授权。

常见问题 (FAQ)

为什么 MCP 服务器只显示三个工具?

Cloudflare API 规范大约有两百万个 token,远远 超出直接交给 Agent 的合理规模。Cloudflare MCP 服务器使用 代码执行模式:Agent 获得一个用于搜索 API 规范的工具和一个 用于执行调用的工具,规范本身留在服务器端。三个工具 才是正确、健康的状态。

这是否只能在 Visual Studio Code 中使用?

不是。同一个服务器 URL(https://mcp.cloudflare.com/mcp)可用于 Claude Code、Cursor、Claude Desktop 或任何支持 MCP 的客户端。Visual Studio Code 只是众多选项之一。

我可以使用 Cloudflare API 令牌代替 OAuth 吗?

可以。你可以将 Cloudflare API 令牌作为 bearer 凭据传递,这适合 CI/CD 或共享自动化。对于交互式编辑器使用,建议走 OAuth。不支持使用 Client IP Address Filtering 的 API 令牌。

在 Visual Studio Code 中使用 MCP 需要 GitHub Copilot 吗?

本指南使用 Copilot Chat 作为 MCP 客户端,因此是的 — 你需要 Copilot (或其他支持 MCP 的扩展)才能实际驱动该服务器。Visual Studio Code 本身提供 MCP 配置和生命周期,但 聊天 UI 来自 Copilot。

故障排查

聊天抛出 'Cannot read properties of undefined'

Visual Studio Code 版本与 Copilot 扩展不同步。 从标题栏中的蓝色按钮更新 Visual Studio Code,让它 重启,然后重试。

写入因 API error 10000 失败,但读取正常

即使你本意是授予完全访问,OAuth 授权仍是只读的。 如果你授权了不止一次,就可能发生这种情况 — 后续流程可能 会以缩小后的范围静默通过。在 Cloudflare 仪表板的 My Profile → Access Management → Connected Applications(我的个人资料 → 访问管理 → 已连接的应用程序) 下撤销该应用程序,在 Visual Studio Code 中退出 MCP 会话(活动栏底部的 账户图标),然后重新启动服务器。

mcp.json 中没有出现 Start 按钮

检查 JSON 中的根键。Visual Studio Code 需要 servers,而不是 mcpServers。同时确认文件位于已打开文件夹内的 .vscode/mcp.json, 而不是文件夹根目录。

Agent 建议创建 API 令牌来绕过错误

请拒绝。如果身份验证已损坏,修复方式是 OAuth 授权,而不是 把 bearer token 粘贴到聊天窗口。按照 API error 10000 的故障排查步骤干净地重新授权。

Visual Studio Code 日志中出现 401 错误

在 Copilot Chat 授权提示中选择 Allow in this Session(允许在此会话中) 之前, 这是预期行为。

在 Cloudflare 上构建 Agent

另外值得了解

Cloudflare 不只是 Agent 的部署目标,也是用来构建你自己 Agent 的完整技术栈。

其他 Agent

这篇文档对您有帮助吗?