AgentSkillsCN

cloudflare-code-mode

Cloudflare 代码模式,可将 MCP 工具转换为在 V8 隔离沙箱中运行的 TypeScript API。适用于使用 Cloudflare Agents SDK 构建 AI 代理、实施代码模式,或进行 MCP 至 TypeScript 的转换与沙箱化代码执行时的场景。

SKILL.md
--- frontmatter
name: cloudflare-code-mode
description: Cloudflare Code Mode pattern for converting MCP tools into TypeScript APIs executed in V8 isolate sandboxes. Use when building AI agents with Cloudflare Agents SDK, implementing Code Mode, or working with MCP-to-TypeScript conversion and sandboxed code execution.
references:
  - references/architecture.md
  - references/api.md
  - references/patterns.md
  - references/gotchas.md

Cloudflare Code Mode

Code Mode is an alternative MCP integration pattern: instead of LLMs directly calling MCP tools via special tokens, the system generates TypeScript APIs from MCP schemas and has LLMs write code that calls those APIs in a sandboxed V8 isolate.

Why Code Mode Exists

Traditional MCPCode Mode
LLM uses synthetic tool-call tokensLLM writes TypeScript (well-trained)
Each tool result feeds back through LLMCode runs in sandbox, returns output
Complex APIs must be "dumbed down"Full API complexity exposed as TS types
Token waste on multi-step tool chainsSingle code block, multiple API calls
API keys visible in tool-call contextKeys hidden behind bindings

Core insight: LLMs are far better at writing TypeScript (trained on real-world code) than using tool-call special tokens (trained on synthetic examples). Or as Cloudflare puts it: "Making an LLM perform tasks with tool calling is like putting Shakespeare through a month-long class in Mandarin and then asking him to write a play in it."

How It Works

code
1. Connect to MCP servers
2. Fetch MCP schemas → Generate TypeScript interfaces
3. Inject TS APIs + docs into LLM system prompt
4. LLM writes TypeScript code using `codemode.*` APIs
5. Create fresh V8 isolate (no internet, binding-only access)
6. Execute code → API calls route back to MCP servers
7. console.log() output returned to LLM

Quick Start

typescript
import { codemode } from "agents/codemode/ai";

const { system, tools } = codemode({
  system: "You are a helpful assistant",
  tools: {
    // existing tool definitions
  },
  // MCP server connections config
});

const stream = streamText({
  model: openai("gpt-5"),
  system,
  tools,
  messages,
});

Decision Tree

code
Building an AI agent on Cloudflare?
├─ Need MCP tool integration → Code Mode (this skill)
│   ├─ Setup/integration → See patterns.md
│   ├─ Understanding architecture → See architecture.md
│   ├─ API generation details → See api.md
│   └─ Security/limitations → See gotchas.md
├─ Need Workers basics → See Cloudflare Workers docs
└─ Need MCP basics → See modelcontextprotocol.io

Reading Order

TaskFiles to Read
Understand conceptThis file only
Implement Code Modeapi.md + patterns.md
Architect a systemarchitecture.md
Debug/troubleshootgotchas.md
Full implementationAll references

In This Reference

FilePurpose
architecture.mdSystem components, execution flow, sandbox model
api.mdTypeScript API generation, Worker Loader API, codemode API
patterns.mdImplementation examples, integration patterns
gotchas.mdSecurity constraints, limitations, debugging

Key Resources