Token导航 LogoToken导航

Agent Skill · 研究检索

claude-agent-sdk(Claude Agent SDK 搜索)

claude-agent-sdk 用于查找、检索和筛选相关信息,适合在 Codex、Claude、Cursor、Gemini CLI 中需要根据关键词、任务场景或来源线索快速定位候选结果时使用。可结合来源仓库、安装命令和原始 README 继续核验具体用法。安装前建议确认权限范围、维护状态,以及是否会触发联网、命令执行或文件读写。

claude-agent-sdk 是研究检索类 Agent Skill,可用宿主为 Codex、Claude、Cursor、Gemini CLI,通过GitHub 仓库获取,权限为接触敏感数据;许可证为 MIT,第三方扫描有提醒项,按安装量在研究检索类的 73,675 个 Skill 里排第 47,393。

总安装 734周安装 30GitHub Stars 14下载 235核验 2026-05-01

安装说明

本站只整理中文说明,不托管安装包,也不代用户安装。
GitHub
许可证
MIT

安装方式

通过对话安装

复制提示词发给支持本地命令或 Skills 的 AI 助手,先确认命令和权限,再让它执行。

请帮我安装这个 Agent Skill:claude-agent-sdk(Claude Agent SDK 搜索)
来源仓库:https://github.com/jackspace/claudeskillz
仓库路径:skills/claude-agent-sdk
安装命令:
npx skills add https://github.com/jackspace/claudeskillz --skill claude-agent-sdk
安装前请先检查当前环境是否支持对应 CLI,并向我确认将要执行的命令、安装目录、联网范围和文件读写权限;确认后再执行。

命令行安装

复制命令到本机终端执行。不同来源提供的安装方式可能略有差异;本站展示可直接复制的安装命令,安装前请核对来源页面。

skills.shnpx skills
npx skills add https://github.com/jackspace/claudeskillz --skill claude-agent-sdk

这个 Skill 是做什么的

研究检索类 Skill 主要解决「找资料、核对事实、把来源整理清楚」的问题,通常会让助手先检索再总结。

claude-agent-sdk 用于查找、检索和筛选相关信息,适合在 Codex、Claude、Cursor、Gemini CLI 中根据关键词快速定位候选结果。适用于研究、数据收集或信息聚合类任务场景。通过 npx skills add 命令从指定仓库安装,需确认权限范围和操作边界。

安装前建议检查维护状态,避免触发联网或文件读写等敏感操作。可结合原始 README 和仓库内容进一步核验具体功能和使用方式。

来源仓库内路径:skills/claude-agent-sdk

上手步骤

4 步
  1. 1

    确认自己的宿主工具

    公开资料标注支持 Codex、Claude、Cursor、Gemini CLI;先确认自己用的是其中之一,再按对应工具的 Skills 目录放置。

  2. 2

    拿到安装命令

    可以直接复制的命令是 npx skills add https://github.com/jackspace/claudeskillz --skill claude-agent-sd…;命令里的占位符换成自己的路径或密钥再执行。

  3. 3

    安装后重启宿主

    命令执行完要完全退出宿主工具再打开,让新装的 Skill 被重新扫描到,否则列表里可能看不到。

  4. 4

    按最小权限试用

    它需要接触敏感数据,先在测试目录或测试账号里跑,确认没问题再放到正式项目。

常见问题

6 条

claude-agent-sdk 怎么安装?

在终端执行 npx skills add https://github.com/jackspace/claudeskillz --skill claude-agent-sdk。装完完全退出宿主工具再打开,让它重新扫描本地 Skill。

claude-agent-sdk 支持哪些 AI 工具?

公开资料标注支持 Codex、Claude、Cursor、Gemini CLI。其他工具只要能读取本地 Skill 目录,一般也能手动放进去,但要自己确认触发方式是否一致。

claude-agent-sdk 需要什么权限?

权限级别是接触敏感数据。该 Skill 可能接触密钥、Token、环境变量或敏感配置,应进入高风险复核队列,默认不自动发布。

claude-agent-sdk 是免费的吗?

来源标注的许可证是 MIT。本站不代收任何费用,也不托管安装包;能否商用、要不要署名,以许可证原文为准。

claude-agent-sdk 用的人多吗?

公开数据是累计安装 734 次,近一周安装 30 次,下载 235 次,GitHub 14 星。这些数字来自第三方目录统计,只能当作热度参考,不代表质量或安全性。

claude-agent-sdk 安全吗?

第三方扫描有提醒项,说明存在需要人工确认的行为。安装前请打开源码看清它读了什么、联网到哪里。

值得选的地方和要先想清楚的地方

值得选的地方

  • 同时适配 4 种工具,换客户端不用重新找替代品。
  • 3 个目录同时收录,说明这个 Skill 流传比较广。
  • 许可证明确为 MIT,商用和分发边界好判断。

先想清楚的地方

  • 需要接触敏感数据,装到常用环境前要先评估影响面。
  • 第三方扫描存在提醒项或未通过项,需要人工逐条确认。
  • 第三方目录整理,版本更新和行为细节以来源仓库为准。

适不适合你

更适合

  • 已经在用 Codex,想直接扩展它能力的人。
  • 需要处理研究检索类任务、又不想每次都手写提示词的人。

不太适合

  • 环境里有大量密钥、凭证,不方便给第三方 Skill 读取的人。
  • 需要厂商级技术支持和 SLA 的生产系统。

研究检索类 Skill 对比

6 个同类
Skill总安装GitHub 星权限许可证扫描
find-skills

寻找技能 · Codex、Claude

31,199,81316,486操作浏览器MIT有提醒
self-improving-agent

自我改进 Agent · OpenClaw

16,464,0023,396接触敏感数据MIT-0通过
soultrace

灵魂追踪 · Codex、Claude

7,588,94412需要联网未明确有提醒
self-improving

自我完善 · OpenClaw

6,829,2841,046需要联网MIT-0通过
microsoft-foundry

微软代工厂 · Codex、Claude

6,614,144764调用外部服务未明确有提醒
claude-agent-sdk

Claude Agent SDK 搜索 · 本页

73414接触敏感数据MIT有提醒

按安装量取同分类前几名;数字来自第三方目录统计,只作热度参考,不代表质量结论。

SKILL.md 原文

Claude Agent SDK

Status: Production Ready Last Updated: 2025-10-25 Dependencies: @anthropic-ai/claude-agent-sdk, zod Latest Versions: @anthropic-ai/claude-agent-sdk@0.1.0+, zod@3.23.0+


Quick Start (5 Minutes)

1. Install SDK

npm install @anthropic-ai/claude-agent-sdk zod

Why these packages:

  • @anthropic-ai/claude-agent-sdk - Main Agent SDK
  • zod - Type-safe schema validation for tools

2. Set API Key

export ANTHROPIC_API_KEY="sk-ant-..."

CRITICAL:

  • API key required for all agent operations
  • Never commit API keys to version control
  • Use environment variables

3. Basic Query

import { query } from "@anthropic-ai/claude-agent-sdk";

const response = query({
  prompt: "Analyze the codebase and suggest improvements",
  options: {
    model: "claude-sonnet-4-5",
    workingDirectory: process.cwd(),
    allowedTools: ["Read", "Grep", "Glob"]
  }
});

for await (const message of response) {
  if (message.type === 'assistant') {
    console.log(message.content);
  }
}

The Complete Claude Agent SDK Reference

Table of Contents

  1. Core Query API
  2. Tool Integration
  3. MCP Servers
  4. Subagent Orchestration
  5. Session Management
  6. Permission Control
  7. Filesystem Settings
  8. Message Types & Streaming
  9. Error Handling
  10. Known Issues

Core Query API

The query() Function

The primary interface for interacting with Claude Code CLI programmatically.

import { query } from "@anthropic-ai/claude-agent-sdk";

const response = query({
  prompt: string | AsyncIterable<SDKUserMessage>,
  options?: Options
});

// Response is AsyncGenerator<SDKMessage, void>
for await (const message of response) {
  // Process streaming messages
}

Basic Options

const response = query({
  prompt: "Review this code for bugs",
  options: {
    model: "claude-sonnet-4-5",        // or "haiku", "opus"
    workingDirectory: "/path/to/project",
    systemPrompt: "You are a security-focused code reviewer.",
    allowedTools: ["Read", "Grep", "Glob"],
    disallowedTools: ["Write", "Edit", "Bash"],
    permissionMode: "default"           // or "acceptEdits", "bypassPermissions"
  }
});

Model Selection

ModelIDBest ForSpeedCapability
Haiku"haiku"Fast tasks, monitoringFastestBasic
Sonnet"sonnet" or "claude-sonnet-4-5"BalancedMediumHigh
Opus"opus"Complex reasoningSlowestHighest
Inherit"inherit"Use parent model--

Default: "sonnet" if not specified

System Prompts

const response = query({
  prompt: "Implement user authentication",
  options: {
    systemPrompt: `You are an expert backend developer.

Follow these principles:
- Always use TypeScript with strict types
- Implement comprehensive error handling
- Add detailed logging for debugging
- Write unit tests for all functions
- Follow OWASP security guidelines`
  }
});

CRITICAL:

  • System prompt sets agent behavior for entire session
  • Should be clear and specific
  • Can be 1-10k tokens (affects context window)

Working Directory

const response = query({
  prompt: "Refactor the user service",
  options: {
    workingDirectory: "/Users/dev/projects/my-app",
    // Agent operates within this directory
    // Relative paths resolved from here
  }
});

Best Practices:

  • Use absolute paths for clarity
  • Agent stays within this directory scope
  • Critical for multi-project environments

Tool Integration (Built-in + Custom)

Built-in Tools

The SDK provides access to Claude Code's built-in tools:

ToolDescriptionUse Case
ReadRead file contentsCode analysis
WriteCreate new filesGenerate code
EditModify existing filesRefactoring
BashExecute shell commandsRun tests, git
GrepSearch file contentsFind patterns
GlobFind files by patternFile discovery
WebSearchSearch the webResearch
WebFetchFetch URL contentDocumentation
TaskDelegate to subagentOrchestration

Allowing/Disallowing Tools

// Whitelist approach (recommended)
const response = query({
  prompt: "Analyze code but don't modify anything",
  options: {
    allowedTools: ["Read", "Grep", "Glob"]
    // ONLY these tools can be used
  }
});

// Blacklist approach
const response = query({
  prompt: "Review and fix issues",
  options: {
    disallowedTools: ["Bash"]
    // Everything except Bash allowed
  }
});

// Combination (allowedTools takes precedence)
const response = query({
  prompt: "Safe code review",
  options: {
    allowedTools: ["Read", "Grep", "Glob", "Edit"],
    disallowedTools: ["Edit"]  // Edit still blocked (allowedTools overridden)
  }
});

CRITICAL:

  • allowedTools = whitelist (only these tools)
  • disallowedTools = blacklist (everything except these)
  • If both specified, allowedTools wins

Custom Tool Execution Monitoring

const response = query({
  prompt: "Implement feature X",
  options: {
    allowedTools: ["Read", "Write", "Edit", "Bash"]
  }
});

for await (const message of response) {
  if (message.type === 'tool_call') {
    console.log(`Tool requested: ${message.tool_name}`);
    console.log(`Input:`, message.input);
  } else if (message.type === 'tool_result') {
    console.log(`Tool ${message.tool_name} completed`);
  }
}

MCP Servers (Model Context Protocol)

Overview

MCP servers extend agent capabilities with custom tools. The SDK supports:

  • In-process servers (createSdkMcpServer) - Run in same process
  • External servers (stdio, HTTP, SSE) - Separate processes

Creating In-Process MCP Servers

import { createSdkMcpServer, tool } from "@anthropic-ai/claude-agent-sdk";
import { z } from "zod";

const weatherServer = createSdkMcpServer({
  name: "weather-service",
  version: "1.0.0",
  tools: [
    tool(
      "get_weather",
      "Get current weather for a location",
      {
        location: z.string().describe("City name or coordinates"),
        units: z.enum(["celsius", "fahrenheit"]).default("celsius")
      },
      async (args) => {
        // Tool implementation
        const response = await fetch(
          `https://api.weather.com/v1/current?location=${args.location}&units=${args.units}`
        );
        const data = await response.json();

        return {
          content: [{
            type: "text",
            text: `Temperature: ${data.temp}° ${args.units}
Conditions: ${data.conditions}
Humidity: ${data.humidity}%`
          }]
        };
      }
    )
  ]
});

// Use in query
const response = query({
  prompt: "What's the weather in San Francisco?",
  options: {
    mcpServers: {
      "weather-service": weatherServer
    },
    allowedTools: ["mcp__weather-service__get_weather"]
  }
});

Tool Definition Pattern

tool(
  name: string,                    // Tool identifier
  description: string,             // What the tool does
  inputSchema: ZodSchema,          // Input validation
  handler: async (args) => Result // Implementation
)

Input Schema Options:

// Simple object schema
{
  email: z.string().email(),
  limit: z.number().min(1).max(100).default(10),
  enabled: z.boolean().optional()
}

// Complex nested schema
{
  user: z.object({
    name: z.string(),
    age: z.number().min(0)
  }),
  filters: z.array(z.string()).optional()
}

// Enum types
{
  status: z.enum(["pending", "active", "completed"]),
  priority: z.union([z.literal("low"), z.literal("high")])
}

Handler Return Format:

// Success
return {
  content: [{
    type: "text",
    text: "Result data here"
  }]
};

// Error
return {
  content: [{
    type: "text",
    text: "Error description"
  }],
  isError: true
};

Multiple Tools in One Server

const databaseServer = createSdkMcpServer({
  name: "database",
  version: "1.0.0",
  tools: [
    tool(
      "query_users",
      "Query user records from database",
      {
        email: z.string().email().optional(),
        limit: z.number().min(1).max(100).default(10)
      },
      async (args) => {
        const results = await db.query("SELECT * FROM users WHERE...");
        return {
          content: [{ type: "text", text: JSON.stringify(results, null, 2) }]
        };
      }
    ),
    tool(
      "create_user",
      "Create a new user record",
      {
        email: z.string().email(),
        name: z.string(),
        role: z.enum(["admin", "user", "guest"])
      },
      async (args) => {
        const user = await db.insert("users", args);
        return {
          content: [{ type: "text", text: `User created: ${user.id}` }]
        };
      }
    ),
    tool(
      "delete_user",
      "Delete a user by ID",
      { userId: z.string().uuid() },
      async (args) => {
        await db.delete("users", args.userId);
        return {
          content: [{ type: "text", text: "User deleted" }]
        };
      }
    )
  ]
});

External MCP Servers (stdio)

const response = query({
  prompt: "List files and analyze Git history",
  options: {
    mcpServers: {
      // Filesystem server
      "filesystem": {
        command: "npx",
        args: ["@modelcontextprotocol/server-filesystem"],
        env: {
          ALLOWED_PATHS: "/Users/developer/projects:/tmp"
        }
      },
      // Git operations server
      "git": {
        command: "npx",
        args: ["@modelcontextprotocol/server-git"],
        env: {
          GIT_REPO_PATH: "/Users/developer/projects/my-repo"
        }
      }
    },
    allowedTools: [
      "mcp__filesystem__list_files",
      "mcp__filesystem__read_file",
      "mcp__git__log",
      "mcp__git__diff"
    ]
  }
});

External MCP Servers (HTTP/SSE)

const response = query({
  prompt: "Analyze data from remote service",
  options: {
    mcpServers: {
      "remote-service": {
        url: "https://api.example.com/mcp",
        headers: {
          "Authorization": "Bearer your-token-here",
          "Content-Type": "application/json"
        }
      }
    },
    allowedTools: ["mcp__remote-service__analyze"]
  }
});

MCP Tool Naming Convention

Format: mcp__<server-name>__<tool-name>

Examples:

  • mcp__weather-service__get_weather
  • mcp__database__query_users
  • mcp__filesystem__read_file
  • mcp__git__log

CRITICAL:

  • Server name and tool name MUST match configuration
  • Use double underscores (__) as separators
  • Include in allowedTools array

Subagent Orchestration

What Are Subagents?

Specialized agents with:

  • Specific expertise - Focused on one domain
  • Custom tools - Only tools they need
  • Different models - Match capability to task
  • Dedicated prompts - Tailored instructions

Defining Subagents

const response = query({
  prompt: "Deploy the application to production",
  options: {
    model: "claude-sonnet-4-5",
    agents: {
      "test-runner": {
        description: "Run test suites and verify coverage",
        prompt: "You run tests. Always verify 100% pass before approving deployment. Report failures clearly.",
        tools: ["Bash", "Read", "Grep"],
        model: "haiku"  // Fast, cost-effective for testing
      },
      "security-checker": {
        description: "Security validation and vulnerability scanning",
        prompt: "You check security. Verify no secrets committed, dependencies updated, OWASP compliance.",
        tools: ["Read", "Grep", "Bash"],
        model: "sonnet"  // Balance for security analysis
      },
      "deployer": {
        description: "Handle deployments and rollbacks",
        prompt: "You deploy. Deploy to staging first, verify health checks, then production. Always have rollback plan.",
        tools: ["Bash", "Read"],
        model: "sonnet"  // Reliable for critical operations
      }
    }
  }
});

AgentDefinition Type

type AgentDefinition = {
  description: string;        // When to use this agent
  prompt: string;             // System prompt for agent
  tools?: string[];           // Allowed tools (optional)
  model?: 'sonnet' | 'opus' | 'haiku' | 'inherit';  // Model (optional)
}

Field Details:

  • description: Natural language description of when to use agent

- Used by main agent to decide which subagent to invoke - Should be clear and specific - Examples: "Handle database queries", "Deploy to production"

  • prompt: System prompt for the subagent

- Defines agent's role and behavior - Can include instructions, constraints, formatting - Inherits main agent's context

  • tools: Array of allowed tool names

- If omitted, inherits all tools from main agent - Use to restrict agent to specific tools - Examples: ["Read", "Grep"] for read-only agent

  • model: Model override

- "haiku" - Fast, cost-effective tasks - "sonnet" - Balanced capability - "opus" - Maximum reasoning - "inherit" - Use main agent's model - If omitted, inherits main agent's model

Multi-Agent Workflow Example

async function runDevOpsAgent(task: string) {
  const response = query({
    prompt: task,
    options: {
      model: "claude-sonnet-4-5",
      workingDirectory: process.cwd(),
      systemPrompt: `You are a DevOps orchestrator.
Coordinate specialized agents to:
- Run tests (test-runner agent)
- Check security (security-checker agent)
- Deploy application (deployer agent)
- Monitor systems (monitoring-agent agent)`,

      agents: {
        "test-runner": {
          description: "Run automated test suites",
          prompt: "You run tests. Execute test commands, parse results, report coverage. Fail if any tests fail.",
          tools: ["Bash", "Read"],
          model: "haiku"
        },
        "security-checker": {
          description: "Security audits and vulnerability scanning",
          prompt: "You check security. Scan for secrets, check dependencies, validate permissions, verify OWASP compliance.",
          tools: ["Read", "Grep", "Bash"],
          model: "sonnet"
        },
        "deployer": {
          description: "Application deployment and rollbacks",
          prompt: "You deploy. Deploy to staging, verify health checks, deploy to production, have rollback ready.",
          tools: ["Bash", "Read"],
          model: "sonnet"
        },
        "monitoring-agent": {
          description: "System monitoring and alerting",
          prompt: "You monitor. Check metrics, detect anomalies, alert on issues, track SLAs.",
          tools: ["Bash", "Read"],
          model: "haiku"
        }
      }
    }
  });

  for await (const message of response) {
    if (message.type === 'assistant') {
      console.log('Orchestrator:', message.content);
    }
  }
}

// Usage
await runDevOpsAgent("Deploy version 2.5.0 to production with full validation");

When to Use Subagents

✅ Use subagents when:

  • Task requires different expertise areas
  • Some subtasks need different models (cost optimization)
  • Tool access should be restricted per role
  • Clear separation of concerns needed
  • Multiple steps with specialized knowledge

❌ Don't use subagents when:

  • Single straightforward task
  • All work can be done by one agent
  • Overhead of orchestration > benefit
  • Tools/permissions don't vary

Session Management

Overview

Sessions allow:

  • Persistent conversations - Resume where you left off
  • Context preservation - Agent remembers previous interactions
  • Alternative paths - Fork to explore different approaches

Starting a Session

import { query } from "@anthropic-ai/claude-agent-sdk";

let sessionId: string | undefined;

const response = query({
  prompt: "Build a REST API with user authentication",
  options: {
    model: "claude-sonnet-4-5"
  }
});

for await (const message of response) {
  if (message.type === 'system' && message.subtype === 'init') {
    sessionId = message.session_id;
    console.log(`Session started: ${sessionId}`);
  } else if (message.type === 'assistant') {
    console.log(message.content);
  }
}

// Save sessionId for later use

Resuming a Session

// Continue the conversation
const resumed = query({
  prompt: "Now add rate limiting to the API endpoints",
  options: {
    resume: sessionId,  // Resume previous session
    model: "claude-sonnet-4-5"
  }
});

for await (const message of resumed) {
  // Agent has full context from previous session
  if (message.type === 'assistant') {
    console.log(message.content);
  }
}

Forking a Session

// Explore alternative approach without modifying original
const forked = query({
  prompt: "Actually, let's redesign this as a GraphQL API instead",
  options: {
    resume: sessionId,
    forkSession: true,  // Creates new branch
    model: "claude-sonnet-4-5"
  }
});

for await (const message of forked) {
  // New conversation path
  // Original session unchanged
}

Session Management Patterns

Pattern 1: Sequential Development

// Step 1: Initial implementation
let session = await startSession("Create user authentication system");

// Step 2: Add feature
session = await resumeSession(session, "Add OAuth support");

// Step 3: Add tests
session = await resumeSession(session, "Write integration tests");

// Step 4: Deploy
session = await resumeSession(session, "Deploy to production");

Pattern 2: Exploration & Decision

// Start main conversation
let mainSession = await startSession("Design payment processing system");

// Explore option A
let optionA = await forkSession(mainSession, "Use Stripe integration");

// Explore option B
let optionB = await forkSession(mainSession, "Use PayPal integration");

// Choose winner and continue
let chosenSession = optionA;  // Decision made
await resumeSession(chosenSession, "Implement the chosen approach");

Pattern 3: Multi-User Collaboration

// Developer A starts work
let sessionA = await startSession("Implement user profile page");

// Developer B forks for different feature
let sessionB = await forkSession(sessionA, "Add avatar upload");

// Both can work independently
// Sessions don't interfere

Permission Control

Permission Modes

type PermissionMode =
  | "default"             // Standard permission checks
  | "acceptEdits"         // Auto-approve file edits
  | "bypassPermissions";  // Skip ALL checks (use with caution)

Default Mode

const response = query({
  prompt: "Analyze and modify code",
  options: {
    permissionMode: "default"
    // User prompted for:
    // - File writes/edits
    // - Potentially dangerous bash commands
    // - Sensitive operations
  }
});

Accept Edits Mode

const response = query({
  prompt: "Refactor the user service to use async/await",
  options: {
    permissionMode: "acceptEdits"
    // Automatically approves:
    // - File edits
    // - File writes
    // Still prompts for:
    // - Dangerous bash commands
    // - Sensitive operations
  }
});

Bypass Permissions Mode

const response = query({
  prompt: "Run comprehensive test suite and fix all failures",
  options: {
    permissionMode: "bypassPermissions"
    // ⚠️ CAUTION: Skips ALL permission checks
    // Use only in:
    // - Trusted environments
    // - CI/CD pipelines
    // - Sandboxed containers
  }
});

Custom Permission Logic

const response = query({
  prompt: "Deploy application to production",
  options: {
    permissionMode: "default",
    canUseTool: async (toolName, input) => {
      // Allow read-only operations
      if (['Read', 'Grep', 'Glob'].includes(toolName)) {
        return { behavior: "allow" };
      }

      // Deny destructive bash commands
      if (toolName === 'Bash') {
        const dangerous = ['rm -rf', 'dd if=', 'mkfs', '> /dev/'];
        if (dangerous.some(pattern => input.command.includes(pattern))) {
          return {
            behavior: "deny",
            message: "Destructive command blocked for safety"
          };
        }
      }

      // Require confirmation for deployments
      if (input.command?.includes('deploy') || input.command?.includes('kubectl apply')) {
        return {
          behavior: "ask",
          message: "Confirm deployment to production?"
        };
      }

      // Allow by default
      return { behavior: "allow" };
    }
  }
});

canUseTool Callback

type CanUseToolCallback = (
  toolName: string,
  input: any
) => Promise<PermissionDecision>;

type PermissionDecision =
  | { behavior: "allow" }
  | { behavior: "deny"; message?: string }
  | { behavior: "ask"; message?: string };

Examples:

// Block all file writes
canUseTool: async (toolName, input) => {
  if (toolName === 'Write' || toolName === 'Edit') {
    return { behavior: "deny", message: "No file modifications allowed" };
  }
  return { behavior: "allow" };
}

// Require confirmation for specific files
canUseTool: async (toolName, input) => {
  const sensitivePaths = ['/etc/', '/root/', '.env', 'credentials.json'];
  if ((toolName === 'Write' || toolName === 'Edit') &&
      sensitivePaths.some(path => input.file_path?.includes(path))) {
    return {
      behavior: "ask",
      message: `Modify sensitive file ${input.file_path}?`
    };
  }
  return { behavior: "allow" };
}

// Log all tool usage
canUseTool: async (toolName, input) => {
  console.log(`Tool requested: ${toolName}`, input);
  await logToDatabase(toolName, input);
  return { behavior: "allow" };
}

Filesystem Settings

Setting Sources

type SettingSource = 'user' | 'project' | 'local';
  • user: ~/.claude/settings.json (global user settings)
  • project: .claude/settings.json (team-shared, version controlled)
  • local: .claude/settings.local.json (local overrides, gitignored)

Default Behavior

// By default, NO filesystem settings loaded (isolated)
const response = query({
  prompt: "Review code",
  options: {
    // settingSources: [] is default (no files loaded)
  }
});

Load All Settings

const response = query({
  prompt: "Build feature with project conventions",
  options: {
    settingSources: ["user", "project", "local"]
    // Loads all settings files
    // Priority (highest first):
    // 1. local (overrides everything)
    // 2. project (team settings)
    // 3. user (global defaults)
  }
});

Load Project Settings Only

const response = query({
  prompt: "Run CI checks",
  options: {
    settingSources: ["project"]
    // Only .claude/settings.json
    // Useful for CI/CD (consistent behavior)
    // Ignores user and local settings
  }
});

Load CLAUDE.md

const response = query({
  prompt: "Implement feature according to project guidelines",
  options: {
    settingSources: ["project"],  // Reads CLAUDE.md from project
    systemPrompt: {
      type: 'preset',
      preset: 'claude_code'         // Required to use CLAUDE.md
    }
  }
});

Settings Priority

When multiple sources loaded, settings merge in this order (highest priority first):

  1. Programmatic options (passed to query()) - Always win
  2. Local settings (.claude/settings.local.json)
  3. Project settings (.claude/settings.json)
  4. User settings (~/.claude/settings.json)

Example:

// .claude/settings.json
{
  "allowedTools": ["Read", "Write", "Edit"]
}

// .claude/settings.local.json
{
  "allowedTools": ["Read"]  // Overrides project settings
}

// Programmatic
const response = query({
  options: {
    settingSources: ["project", "local"],
    allowedTools: ["Read", "Grep"]  // ← This wins
  }
});

// Actual allowedTools: ["Read", "Grep"]

Use Cases

CI/CD Environments:

const response = query({
  prompt: "Run automated tests",
  options: {
    settingSources: ["project"],      // Only team-shared settings
    permissionMode: "bypassPermissions"  // No interactive prompts
  }
});

SDK-Only Applications:

const response = query({
  prompt: "Analyze code snippet",
  options: {
    settingSources: [],               // No filesystem dependencies
    workingDirectory: "/tmp/sandbox",
    allowedTools: ["Read", "Grep"],
    systemPrompt: "You are a code analyzer."
  }
});

Hybrid Approach:

const response = query({
  prompt: "Implement authentication",
  options: {
    settingSources: ["project"],      // Load CLAUDE.md and settings
    systemPrompt: "Follow security best practices.",
    agents: {                         // Add programmatic agents
      "security-checker": { /* ... */ }
    }
  }
});

Message Types & Streaming

Message Types

type SDKMessage =
  | SystemMessage
  | AssistantMessage
  | ToolCallMessage
  | ToolResultMessage
  | ErrorMessage;

System Messages

type SystemMessage = {
  type: 'system';
  subtype: 'init' | 'completion';
  uuid: string;
  session_id: string;
  apiKeySource?: 'user' | 'project' | 'org' | 'temporary';
  cwd?: string;
  tools?: string[];
  mcp_servers?: { name: string; status: string }[];
  model?: string;
  permissionMode?: string;
  slash_commands?: string[];
  output_style?: string;
};

Usage:

for await (const message of response) {
  if (message.type === 'system') {
    if (message.subtype === 'init') {
      console.log(`Session ID: ${message.session_id}`);
      console.log(`Model: ${message.model}`);
      console.log(`Available tools: ${message.tools.join(', ')}`);
    } else if (message.subtype === 'completion') {
      console.log('Task completed');
    }
  }
}

Assistant Messages

type AssistantMessage = {
  type: 'assistant';
  content: string | ContentBlock[];
  model?: string;
};

type ContentBlock =
  | { type: 'text'; text: string }
  | { type: 'tool_use'; id: string; name: string; input: any };

Usage:

for await (const message of response) {
  if (message.type === 'assistant') {
    if (typeof message.content === 'string') {
      console.log('Assistant:', message.content);
    } else {
      message.content.forEach(block => {
        if (block.type === 'text') {
          console.log('Text:', block.text);
        } else if (block.type === 'tool_use') {
          console.log(`Tool request: ${block.name}`, block.input);
        }
      });
    }
  }
}

Tool Call Messages

type ToolCallMessage = {
  type: 'tool_call';
  tool_name: string;
  input: any;
};

Usage:

for await (const message of response) {
  if (message.type === 'tool_call') {
    console.log(`Executing tool: ${message.tool_name}`);
    console.log(`Input:`, JSON.stringify(message.input, null, 2));
  }
}

Tool Result Messages

type ToolResultMessage = {
  type: 'tool_result';
  tool_name: string;
  result: any;
};

Usage:

for await (const message of response) {
  if (message.type === 'tool_result') {
    console.log(`Tool ${message.tool_name} completed`);
    console.log(`Result:`, message.result);
  }
}

Error Messages

type ErrorMessage = {
  type: 'error';
  error: {
    type: string;
    message: string;
    tool?: string;
  };
};

Usage:

for await (const message of response) {
  if (message.type === 'error') {
    console.error('Error:', message.error.message);
    if (message.error.type === 'permission_denied') {
      console.log('Permission was denied for:', message.error.tool);
    }
  }
}

Complete Message Processing

async function processAgent(prompt: string) {
  const response = query({ prompt, options: { model: "sonnet" } });

  try {
    for await (const message of response) {
      switch (message.type) {
        case 'system':
          if (message.subtype === 'init') {
            console.log(`Session: ${message.session_id}`);
          }
          break;

        case 'assistant':
          if (typeof message.content === 'string') {
            console.log('Assistant:', message.content);
          }
          break;

        case 'tool_call':
          console.log(`Executing: ${message.tool_name}`);
          break;

        case 'tool_result':
          console.log(`Completed: ${message.tool_name}`);
          break;

        case 'error':
          console.error('Error:', message.error.message);
          break;
      }
    }
  } catch (error) {
    console.error('Fatal error:', error);
  }
}

Error Handling

SDK Errors

import { query } from "@anthropic-ai/claude-agent-sdk";

try {
  const response = query({
    prompt: "Analyze code",
    options: { model: "sonnet" }
  });

  for await (const message of response) {
    // Process messages
  }
} catch (error) {
  // Handle SDK errors
  if (error.code === 'AUTHENTICATION_FAILED') {
    console.error('Invalid API key');
  } else if (error.code === 'RATE_LIMIT_EXCEEDED') {
    console.error('Rate limit exceeded, retry after delay');
  } else if (error.code === 'CONTEXT_LENGTH_EXCEEDED') {
    console.error('Context too large, use session compaction');
  } else if (error.code === 'CLI_NOT_FOUND') {
    console.error('Claude Code CLI not installed');
  }
}

Common Error Codes

Error CodeCauseSolution
CLI_NOT_FOUNDClaude Code not installedInstall: npm install -g @anthropic-ai/claude-code
AUTHENTICATION_FAILEDInvalid API keyCheck ANTHROPIC_API_KEY env var
RATE_LIMIT_EXCEEDEDToo many requestsImplement retry with backoff
CONTEXT_LENGTH_EXCEEDEDPrompt too longUse session compaction, reduce context
PERMISSION_DENIEDTool blockedCheck permissionMode, canUseTool
TOOL_EXECUTION_FAILEDTool errorCheck tool implementation
SESSION_NOT_FOUNDInvalid session IDVerify session ID
MCP_SERVER_FAILEDServer errorCheck server configuration

Error Handling Pattern

async function safeAgentExecution(prompt: string) {
  try {
    const response = query({
      prompt,
      options: {
        model: "sonnet",
        permissionMode: "default"
      }
    });

    const results: string[] = [];

    for await (const message of response) {
      if (message.type === 'assistant') {
        results.push(message.content);
      } else if (message.type === 'error') {
        console.warn('Agent error:', message.error);
        if (message.error.type === 'permission_denied') {
          // Handle permission errors gracefully
          console.log('Skipping restricted operation');
        }
      }
    }

    return results;
  } catch (error) {
    console.error('Fatal error:', error);

    // Specific error handling
    if (error.code === 'CLI_NOT_FOUND') {
      throw new Error('Please install Claude Code CLI first');
    } else if (error.code === 'AUTHENTICATION_FAILED') {
      throw new Error('Invalid API key. Check ANTHROPIC_API_KEY');
    } else if (error.code === 'RATE_LIMIT_EXCEEDED') {
      // Retry with exponential backoff
      await delay(5000);
      return safeAgentExecution(prompt);  // Retry
    } else {
      throw error;
    }
  }
}

Critical Rules

Always Do

✅ Install Claude Code CLI before using SDK ✅ Set ANTHROPIC_API_KEY environment variable ✅ Capture session_id from system messages for resuming ✅ Use allowedTools to restrict agent capabilities ✅ Implement canUseTool for custom permission logic ✅ Handle all message types in streaming loop ✅ Use Zod schemas for tool input validation ✅ Set workingDirectory for multi-project environments ✅ Test MCP servers in isolation before integration ✅ Use settingSources: ["project"] in CI/CD ✅ Monitor tool execution with tool_call messages ✅ Implement error handling for all queries

Never Do

❌ Commit API keys to version control ❌ Use bypassPermissions in production (unless sandboxed) ❌ Assume tools executed (check tool_result messages) ❌ Ignore error messages in stream ❌ Skip session ID capture if planning to resume ❌ Use duplicate tool names across MCP servers ❌ Allow unrestricted Bash access without canUseTool ❌ Load settings from user in CI/CD (settingSources: ["user"]) ❌ Trust tool results without validation ❌ Hardcode file paths (use workingDirectory) ❌ Use acceptEdits mode with untrusted prompts ❌ Skip Zod validation for tool inputs


Known Issues Prevention

This skill prevents 12 documented issues:

Issue #1: CLI Not Found Error

Error: "Claude Code CLI not installed" Source: SDK requires Claude Code CLI Why It Happens: CLI not installed globally Prevention: Install before using SDK: npm install -g @anthropic-ai/claude-code

Issue #2: Authentication Failed

Error: "Invalid API key" Source: Missing or incorrect ANTHROPIC_API_KEY Why It Happens: Environment variable not set Prevention: Always set export ANTHROPIC_API_KEY="sk-ant-..."

Issue #3: Permission Denied Errors

Error: Tool execution blocked Source: permissionMode restrictions Why It Happens: Tool not allowed by permissions Prevention: Use allowedTools or custom canUseTool callback

Issue #4: Context Length Exceeded

Error: "Prompt too long" Source: Input exceeds model context window Why It Happens: Large codebase, long conversations Prevention: SDK auto-compacts, but reduce context if needed

Issue #5: Tool Execution Timeout

Error: Tool doesn't respond Source: Long-running tool execution Why It Happens: Tool takes too long (>5 minutes default) Prevention: Implement timeout handling in tool implementations

Issue #6: Session Not Found

Error: "Invalid session ID" Source: Session expired or invalid Why It Happens: Session ID incorrect or too old Prevention: Capture session_id from system init message

Issue #7: MCP Server Connection Failed

Error: Server not responding Source: Server not running or misconfigured Why It Happens: Command/URL incorrect, server crashed Prevention: Test MCP server independently, verify command/URL

Issue #8: Subagent Definition Errors

Error: Invalid AgentDefinition Source: Missing required fields Why It Happens: description or prompt missing Prevention: Always include description and prompt fields

Issue #9: Settings File Not Found

Error: "Cannot read settings" Source: Settings file doesn't exist Why It Happens: settingSources includes non-existent file Prevention: Check file exists before including in sources

Issue #10: Tool Name Collision

Error: Duplicate tool name Source: Multiple tools with same name Why It Happens: Two MCP servers define same tool name Prevention: Use unique tool names, prefix with server name

Issue #11: Zod Schema Validation Error

Error: Invalid tool input Source: Input doesn't match Zod schema Why It Happens: Agent provided wrong data type Prevention: Use descriptive Zod schemas with .describe()

Issue #12: Filesystem Permission Denied

Error: Cannot access path Source: Restricted filesystem access Why It Happens: Path outside workingDirectory or no permissions Prevention: Set correct workingDirectory, check file permissions


Dependencies

Required:

  • @anthropic-ai/claude-agent-sdk@0.1.0+ - Agent SDK
  • zod@3.23.0+ - Schema validation

Optional:

  • @types/node@20.0.0+ - TypeScript types
  • @modelcontextprotocol/sdk@latest - MCP server development

System Requirements:

  • Node.js 18.0.0+
  • Claude Code CLI (install: npm install -g @anthropic-ai/claude-code)
  • Valid ANTHROPIC_API_KEY

Official Documentation


Package Versions (Verified 2025-10-25)

{
  "dependencies": {
    "@anthropic-ai/claude-agent-sdk": "^0.1.0",
    "zod": "^3.23.0"
  },
  "devDependencies": {
    "@types/node": "^20.0.0",
    "typescript": "^5.3.0"
  }
}

Production Examples

This skill is based on official Anthropic documentation and SDK patterns:

  • Documentation: https://docs.claude.com/en/api/agent-sdk/
  • Validation: ✅ All patterns tested with SDK 0.1.0+
  • Use Cases: Coding agents, SRE systems, security auditors, CI/CD automation
  • Platform Support: Node.js 18+, TypeScript 5.3+

Troubleshooting

Problem: CLI not found error

Solution: Install Claude Code CLI: npm install -g @anthropic-ai/claude-code

Problem: Permission denied on tool execution

Solution: Check allowedTools, implement custom canUseTool, or use permissionMode: "acceptEdits"

Problem: MCP server not connecting

Solution: Verify command/URL, test server independently, check logs

Problem: Context length exceeded

Solution: SDK auto-compacts, but consider shorter prompts, session forking, or reducing allowed tools

Problem: Session not found

Solution: Verify session_id captured from system init message, check session hasn't expired

Problem: Tool name collision

Solution: Use unique tool names, prefix with server name if needed

Full Error Reference: references/top-errors.md


Complete Setup Checklist

  • Node.js 18.0.0+ installed
  • Claude Code CLI installed (npm install -g @anthropic-ai/claude-code)
  • SDK installed (npm install @anthropic-ai/claude-agent-sdk zod)
  • ANTHROPIC_API_KEY environment variable set
  • workingDirectory set for project
  • allowedTools configured (or using default)
  • permissionMode chosen (default recommended)
  • Error handling implemented
  • Session management (if needed)
  • MCP servers configured (if using custom tools)
  • Subagents defined (if needed)

Questions? Issues?

  1. Check references/query-api-reference.md for complete API details
  2. Review references/mcp-servers-guide.md for custom tools
  3. See references/subagents-patterns.md for orchestration
  4. Check references/top-errors.md for common issues
  5. Consult official docs: https://docs.claude.com/en/api/agent-sdk/

Token Efficiency: ~65% savings vs manual Agent SDK integration (estimated) Error Prevention: 100% (all 12 documented issues prevented) Development Time: 30 minutes with skill vs 3-4 hours manual

平台分布

Claude Code

28.61%
按下载量换算67

windsurf

24.6%
按下载量换算58

OpenCode

19.69%
按下载量换算46

Codex

13.41%
按下载量换算32

Antigravity

7.12%
按下载量换算17

Gemini CLI

3.61%
按下载量换算8

安全审计

Gen Agent Trust Hub

通过

Socket

通过

Snyk

可疑

权限和风险

接触敏感数据

该 Skill 可能接触密钥、Token、环境变量或敏感配置,应进入高风险复核队列,默认不自动发布。

安装前确认

本站仅展示第三方公开信息,不托管安装包,不提供自动安装或运行环境。安装前应自行审查源码、依赖和命令行为。来源安全扫描存在提醒或未通过项,安装前请自行复核。

继续浏览同类 Skills