@ 21st- sdk/ 代理服务器
定义全类型推断的AI代理. 配置层 21号特工。 。 。 .
安装
npm install @21st-sdk/agent zod
快速启动
创建 src/agent.ts数字 :
import { agent, tool } from "@21st-sdk/agent"
import { z } from "zod"
export default agent({
model: "claude-sonnet-4-6",
systemPrompt: "You are a helpful coding assistant.",
tools: {
greet: tool({
description: "Greet a user by name",
inputSchema: z.object({ name: z.string() }),
execute: async ({ name }) => ({
content: [{ type: "text", text: `Hello, ${name}!` }],
}),
}),
},
})
然后部署与 第21个SDK CLI数字 :
npx @21st-sdk/cli login
npx @21st-sdk/cli deploy
API 密码
agent(config)
创建代理配置。 返回配置为 a _type: "agent" 运行时间标记 .
export default agent({
// Model to use (default: "claude-sonnet-4-6")
model: "claude-sonnet-4-6",
// For "claude-code" runtime, string prompts are appended
// to Claude Code's preset system prompt by default.
systemPrompt: "You are a PR reviewer...",
// Custom tools the agent can use
tools: {
lint: tool({ ... }),
test: tool({ ... }),
},
// Runtime: "claude-code" (default) or "codex"
runtime: "claude-code",
// Permission mode: "default", "acceptEdits", or "bypassPermissions"
permissionMode: "default",
// Max turns before stopping (default: 50)
maxTurns: 50,
// Max budget in USD
maxBudgetUsd: 5,
// Lifecycle hooks
onStart: async () => { ... },
onToolCall: async ({ toolName, input }) => { ... },
onToolResult: async ({ toolName, input, output }) => { ... },
onStepFinish: async ({ step }) => { ... },
onFinish: async ({ result }) => { ... },
onError: async ({ error }) => { ... },
})
tool(definition)
用 Zod schema 验证创建工具定义 .
import { tool } from "@21st-sdk/agent"
import { z } from "zod"
const myTool = tool({
description: "What this tool does",
inputSchema: z.object({
path: z.string(),
verbose: z.boolean().optional(),
}),
execute: async (args) => {
// args is fully typed from the schema
return {
content: [{ type: "text", text: "result" }],
}
},
})
钩子
onToolCall
在工具执行前运行 。 返回时 false 挡住它.
onToolCall: async ({ toolName, input }) => {
if (toolName === "Bash" && input.command?.includes("rm -rf")) {
return false // blocked
}
}
onFinish
代理成功完成后运行 .
onFinish: async ({ result }) => {
await fetch("https://my-api.com/webhook", {
method: "POST",
body: JSON.stringify({ done: true }),
})
}
如何运作
agent() 财务报告和审定财务报表 tool() 是身份功能 —— 它们完全返回您传递的东西, 并添加了类型的推论。 实际处决发生在使用克洛德特工SDK的第21代理运行时间(E2B sandbox).
您的代码运行在一个安全的云沙盒中, 完全可以访问 Node.js, git, 和系统工具.
对于 runtime: "claude-code",省略 systemPrompt 使用 Claude Code 的默认预设系统快取. 如果您通过一个字符串, 它会自动附在预设上 。 您也可以直接通过预设对象 :
systemPrompt: {
type: "preset",
preset: "claude_code",
append: "Your extra instructions here",
}
许可证
麻省理工学院