agents-sdk by cloudflare/skills
npx skills add https://github.com/cloudflare/skills --skill agents-sdk您对 Agents SDK 的了解可能已过时。对于任何 Agents SDK 任务,请优先使用检索而非预训练知识。
在实现之前,请从 https://github.com/cloudflare/agents/tree/main/docs 获取最新文档。
| 主题 | 文档 | 用途 |
|---|---|---|
| 入门指南 | docs/getting-started.md | 第一个智能体,项目设置 |
| 状态 | docs/state.md | setState、validateStateChange、持久化 |
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
| 路由 | docs/routing.md | URL 模式、routeAgentRequest、basePath |
| 可调用方法 | docs/callable-methods.md | @callable、RPC、流式传输、超时 |
| 调度 | docs/scheduling.md | schedule()、scheduleEvery()、cron |
| 工作流 | docs/workflows.md | AgentWorkflow、持久的多步骤任务 |
| HTTP/WebSockets | docs/http-websockets.md | 生命周期钩子、休眠 |
| 电子邮件 | docs/email.md | 电子邮件路由、安全回复解析器 |
| MCP 客户端 | docs/mcp-client.md | 连接到 MCP 服务器 |
| MCP 服务器 | docs/mcp-servers.md | 使用 McpAgent 构建 MCP 服务器 |
| 客户端 SDK | docs/client-sdk.md | useAgent、useAgentChat、React 钩子 |
| 人工介入 | docs/human-in-the-loop.md | 审批流程、暂停工作流 |
| 可恢复流式传输 | docs/resumable-streaming.md | 断开连接时的流恢复 |
Cloudflare 文档:https://developers.cloudflare.com/agents/
Agents SDK 提供:
@callable() 方法scheduleEvery) 和 cron 任务AgentWorkflow 实现持久的多步骤后台处理McpAgent 构建自己的服务器AIChatAgentuseAgent、useAgentChatnpm ls agents # 应显示 agents 包
如果未安装:
npm install agents
{
"durable_objects": {
"bindings": [{ "name": "MyAgent", "class_name": "MyAgent" }]
},
"migrations": [{ "tag": "v1", "new_sqlite_classes": ["MyAgent"] }]
}
import { Agent, routeAgentRequest, callable } from "agents";
type State = { count: number };
export class Counter extends Agent<Env, State> {
initialState = { count: 0 };
// 验证钩子 - 在状态持久化之前运行(同步,抛出错误会拒绝更新)
validateStateChange(nextState: State, source: Connection | "server") {
if (nextState.count < 0) throw new Error("Count cannot be negative");
}
// 通知钩子 - 在状态持久化之后运行(异步,非阻塞)
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 })
};
请求路由到 /agents/{agent-name}/{instance-name}:
| 类 | URL |
|---|---|
Counter | /agents/counter/user-123 |
ChatRoom | /agents/chat-room/lobby |
客户端:useAgent({ agent: "Counter", name: "user-123" })
| 任务 | API |
|---|---|
| 读取状态 | this.state.count |
| 写入状态 | this.setState({ count: 1 }) |
| SQL 查询 | this.sqlSELECT * FROM users WHERE id = ${id}`` |
| 调度(延迟) | await this.schedule(60, "task", payload) |
| 调度(cron) | await this.schedule("0 * * * *", "task", payload) |
| 调度(间隔) | await this.scheduleEvery(30, "poll") |
| RPC 方法 | @callable() myMethod() { ... } |
| 流式 RPC | @callable({ streaming: true }) stream(res) { ... } |
| 启动工作流 | await this.runWorkflow("ProcessingWorkflow", params) |
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>
);
}
每周安装量
1.5K
代码仓库
GitHub 星标数
571
首次出现
2026 年 1 月 19 日
安全审计
安装于
opencode1.3K
codex1.2K
gemini-cli1.2K
github-copilot1.1K
claude-code1.0K
amp1.0K
Your knowledge of the Agents SDK may be outdated. Prefer retrieval over pre-training for any Agents SDK task.
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 |
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/
The Agents SDK provides:
@callable() methods invoked over WebSocketscheduleEvery), and cron tasksAgentWorkflowMcpAgentAIChatAgent with resumable streamsuseAgent, useAgentChat for client appsnpm ls agents # Should show agents package
If not installed:
npm install agents
{
"durable_objects": {
"bindings": [{ "name": "MyAgent", "class_name": "MyAgent" }]
},
"migrations": [{ "tag": "v1", "new_sqlite_classes": ["MyAgent"] }]
}
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 })
};
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" })
| Task | API |
|---|---|
| Read state | this.state.count |
| Write state | this.setState({ count: 1 }) |
| SQL query | this.sqlSELECT * 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") |
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>
);
}
Weekly Installs
1.5K
Repository
GitHub Stars
571
First Seen
Jan 19, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykWarn
Installed on
opencode1.3K
codex1.2K
gemini-cli1.2K
github-copilot1.1K
claude-code1.0K
amp1.0K
React 组合模式指南:Vercel 组件架构最佳实践,提升代码可维护性
102,200 周安装
AI智能体长期记忆系统 - 精英级架构,融合6种方法,永不丢失上下文
1,200 周安装
AI新闻播客制作技能:实时新闻转对话式播客脚本与音频生成
1,200 周安装
Word文档处理器:DOCX创建、编辑、分析与修订痕迹处理全指南 | 自动化办公解决方案
1,200 周安装
React Router 框架模式指南:全栈开发、文件路由、数据加载与渲染策略
1,200 周安装
Nano Banana AI 图像生成工具:使用 Gemini 3 Pro 生成与编辑高分辨率图像
1,200 周安装
SVG Logo Designer - AI 驱动的专业矢量标识设计工具,生成可缩放品牌标识
1,200 周安装
| RPC method | @callable() myMethod() { ... } |
| Streaming RPC | @callable({ streaming: true }) stream(res) { ... } |
| Start workflow | await this.runWorkflow("ProcessingWorkflow", params) |