jackspace/claudeskillz/cloudflare-agents

Trust tier 3
  • community
Source check pending

Checking that the SKILL.md file is still in the repository.

Guide to the Cloudflare Agents SDK for building stateful AI agents on Workers plus Durable Objects. Covers the Agent class, this.setState and this.sql state, WebSocket and streaming chat via AIChatAgent and useAgentChat, scheduling with this.schedule (delays, dates, cron), triggering Workflows, Browser Rendering, RAG with Vectorize and Workers AI embeddings, MCP servers with McpAgent, human-in-the-loop review, and routing through routeAgentRequest, getAgentByName, useAgent and AgentClient. Documents 15+ pitfalls: missing new_sqlite_classes, non-atomic migrations, Agent class not exported, binding name mismatches, global uniqueness surprises, state size limits, MCP transport confusion and bypassed authentication. Use when building AI agents, chat agents, MCP servers or multi-agent systems on Cloudflare, or debugging Agent binding, migration and state-sync errors.

Install

npx vskill@latest install jackspace/claudeskillz/cloudflare-agents

SKILL.md

Cloudflare Agents SDK

Status: Production Ready ✅ Last Updated: 2025-10-21 Dependencies: cloudflare-worker-base (recommended) Latest Versions: agents@latest, @modelcontextprotocol/sdk@latest Production Tested: Cloudflare's own MCP servers (https://github.com/cloudflare/mcp-server-cloudflare)


What is Cloudflare Agents?

The Cloudflare Agents SDK enables building AI-powered autonomous agents that run on Cloudflare Workers + Durable Objects. Agents can:

  • Communicate in real-time via WebSockets and Server-Sent Events
  • Persist state with built-in SQLite database (up to 1GB per agent)
  • Schedule tasks using delays, specific dates, or cron expressions
  • Run workflows by triggering asynchronous Cloudflare Workflows
  • Browse the web using Browser Rendering API + Puppeteer
  • Implement RAG with Vectorize vector database + Workers AI embeddings
  • Build MCP servers implementing the Model Context Protocol
  • Support human-in-the-loop patterns for review and approval
  • Scale to millions of independent agent instances globally

Each agent instance is a globally unique, stateful micro-server that can run for seconds, minutes, or hours.


Quick Start (10 Minutes)

1. Scaffold Project with Template
bash
npm create cloudflare@latest my-agent -- \
--template=cloudflare/agents-starter \
--ts \
--git \
--deploy false

What this creates:

  • Complete Agent project structure
  • TypeScript configuration
  • wrangler.jsonc with Durable Objects bindings
  • Example chat agent implementation
  • React client with useAgent hook
2. Or Add to Existing Worker
bash
cd my-existing-worker
npm install agents

Then create an Agent class:

typescript
// src/index.ts
import { Agent, AgentNamespace } from "agents";
export class MyAgent extends Agent {
async onRequest(request: Request): Promise<Response> {
return new Response("Hello from Agent!");
}
}
export default MyAgent;
3. Configure Durable Objects Binding

Create or update wrangler.jsonc:

jsonc
{
"$schema": "node_modules/wrangler/config-schema.json",
"name": "my-agent",
"main": "src/index.ts",
"compatibility_date": "2025-10-21",
"compatibility_flags": ["nodejs_compat"],
"durable_objects": {
"bindings": [
{
"name": "MyAgent", // MUST match class name
"class_name": "MyAgent" // MUST match exported class
}
]
},
"migrations": [
{
"tag": "v1",
"new_sqlite_classes": ["MyAgent"] // CRITICAL: Enables SQLite storage
}
]
}

CRITICAL Configuration Rules:

  • ✅ name and class_name MUST be identical
  • ✅ new_sqlite_classes MUST be in first migration (cannot add later)
  • ✅ Agent class MUST be exported (or binding will fail)
  • ✅ Migration tags CANNOT be reused (each migration needs unique tag)
4. Deploy
bash
npx wrangler@latest deploy

Your agent is now running at: https://my-agent.<subdomain>.workers.dev


Configuration Deep Dive

Complete wrangler.jsonc Example
jsonc
{
"$schema": "node_modules/wrangler/config-schema.json",
"name": "my-agent",
"main": "src/index.ts",
"account_id": "YOUR_ACCOUNT_ID",
"compatibility_date": "2025-10-21",
"compatibility_flags": ["nodejs_compat"],
// Durable Objects configuration (REQUIRED)
"durable_objects": {
"bindings": [
{
"name": "MyAgent",
"class_name": "MyAgent"
}
]
},
// Migrations (REQUIRED)
"migrations": [
{
"tag": "v1",
"new_sqlite_classes": ["MyAgent"] // Enables state persistence
}
],
// Optional: Workers AI binding (for AI model calls)
"ai": {
"binding": "AI"
},
// Optional: Vectorize binding (for RAG)
"vectorize": {
"bindings": [
{
"binding": "VECTORIZE",
"index_name": "my-agent-vectors"
}
]
},
// Optional: Browser Rendering binding (for web browsing)
"browser": {
"binding": "BROWSER"
},
// Optional: Workflows binding (for async workflows)
"workflows": [
{
"name": "MY_WORKFLOW",
"class_name": "MyWorkflow",
"script_name": "my-workflow-script" // If in different project
}
],
// Optional: D1 binding (for additional persistent data)
"d1_databases": [
{
"binding": "DB",
"database_name": "my-agent-db",
"database_id": "your-database-id"
}
],
// Optional: R2 binding (for file storage)
"r2_buckets": [
{
"binding": "BUCKET",
"bucket_name": "my-agent-files"
}
],
// Optional: Environment variables
"vars": {
"ENVIRONMENT": "production"
},
// Optional: Secrets (set with: wrangler secret put KEY)
// OPENAI_API_KEY, ANTHROPIC_API_KEY, etc.
// Observability
"observability": {
"enabled": true
}
}
Migrations Best Practices

Atomic Deployments: Migrations are atomic operations - they cannot be gradually deployed.

jsonc
{
"migrations": [
{
"tag": "v1",
"new_sqlite_classes": ["MyAgent"] // Initial: enable SQLite
},
{
"tag": "v2",
"renamed_classes": [
{"from": "MyAgent", "to": "MyRenamedAgent"}
]
},
{
"tag": "v3",
"deleted_classes": ["OldAgent"]
},
{
"tag": "v4",
"transferred_classes": [
{
"from": "AgentInOldScript",
"from_script": "old-worker",
"to": "AgentInNewScript"
}
]
}
]
}

Migration Rules:

  • ✅ Each migration needs a unique tag
  • ✅ Cannot enable SQLite on existing deployed class (must be in first migration)
  • ✅ Migrations apply in order during deployment
  • ✅ Cannot edit or remove previous migration tags
  • ❌ Never deploy new migrations gradually (atomic only)
Environment-Specific Migrations
jsonc
{
"migrations": [{"tag": "v1", "new_sqlite_classes": ["MyAgent"]}],
"env": {
"staging": {
"migrations": [
{"tag": "v1", "new_sqlite_classes": ["MyAgent"]},
{"tag": "v2-staging", "renamed_classes": [{"from": "MyAgent", "to": "StagingAgent"}]}
]
}
}
}

Agent Class API

The Agent class is the foundation of the Agents SDK. Extend it to create your agent.

Basic Agent Structure
typescript
import { Agent } from "agents";
interface Env {
// Environment variables and bindings
OPENAI_API_KEY: string;
AI: Ai;
VECTORIZE: Vectorize;
DB: D1Database;
}
interface State {
// Your agent's persistent state
counter: number;
messages: string[];
lastUpdated: Date | null;
}
export class MyAgent extends Agent<Env, State> {
// Optional: Set initial state (first time agent is created)
initialState: State = {
counter: 0,
messages: [],
lastUpdated: null
};
// Optional: Called when agent instance starts or wakes from hibernation
async onStart() {
console.log('Agent started:', this.name, 'State:', this.state);
}
// Handle HTTP requests
async onRequest(request: Request): Promise<Response> {
return Response.json({ message: "Hello from Agent", state: this.state });
}
// Handle WebSocket connections (optional)
async onConnect(connection: Connection, ctx: ConnectionContext) {
console.log('Client connected:', connection.id);
// Connections are automatically accepted
}
// Handle WebSocket messages (optional)
async onMessage(connection: Connection, message: WSMessage) {
if (typeof message === 'string') {
connection.send(`Echo: ${message}`);
}
}
// Handle WebSocket errors (optional)
async onError(connection: Connection, error: unknown): Promise<void> {
console.error('Connection error:', error);
}
// Handle WebSocket close (optional)
async onClose(connection: Connection, code: number, reason: string, wasClean: boolean): Promise<void> {
console.log('Connection closed:', code, reason);
}
// Called when state is updated from any source (optional)
onStateUpdate(state: State, source: "server" | Connection) {
console.log('State updated:', state, 'Source:', source);
}
// Custom methods (call from any handler)
async customMethod(data: any) {
this.setState({
...this.state,
counter: this.state.counter + 1,
lastUpdated: new Date()
});
}
}
Accessing Agent Properties

Within any Agent method:

typescript
export class MyAgent extends Agent<Env, State> {
async someMethod() {
// Access environment variables and bindings
const apiKey = this.env.OPENAI_API_KEY;
const ai = this.env.AI;
// Access current state (read-only)
const counter = this.state.counter;
// Update state (persisted automatically)
this.setState({ ...this.state, counter: counter + 1 });
// Access SQL database
const results = await this.sql`SELECT * FROM users`;
// Get agent instance name
const instanceName = this.name; // e.g., "user-123"
// Schedule tasks
await this.schedule(60, "runLater", { data: "example" });
// Call other methods
await this.customMethod({ foo: "bar" });
}
}

HTTP & Server-Sent Events

HTTP Request Handling
typescript
export class MyAgent extends Agent<Env> {
async onRequest(request: Request): Promise<Response> {
const url = new URL(request.url);
const method = request.method;
if (method === "POST" && url.pathname === "/increment") {
const counter = (this.state.counter || 0) + 1;
this.setState({ ...this.state, counter });
return Response.json({ counter });
}
if (method === "GET" && url.pathname === "/status") {
return Response.json({ state: this.state, name: this.name });
}
return new Response("Not Found", { status: 404 });
}
}
Server-Sent Events (SSE) Streaming
typescript
export class MyAgent extends Agent<Env> {
async onRequest(request: Request): Promise<Response> {
const stream = new ReadableStream({
async start(controller) {
const encoder = new TextEncoder();
// Send events to client
controller.enqueue(encoder.encode('data: {"message": "Starting"}\n\n'));
await new Promise(resolve => setTimeout(resolve, 1000));
controller.enqueue(encoder.encode('data: {"message": "Processing"}\n\n'));
await new Promise(resolve => setTimeout(resolve, 1000));
controller.enqueue(encoder.encode('data: {"message": "Complete"}\n\n'));
controller.close();
}
});
return new Response(stream, {
headers: {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive'
}
});
}
}

SSE vs WebSockets:

FeatureSSEWebSockets
DirectionServer → Client onlyBi-directional
ProtocolHTTPws:// or wss://
ReconnectionAutomaticManual
Binary DataLimitedFull support
Use CaseStreaming responses, notificationsChat, real-time collaboration

Recommendation: Use WebSockets for most agent applications (full duplex, better for long sessions).


WebSockets

Complete WebSocket Example
typescript
import { Agent, Connection, ConnectionContext, WSMessage } from "agents";
interface ChatState {
messages: Array<{ id: string; text: string; sender: string; timestamp: number }>;
participants: string[];
}
export class ChatAgent extends Agent<Env, ChatState> {
initialState: ChatState = {
messages: [],
participants: []
};
async onConnect(connection: Connection, ctx: ConnectionContext) {
// Access original HTTP request for auth
const authHeader = ctx.request.headers.get('Authorization');
const userId = ctx.request.headers.get('X-User-ID') || 'anonymous';
// Connections are automatically accepted
// Optionally close connection if unauthorized:
// if (!authHeader) {
// connection.close(401, "Unauthorized");
// return;
// }

This is the start of the file. Read the full SKILL.md on GitHub.

Security

  • Security scanPassed, 100 of 100Pattern checks on the skill's files.
  • Model reviewPassed

Quality evaluation

  • Task evaluationNot runNo evaluation yet. Run one in Skill Studio.

Source

Popularity

Repository stars
14
Repository forks
6
7-day trend score
14

Stars and forks are for the whole GitHub repository, not this skill alone. Refreshed 8 May 2026, 5 months ago.

Works with

Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.

  • Claude Code
  • Cursor
  • GitHub Copilot
  • Windsurf
  • Codex
  • Gemini CLI
  • Cline
  • Amp
  • Roo Code
  • Goose
  • Continue
  • Kilo Code
  • Junie
  • Trae
  • OpenHands
  • Qwen Code
  • AI Agent
  • Tabnine Chat
  • Sourcegraph Cody
  • Amazon Q
  • Firebase Genkit
  • Sweep
  • Devin
  • Phind
  • Mentat
  • Claude Desktop
  • Aider
  • GPT Pilot
  • Plandex
  • bolt.new
  • v0
  • Replit Agent
  • Anse
  • TypingMind
  • BetterChatGPT
  • LibreChat
  • Warp AI
  • Pieces
  • Fig AI
Embed badge
vskill VERIFIED badge
[![vskill VERIFIED](https://verified-skill.com/api/v1/skills/jackspace/claudeskillz/cloudflare-agents/badge)](https://verified-skill.com/skills/jackspace/claudeskillz/cloudflare-agents)

Something wrong with this skill or its page? Tell us.