cloudflare-codemode
Build and run sandboxed agent tools using Cloudflare Code Mode (@cloudflare/codemode). Connects dynamic Worker sandboxes, Durable Objects, OpenAPI, and MCP…
by siddu1004★ 43 GitHub stars
Install cloudflare-codemode in Claude Code & Claude Desktop
Sign up to install this skill
Create a free account to reveal the install command and save the skill to your library.
- Reveal the one-line install command
- Save skills to your synced library
- Get notified when skills update
About this skill
Cloudflare Code Mode
Build, configure, and operate sandboxed tool execution for AI agents using Cloudflare Code Mode (@cloudflare/codemode). Code Mode enables agents to execute dynamic JavaScript/TypeScript inside isolated Worker or browser sandboxes, calling typed host connectors without leaking secrets or direct host authority.
Use This Skill When
- Building or configuring sandboxed execution environments for AI agents using
@cloudflare/codemode. - Exposing OpenAPI endpoints or MCP servers to agents as safe, typed sandboxed globals (
McpConnector,OpenApiConnector). - Implementing stateful, durable agent runtimes backed by Cloudflare Durable Objects (
createCodemodeRuntime). - Integrating agent code execution with Vercel AI SDK (
@cloudflare/codemode/ai), TanStack AI (@cloudflare/codemode/tanstack-ai), or MCP tools.
Architecture & Core Concepts
- Host Control Plane (
createCodemodeRuntime): Runs inside a Cloudflare Durable Object to manage execution states, audit logs, and runtime facets. - Connectors (
CodemodeConnector): Bridge host-side capabilities into sandbox globals. Built-in connectors includeMcpConnector,OpenApiConnector, andToolSetConnector. - Executors (
Executor): Isolated execution engines:DynamicWorkerExecutor: Ephemeral V8/Worker sandbox running generated code with strict CPU/memory limits.IframeSandboxExecutor: Browser-side sandboxed iframe for client-side tool execution.
Step-by-Step Implementation
1. Install Dependencies
npm install @cloudflare/codemode
# For AI SDK or framework adapters:
npm install @cloudflare/codemode/ai @ai-sdk/openai ai
2. Configure Runtime in a Durable Object
import { DurableObject } from "cloudflare:workers";
import { createCodemodeRuntime, DynamicWorkerExecutor, McpConnector } from "@cloudflare/codemode";
export class AgentRuntimeDO extends DurableObject {
private runtime;
constructor(ctx: DurableObjectState, env: Env) {
super(ctx, env);
this.runtime = createCodemodeRuntime({
ctx,
name: "agent-session",
maxExecutions: 50,
connectors: [
new McpConnector({
name: "tools",
client: mcpClientInstance,
}),
],
executor: new DynamicWorkerExecutor({
env,
limits: { cpuMs: 5000, subrequests: 10 },
}),
transformResult: (result) => {
// Redact internal pointers before returning to LLM
return typeof result === "object" ? { ...result, _internal: undefined } : result;
},
});
}
async executeCode(code: string) {
return await this.runtime.run({ code });
}
}
3. Expose as an AI SDK Code Tool
import { createCodeTool } from "@cloudflare/codemode/ai";
export const sandboxTool = createCodeTool({
runtime: runtimeHandle,
name: "run_sandboxed_code",
description: "Execute sandboxed JavaScript code with access to configured connectors (e.g. tools.*).",
});
Verification & Checks
- Ensure connector names are unique and do not collide with reserved name
codemode. - Verify
DynamicWorkerExecutorlimits (CPU milliseconds and subrequest quotas) match workload boundaries. - Confirm sensitive environment secrets are passed via scoped connectors, never exposed as raw global environment variables in the sandbox.
- Audit execution logs via
runtime.getExecutions()to verify non-leaking error traces.
Common Pitfalls
- State Leakage: Sandboxes are ephemeral per execution; do not rely on in-memory global state across runs. Use Durable Object storage.
- Reserved Identifier: Never name a connector
codemode. - Unhandled Async Promises: Ensure all connector calls within sandbox code are awaited before the execution script completes.
Sources
Allowed tools
Tools this skill is permitted to call.
No restriction — this skill can use any tool.
Bundled files
- LICENSE
FAQ
What does the cloudflare-codemode skill do?
Build and run sandboxed agent tools using Cloudflare Code Mode (@cloudflare/codemode). Connects dynamic Worker sandboxes, Durable Objects, OpenAPI, and MCP servers safely. Use when creating agent sandboxes, building code tools, or integrating Cloudflare Workers with agents.
How do I install the cloudflare-codemode skill?
Copy the skill folder into ~/.claude/skills (the Claude Code tab above does this in one command), or install it as a plugin.
Does the cloudflare-codemode skill run scripts?
No, this skill is instructions only (SKILL.md) with no executable scripts.
Related skills
mcp-builder
AnthropicGuide for creating high-quality MCP (Model Context Protocol) servers that enable LLMs to interact with external services through well-designed tools. Use when…
claude-api
AnthropicReference for the Claude API / Anthropic SDK — model ids, pricing, params, streaming, tool use, MCP, agents, caching, token counting, model migration. TRIGGER…
algorithmic-art
AnthropicCreating algorithmic art using p5.js with seeded randomness and interactive parameter exploration. Use this when users request creating art using code…
mcp-integration
AnthropicThis skill should be used when the user asks to "add MCP server", "integrate MCP", "configure MCP in plugin", "use .mcp.json", "set up Model Context…