spinny:~/writing $ less building-ai-agents-claude-agent-sdk.md
12Claude Agent SDK为你提供了与驱动Claude Code相同的Agent循环的编程访问。你的Agent可以读取文件、执行Shell命令、搜索网页、编辑代码、通过MCP服务器调用外部API,以及编排子Agent - 所有这些只需要几行TypeScript或Python代码。34与标准的Anthropic Client SDK需要你自己构建工具循环不同,Agent SDK在内部处理工具执行、上下文管理、重试和编排。你只需描述你想做什么、提供工具,Agent会自己搞定剩下的。56## 架构78SDK遵循一个简单的循环: **收集上下文、执行操作、验证、重复**。910```mermaid11graph TD12 Input[User Prompt] --> Loop[Agent Loop]13 Loop --> Reason[Claude Reasons]14 Reason --> Tool[Call Tool]15 Tool --> Result[Tool Result]16 Result --> Loop17 Reason --> Done[Final Response]1819 subgraph "Built-in Tools"20 T1[Read / Write / Edit]21 T2[Bash / Terminal]22 T3[Glob / Grep]23 T4[WebSearch / WebFetch]24 end2526 subgraph "Custom Tools via MCP"27 M1[Your API]28 M2[Database]29 M3[Slack / GitHub / etc.]30 end3132 Tool --> T133 Tool --> M134```3536核心入口是 `query()`,它返回一个异步迭代器,在Agent工作时流式传输消息。每条消息告诉你Agent正在做什么: 推理中、调用工具、接收结果或者交付最终输出。3738## 开始使用3940### 安装4142```bash43# TypeScript44npm install @anthropic-ai/claude-agent-sdk4546# Python47pip install claude-agent-sdk48```4950你需要在环境变量中设置Anthropic API密钥 `ANTHROPIC_API_KEY`。5152### 你的第一个Agent5354```typescript55import { query } from "@anthropic-ai/claude-agent-sdk";5657const conversation = query({58 prompt: "Find all TODO comments in the codebase and create a summary",59 options: {60 allowedTools: ["Read", "Glob", "Grep"],61 },62});6364for await (const message of conversation) {65 if (message.type === "assistant") {66 process.stdout.write(message.content);67 }68 if (message.type === "result" && message.subtype === "success") {69 console.log("\nDone:", message.result);70 }71}72```7374就这么简单。Agent会使用Glob查找文件,用Grep搜索TODO模式,用Read检查匹配项,然后返回结构化的摘要。你不需要编写编排逻辑 - SDK会处理。7576### Python等效写法7778```python79from claude_agent_sdk import query8081async for message in query(82 prompt="Find all TODO comments in the codebase and create a summary",83 options={"allowed_tools": ["Read", "Glob", "Grep"]},84):85 if message.type == "assistant":86 print(message.content, end="")87 if message.type == "result" and message.subtype == "success":88 print(f"\nDone: {message.result}")89```9091## 内置工具9293SDK附带了与Claude Code中相同的工具:9495| 工具 | 描述 |96|------|-------------|97| **Read** | 读取文件内容 |98| **Write** | 创建新文件 |99| **Edit** | 对现有文件进行定向编辑 |100| **Bash** | 执行Shell命令 |101| **Glob** | 按模式查找文件 |102| **Grep** | 使用正则表达式搜索文件内容 |103| **WebSearch** | 搜索网页 |104| **WebFetch** | 获取URL并返回其内容 |105| **AskUserQuestion** | 提示用户输入 |106107你通过 `allowedTools` 控制Agent可以使用哪些工具。如果一个工具不在列表中,Agent就无法调用它。108109## 权限模式110111由于Agent在真实系统上执行真实命令,权限至关重要。112113| 模式 | 行为 | 使用场景 |114|------|----------|----------|115| `default` | 自定义 `canUseTool` 回调逐次判断 | 细粒度控制 |116| `acceptEdits` | 自动批准文件操作,Bash需要确认 | 开发工作流 |117| `dontAsk` | 拒绝不在allowedTools中的所有操作 | 受限Agent |118| `bypassPermissions` | 自动批准所有操作 | 受信任的沙箱环境 |119| `auto` | 模型分类器判断安全性 | 平衡的自动化 |120121```typescript122const conversation = query({123 prompt: "Refactor the auth module to use JWT",124 options: {125 allowedTools: ["Read", "Edit", "Glob", "Grep", "Bash"],126 permissionMode: "acceptEdits",127 },128});129```130131在生产环境中,始终在沙箱化环境(容器、VM)中运行Agent,并使用能让Agent完成工作的最严格权限模式。132133## 使用MCP构建自定义工具134135SDK的真正强大之处在于用你自己的工具扩展Agent。自定义工具被定义为进程内MCP服务器 - 无需子进程管理,没有网络开销。136137### 示例: 天气工具138139```typescript140import { tool, createSdkMcpServer, query } from "@anthropic-ai/claude-agent-sdk";141import { z } from "zod";142143const getTemperature = tool(144 "get_temperature",145 "Get the current temperature at a location",146 {147 latitude: z.number().describe("Latitude"),148 longitude: z.number().describe("Longitude"),149 },150 async ({ latitude, longitude }) => {151 const res = await fetch(152 `https://api.open-meteo.com/v1/forecast?latitude=${latitude}&longitude=${longitude}¤t=temperature_2m&temperature_unit=celsius`153 );154 const data = await res.json();155 return {156 content: [157 {158 type: "text",159 text: `Current temperature: ${data.current.temperature_2m}C`,160 },161 ],162 };163 }164);165166const weatherServer = createSdkMcpServer({167 name: "weather",168 version: "1.0.0",169 tools: [getTemperature],170});171172for await (const message of query({173 prompt: "What's the weather like in Rome?",174 options: {175 mcpServers: { weather: weatherServer },176 allowedTools: ["mcp__weather__get_temperature"],177 },178})) {179 if (message.type === "result" && message.subtype === "success") {180 console.log(message.result);181 }182}183```184185自定义工具遵循 `mcp__{server_name}__{tool_name}` 的命名约定。你可以在 `allowedTools` 中使用通配符: `"mcp__weather__*"` 允许weather服务器的所有工具。186187### 示例: 数据库查询工具188189```typescript190const queryDb = tool(191 "query_database",192 "Run a read-only SQL query against the application database",193 {194 sql: z.string().describe("SQL SELECT query to execute"),195 },196 async ({ sql }) => {197 // Validate: only allow SELECT queries198 if (!sql.trim().toUpperCase().startsWith("SELECT")) {199 return {200 content: [{ type: "text", text: "Error: Only SELECT queries are allowed." }],201 };202 }203204 const result = await pool.query(sql);205 return {206 content: [207 {208 type: "text",209 text: JSON.stringify(result.rows, null, 2),210 },211 ],212 };213 }214);215```216217## 连接外部MCP服务器218219除了进程内工具,你还可以连接到任何现有的MCP服务器 - 与Claude Desktop、Cursor和其他MCP客户端兼容的同一批服务器。220221```typescript222for await (const message of query({223 prompt: "Check the latest issues in the frontend repo and summarize them",224 options: {225 mcpServers: {226 github: {227 command: "npx",228 args: ["-y", "@modelcontextprotocol/server-github"],229 env: { GITHUB_PERSONAL_ACCESS_TOKEN: process.env.GITHUB_TOKEN },230 },231 },232 allowedTools: ["mcp__github__*"],233 },234})) {235 // ...236}237```238239你可以组合多个MCP服务器。Agent能够看到所有已连接服务器的所有工具,并按需使用。240241```mermaid242graph LR243 Agent[Your Agent] --> SDK[Agent SDK]244 SDK --> InProcess[In-process MCP\nCustom Tools]245 SDK --> GitHub[GitHub MCP Server]246 SDK --> Postgres[PostgreSQL MCP Server]247 SDK --> Slack[Slack MCP Server]248```249250## 多Agent编排251252对于复杂的工作流,你可以定义专门的子Agent,由父Agent委派任务。每个子Agent有自己的提示词、工具和专注领域。253254```typescript255for await (const message of query({256 prompt: "Review the PR, check for security issues, and update the changelog",257 options: {258 allowedTools: ["Read", "Edit", "Bash", "Glob", "Grep", "Agent"],259 agents: [260 {261 name: "security-reviewer",262 description: "Reviews code for security vulnerabilities",263 prompt: "You are a security expert. Analyze code for OWASP Top 10 vulnerabilities.",264 allowedTools: ["Read", "Glob", "Grep"],265 },266 {267 name: "changelog-writer",268 description: "Updates the CHANGELOG.md file based on recent changes",269 prompt: "You maintain the project changelog. Follow Keep a Changelog format.",270 allowedTools: ["Read", "Edit", "Bash"],271 },272 ],273 },274})) {275 // The parent agent will:276 // 1. Read the PR diff277 // 2. Delegate security review to security-reviewer278 // 3. Delegate changelog update to changelog-writer279 // 4. Synthesize results280}281```282283在父级的 `allowedTools` 中包含 `"Agent"` 以启用委派。子Agent使用自己的工具运行,除非明确授权,否则无法访问父级的工具。284285```mermaid286graph TD287 Parent[Parent Agent] --> SR[Security Reviewer\nRead, Glob, Grep]288 Parent --> CW[Changelog Writer\nRead, Edit, Bash]289 SR --> Report[Security Report]290 CW --> Updated[Updated CHANGELOG]291 Report --> Parent292 Updated --> Parent293 Parent --> Final[Final Summary]294```295296## 会话与连续性297298Agent可以使用会话在多个查询之间保持上下文。从第一次交互中捕获 `session_id`,并在后续查询中传递给 `resume`。299300```typescript301let sessionId: string | undefined;302303// First query304for await (const message of query({305 prompt: "Read the project structure and understand the architecture",306 options: { allowedTools: ["Read", "Glob", "Grep"] },307})) {308 if (message.type === "init") {309 sessionId = message.session_id;310 }311}312313// Follow-up query (same session, full context preserved)314for await (const message of query({315 prompt: "Now refactor the auth module based on what you learned",316 resume: sessionId,317 options: { allowedTools: ["Read", "Edit", "Bash"] },318})) {319 // Agent remembers the full project context from the first query320}321```322323## Claude Managed Agents324325如果你不想自己托管Agent基础设施,**Claude Managed Agents**(2026年4月推出)提供了完全托管的云服务。Anthropic运行容器、处理扩展,并提供流式API。326327```mermaid328graph LR329 subgraph "Self-hosted (Agent SDK)"330 Code[Your Code] --> SDK2[Agent SDK]331 SDK2 --> API[Claude API]332 SDK2 --> Tools[Your Tools]333 end334335 subgraph "Managed Agents"336 App[Your App] --> MAPI[Managed Agents API]337 MAPI --> Container[Anthropic-hosted Container]338 Container --> API2[Claude API]339 Container --> Tools2[Tools in Container]340 end341```342343关键区别: 使用Agent SDK时,你在自己的基础设施中运行Agent循环。使用Managed Agents时,Anthropic为你托管和运行Agent。你通过基于会话的API进行交互,并通过Server-Sent Events接收事件。344345**定价:**346- **Agent SDK**: 仅标准Claude API token费率。你自己负责托管。347- **Managed Agents**: token费率加上每会话小时$0.08(按毫秒计费)。348349## 生产环境最佳实践350351### 1. 始终沙箱化352353永远不要在生产机器上以不受限制的权限运行Agent。使用容器(Docker、Fly.io、Modal)或沙箱环境(E2B、Vercel Sandbox)。354355### 2. 限制工具访问356357遵循最小权限原则。生成报告的Agent不需要 `Bash` 或 `Write`。358359```typescript360// Too permissive361allowedTools: ["Read", "Write", "Edit", "Bash", "Glob", "Grep"]362363// Better: only what's needed364allowedTools: ["Read", "Glob", "Grep"]365```366367### 3. 使用Hook设置护栏368369Hook允许你在工具调用执行前后进行拦截。用于日志记录、验证和速率限制。370371```typescript372const conversation = query({373 prompt: "Analyze the codebase",374 options: {375 allowedTools: ["Read", "Glob", "Grep"],376 hooks: {377 PreToolUse: async (toolName, input) => {378 console.log(`Tool call: ${toolName}`, input);379 // Return false to block the call380 if (toolName === "Bash" && input.command.includes("rm")) {381 return false;382 }383 return true;384 },385 },386 },387});388```389390### 4. 优雅地处理错误391392Agent循环可能产生错误 - 工具失败、API速率限制、上下文窗口溢出。始终检查消息类型。393394```typescript395for await (const message of conversation) {396 switch (message.type) {397 case "assistant":398 // Agent reasoning399 break;400 case "tool_use":401 // Agent is calling a tool402 break;403 case "result":404 if (message.subtype === "error") {405 console.error("Agent failed:", message.error);406 }407 break;408 }409}410```411412### 5. 监控Token使用量413414Agent循环可能消耗大量token,尤其是在大型代码库中。SDK包含自动上下文压缩功能,但你仍然应该监控使用量。415416## 总结417418Claude Agent SDK将LLM从一个问答机器转变为更接近初级开发者的角色。你的Agent可以读取、编写、执行、验证和迭代 - 与人类遵循的工作流程相同。419420从小处着手: 用几个内置工具构建一个Agent。然后为你的特定领域添加自定义MCP工具。当你的工作流需要专业化时,再扩展到多Agent编排。421422Agent循环与驱动Claude Code的是同一个。如果它能构建软件,你的Agent也能。423424> **入门清单:**425>426> - [x] 安装SDK (`npm install @anthropic-ai/claude-agent-sdk`)427> - [x] 在环境变量中设置 `ANTHROPIC_API_KEY`428> - [x] 使用内置工具(Read、Glob、Grep)构建一个简单的Agent429> - [x] 通过进程内MCP服务器添加自定义工具430> - [x] 连接外部MCP服务器(GitHub、PostgreSQL等)431> - [x] 使用子Agent实现多Agent编排432> - [x] 为生产环境搭建沙箱化环境433> - [x] 添加用于日志记录和护栏的Hook434
:使用Claude Agent SDK构建AI Agent - 实用指南lines 1-434 (END) — press q to close