agents-sdk · openai — Codex skill: what it does, how to install
Guides building AI agents on Cloudflare Workers with persistent state, durable workflows, and MCP support. Useful when developing server-side agents or chat apps.
npx skills add openai/plugins --skill agents-sdk -a codexWhat agents-sdk does
This skill guides the model on writing code for the Cloudflare Agents SDK. It instructs the assistant to pull fresh docs from the official GitHub repository rather than relying on pre-trained knowledge. It covers the base Agent class, state validation with validateStateChange, SQLite persistence, and routing through routeAgentRequest.
Inside, you will find patterns for @callable() RPC methods over WebSockets, background processing via AgentWorkflow, and building MCP servers with McpAgent. It also includes client-side examples using the useAgent hook in React.
Running this code requires the agents package from npm and Durable Objects SQLite migrations in your Wrangler config. Install this skill if you develop agents on Cloudflare Workers. Outside that platform, it offers little value.
What it helps with
- set up SQLite-backed persistent state on Cloudflare Workers
- expose RPC methods over WebSockets using the
@callable()decorator - run durable multi-step background tasks via
AgentWorkflow - connect a React client to an agent using the
useAgenthook - build custom MCP servers using the
McpAgentclass
Example prompts for the agent
- Create a Cloudflare Workers agent with SQLite state and a @callable increment method
- Set up a background workflow using AgentWorkflow
- Show how to connect a React component to a Counter agent instance with useAgent
How to install agents-sdk
1Skills CLI (Vercel)
npx skills add openai/plugins --skill agents-sdk -a codex2Manually: put the files in place
.agents/skills/agents-sdk/SKILL.mdFor all projects use ~/.agents/skills/
1Skills CLI (Vercel)
npx skills add openai/plugins --skill agents-sdk -a claude-code2Manually: put the files in place
~/.claude/skills/agents-sdk/SKILL.mdCopy the whole skill folder; for one project use .claude/skills/agents-sdk/
Manually: put the files in place
.cursor/skills/agents-sdk/SKILL.mdCursor also reads .agents/skills/ and .claude/skills/
1GitHub CLI
gh skill install openai/plugins agents-sdk2Manually: put the files in place
.github/skills/agents-sdk/SKILL.mdPlatform CLI
gemini skills install https://github.com/openai/plugins --path plugins/cloudflare/skills/agents-sdkManually: put the files in place
.devin/skills/agents-sdk/SKILL.mdThe legacy .windsurf/skills/ path still works
Claude.ai
Zip the folder and upload it in Settings → Capabilities → Skills
From the same set201
The whole set →Other versions of agents-sdk
Similar
Questions about agents-sdk
How do I install agents-sdk in Codex?
Run npx skills add openai/plugins --skill agents-sdk in your terminal. This creates .agents/skills/agents-sdk/SKILL.md in your project.
What does agents-sdk do?
It helps the assistant generate accurate Cloudflare Agents SDK code, including Durable Objects, SQLite persistence, and MCP integrations.
What dependencies does this SDK require?
Install the package with npm install agents. You also need Durable Objects bindings and SQLite migrations configured in your Wrangler file.
Source text
SKILL.md · Apache-2.0
---
name: agents-sdk
description: Build AI agents on Cloudflare Workers using the Agents SDK. Load when creating stateful agents, durable workflows, real-time WebSocket apps, scheduled tasks, MCP servers, or chat applications. Covers Agent class, state management, callable RPC, Workflows integration, and React hooks. Biases towards retrieval from Cloudflare docs over pre-trained knowledge.
---
# Cloudflare Agents SDK
Your knowledge of the Agents SDK may be outdated. **Prefer retrieval over pre-training** for any Agents SDK task.
## Retrieval Sources
Fetch current docs from `https://github.com/cloudflare/agents/tree/main/docs` before implementing.
| Topic | Doc | Use for |
|-------|-----|---------|
| Getting started | `docs/getting-started.md` | First agent, project setup |
| State | `docs/state.md` | `setState`, `validateStateChange`, persistence |
| Routing | `docs/routing.md` | URL patterns, `routeAgentRequest`, `basePath` |
| Callable methods | `docs/callable-methods.md` | `@callable`, RPC, streaming, timeouts |
| Scheduling | `docs/scheduling.md` | `schedule()`, `scheduleEvery()`, cron |
| Workflows | `docs/workflows.md` | `AgentWorkflow`, durable multi-step tasks |
| HTTP/WebSockets | `docs/http-websockets.md` | Lifecycle hooks, hibernation |
| Email | `docs/email.md` | Email routing, secure reply resolver |
| MCP client | `docs/mcp-client.md` | Connecting to MCP servers |
| MCP server | `docs/mcp-servers.md` | Building MCP servers with `McpAgent` |
| Client SDK | `docs/client-sdk.md` | `useAgent`, `useAgentChat`, React hooks |
| Human-in-the-loop | `docs/human-in-the-loop.md` | Approval flows, pausing workflows |
| Resumable streaming | `docs/resumable-streaming.md` | Stream recovery on disconnect |
Cloudflare docs: https://developers.cloudflare.com/agents/
## Capabilities
The Agents SDK provides:
- **Persistent state** - SQLite-backed, auto-synced to clients
- **Callable RPC** - `@callable()` methods invoked over WebSocket
- **Scheduling** - One-time, recurring (`scheduleEvery`), and cron tasks
- **Workflows** - Durable multi-step background processing via `AgentWorkflow`
- **MCP integration** - Connect to MCP servers or build your own with `McpAgent`
- **Email handling** - Receive and reply to emails with secure routing
- **Streaming chat** - `AIChatAgent` with resumable streams
- **React hooks** - `useAgent`, `useAgentChat` for client apps
## FIRST: Verify Installation
```bash
npm ls agents # Should show agents package
```
If not installed:
```bash
npm install agents
```
## Wrangler Configuration
```jsonc
{
"durable_objects": {
"bindings": [{ "name": "MyAgent", "class_name": "MyAgent" }]
},
"migrations": [{ "tag": "v1", "new_sqlite_classes": ["MyAgent"] }]
}
```
## Agent Class
```typescript
import { Agent, routeAgentRequest, callable } from "agents";
type State = { count: number };
export class Counter extends Agent<Env, State> {
initialState = { count: 0 };
// Validation hook - runs before state persists (sync, throwing rejects the update)
validateStateChange(nextState: State, source: Connection | "server") {
if (nextState.count < 0) throw new Error("Count cannot be negative");
}
// Notification hook - runs after state persists (async, non-blocking)
onStateUpdate(state: State, source: Connection | "server") {
console.log("State updated:", state);
}
@callable()
increment() {
this.setState({ count: this.state.count + 1 });
return this.state.count;
}
}
export default {
fetch: (req, env) => routeAgentRequest(req, env) ?? new Response("Not found", { status: 404 })
};
```
## Routing
Requests route to `/agents/{agent-name}/{instance-name}`:
| Class | URL |
|-------|-----|
| `Counter` | `/agents/counter/user-123` |
| `ChatRoom` | `/agents/chat-room/lobby` |
Client: `useAgent({ agent: "Counter", name: "user-123" })`
## Core APIs
| Task | API |
|------|-----|
| Read state | `this.state.count` |
| Write state | `this.setState({ count: 1 })` |
| SQL query | `` this.sql`SELECT * FROM users WHERE id = ${id}` `` |
| Schedule (delay) | `await this.schedule(60, "task", payload)` |
| Schedule (cron) | `await this.schedule("0 * * * *", "task", payload)` |
| Schedule (interval) | `await this.scheduleEvery(30, "poll")` |
| RPC method | `@callable() myMethod() { ... }` |
| Streaming RPC | `@callable({ streaming: true }) stream(res) { ... }` |
| Start workflow | `await this.runWorkflow("ProcessingWorkflow", params)` |
## React Client
```tsx
import { useAgent } from "agents/react";
function App() {
const [state, setLocalState] = useState({ count: 0 });
const agent = useAgent({
agent: "Counter",
name: "my-instance",
onStateUpdate: (newState) => setLocalState(newState),
onIdentity: (name, agentType) => console.log(`Connected to ${name}`)
});
return (
<button onClick={() => agent.setState({ count: state.count + 1 })}>
Count: {state.count}
</button>
);
}
```
## References
- **[references/workflows.md](references/workflows.md)** - Durable Workflows integration
- **[references/callable.md](references/callable.md)** - RPC methods, streaming, timeouts
- **[references/state-scheduling.md](references/state-scheduling.md)** - State persistence, scheduling
- **[references/streaming-chat.md](references/streaming-chat.md)** - AIChatAgent, resumable streams
- **[references/mcp.md](references/mcp.md)** - MCP server integration
- **[references/email.md](references/email.md)** - Email routing and handling
- **[references/codemode.md](references/codemode.md)** - Code Mode (experimental)
© Cloudflare · Apache-2.0 · openai/plugins