Claude SDK Expert by frankxai/claude-skills-library
npx skills add https://github.com/frankxai/claude-skills-library --skill 'Claude SDK Expert'本技能提供关于使用 Claude Agent SDK(原 Claude Code SDK)构建自主 AI 代理的全面指导,利用计算机使用能力、工具编排和 MCP 集成进行生产部署。
Claude Agent SDK 支持构建能够与计算机交互、写入文件、运行命令并迭代其工作的自主代理。
演变: 从“Claude Code SDK”更名,以反映其超越编码的更广泛能力。
核心理念: 为 Claude 提供一台计算机,以解锁超越基于聊天的交互的代理效能。
革命性功能: Claude 可以控制计算机环境以完成任务。
这实现了:
使用案例:
文件操作:
Read - 读取文件内容Write - 创建或覆盖文件广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
Edit - 对现有文件进行针对性编辑命令执行:
Bash - 运行 shell 命令和脚本搜索与发现:
Grep - 使用正则表达式搜索文件内容Glob - 按模式查找文件网络访问:
WebFetch - 检索和分析网页WebSearch - 在互联网上搜索信息所有工具都经过生产测试并针对代理使用进行了优化。
模型上下文协议支持: 通过 MCP 服务器定义自定义工具。
优势:
示例 MCP 服务器:
场景: 代理无需人工干预即可完成多步骤任务
流程:
User Request
↓
Claude analyzes task
↓
Breaks into subtasks
↓
Executes via tools (Read, Bash, Write, etc.)
↓
Iterates on failures
↓
Returns result
示例:
from anthropic import Anthropic
client = Anthropic()
response = client.messages.create(
model="claude-sonnet-4-5",
max_tokens=4096,
tools=[
{"type": "computer_use"},
{"type": "bash"},
{"type": "file_operations"}
],
messages=[{
"role": "user",
"content": "Analyze the last 30 days of sales data and create a summary report"
}]
)
# Claude autonomously:
# 1. Reads sales data files
# 2. Runs analysis scripts
# 3. Generates report
# 4. Saves to file
场景: 代理提出行动方案,在执行前等待审批
流程:
Task → Plan → Show to Human → Approve? → Execute → Result
↓ No
Revise Plan
实现:
# Step 1: Generate plan
plan_response = client.messages.create(
model="claude-sonnet-4-5",
messages=[{
"role": "user",
"content": "Create a plan to refactor the authentication system"
}]
)
# Step 2: Human reviews plan
if human_approves(plan_response.content):
# Step 3: Execute with tools
execution_response = client.messages.create(
model="claude-sonnet-4-5",
tools=all_tools,
messages=[{
"role": "user",
"content": f"Execute this plan: {plan_response.content}"
}]
)
场景: 代理根据反馈/错误迭代工作
流程:
Attempt 1 → Error → Analyze → Attempt 2 → Error → Analyze → Attempt 3 → Success
内置功能: Claude SDK 通过计算机使用自然支持此模式——代理可以查看命令输出并进行调整。
良好的工具设计:
# Clear, focused tool
{
"name": "get_customer_orders",
"description": "Retrieve all orders for a specific customer ID",
"input_schema": {
"type": "object",
"properties": {
"customer_id": {
"type": "string",
"description": "The unique customer identifier"
},
"since_date": {
"type": "string",
"description": "ISO date to filter orders from (optional)"
}
},
"required": ["customer_id"]
}
}
不良的工具设计:
# Too broad, unclear purpose
{
"name": "do_customer_stuff",
"description": "Does various things with customers",
"input_schema": {
"type": "object",
"properties": {
"action": {"type": "string"},
"data": {"type": "object"}
}
}
}
应该: ✅ 提供与任务相关的工具 ✅ 使用清晰、描述性的名称 ✅ 编写详细的描述(Claude 会阅读这些!) ✅ 定义严格的输入模式 ✅ 在工具中实现错误处理 ✅ 返回结构化的、可解析的输出
不应该: ❌ 给代理不需要的工具(会增加混淆) ❌ 使用模糊的名称,如“handler”或“processor” ❌ 跳过输入验证 ❌ 返回没有上下文的原始错误消息 ❌ 使工具的副作用不明确
# Define MCP server connection
mcp_config = {
"servers": {
"github": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-github"],
"env": {
"GITHUB_TOKEN": os.getenv("GITHUB_TOKEN")
}
},
"postgres": {
"command": "docker",
"args": ["run", "mcp-postgres-server"],
"env": {
"DATABASE_URL": os.getenv("DATABASE_URL")
}
}
}
}
# Claude automatically discovers tools from MCP servers
response = client.messages.create(
model="claude-sonnet-4-5",
mcp_servers=mcp_config,
messages=[{
"role": "user",
"content": "Find all GitHub issues assigned to me and update the project database"
}]
)
# Claude uses both github and postgres MCP tools
# Create custom MCP server for internal API
from mcp import Server, Tool
server = Server("internal-crm")
@server.tool()
def get_customer_data(customer_id: str):
"""Retrieve customer information from internal CRM"""
return crm_api.get_customer(customer_id)
@server.tool()
def update_customer_notes(customer_id: str, notes: str):
"""Add notes to customer record"""
return crm_api.update(customer_id, {"notes": notes})
# Deploy and connect to Claude
原因: 实时显示用户进度,建立对代理操作的信任
with client.messages.stream(
model="claude-sonnet-4-5",
max_tokens=4096,
tools=tools,
messages=messages
) as stream:
for event in stream:
if event.type == "content_block_delta":
print(event.delta.text, end="", flush=True)
elif event.type == "tool_use":
print(f"\nUsing tool: {event.name}")
稳健的错误管理:
try:
response = client.messages.create(
model="claude-sonnet-4-5",
tools=tools,
messages=messages
)
except anthropic.APIError as e:
# Handle API errors
log_error(f"API Error: {e}")
return fallback_response()
except anthropic.RateLimitError:
# Handle rate limits
time.sleep(60)
retry()
except Exception as e:
# Handle tool execution errors
log_error(f"Tool Error: {e}")
return safe_error_message()
策略:
简单任务使用 Claude Haiku,复杂推理使用 Sonnet
为重复上下文实现缓存
尽可能批量处理类似请求
适当限制 max_tokens
通过回调监控令牌使用情况
simple_task_response = client.messages.create( model="claude-haiku-4", # Cheaper, faster messages=[{"role": "user", "content": "Format this JSON"}] )
complex_task_response = client.messages.create( model="claude-sonnet-4-5", # More capable messages=[{"role": "user", "content": "Analyze architectural trade-offs"}] )
关键安全措施:
工具权限:
# Restrict file access
safe_file_tools = {
"read": {
"allowed_paths": ["/data/public"],
"denied_paths": ["/etc", "/secrets"]
},
"write": {
"allowed_paths": ["/output"],
"denied_paths": ["/"]
}
}
输入净化:
def sanitize_bash_command(cmd: str) -> str:
"""Prevent dangerous commands"""
dangerous = ["rm -rf", ":(){ :|:& };:", "dd if="]
for danger in dangerous:
if danger in cmd:
raise SecurityError(f"Dangerous command blocked: {danger}")
return cmd
审计日志记录:
def log_agent_action(action: dict):
"""Track all agent actions for security audit"""
audit_log.write({
"timestamp": datetime.now(),
"tool": action["tool_name"],
"input": action["input"],
"user": action["user_id"],
"result": action["result"]
})
在适当时,Claude 可以同时使用多个工具:
# Claude automatically parallelizes when possible
response = client.messages.create(
model="claude-sonnet-4-5",
tools=[weather_api, stock_api, news_api],
messages=[{
"role": "user",
"content": "Give me weather, stock prices, and news for San Francisco"
}]
)
# Claude calls all 3 APIs in parallel
# Cache system prompts and large contexts
response = client.messages.create(
model="claude-sonnet-4-5",
system=[{
"type": "text",
"text": large_system_prompt,
"cache_control": {"type": "ephemeral"}
}],
messages=messages
)
# System prompt cached for ~5 minutes
def test_customer_lookup_tool():
"""Test individual tool behavior"""
result = get_customer_orders("CUST123")
assert result["customer_id"] == "CUST123"
assert isinstance(result["orders"], list)
def test_agent_workflow():
"""Test agent using multiple tools"""
response = client.messages.create(
model="claude-sonnet-4-5",
tools=[tool1, tool2, tool3],
messages=[{
"role": "user",
"content": "Process order #12345"
}]
)
# Verify expected tool usage
tool_calls = extract_tool_calls(response)
assert "verify_order" in tool_calls
assert "process_payment" in tool_calls
# Use Claude's built-in evaluation
from anthropic import Anthropic
eval_client = Anthropic()
eval_results = eval_client.evaluate(
agent=my_agent,
test_cases=[
{"input": "...", "expected_output": "..."},
# More test cases
],
metrics=["accuracy", "latency", "tool_efficiency"]
)
async def research_agent(query: str):
"""Agent researches topic using multiple sources"""
response = await client.messages.create(
model="claude-sonnet-4-5",
tools=[web_search, web_fetch, summarize],
messages=[{
"role": "user",
"content": f"Research '{query}' and provide comprehensive summary"
}]
)
# Claude: searches → fetches articles → summarizes → synthesizes
return response.content
def code_agent(requirements: str):
"""Agent writes and tests code"""
response = client.messages.create(
model="claude-sonnet-4-5",
tools=[write_file, bash, read_file],
messages=[{
"role": "user",
"content": f"Write and test code for: {requirements}"
}]
)
# Claude: writes code → saves file → runs tests → fixes errors → retries
return response.content
def data_pipeline_agent(source: str, destination: str):
"""Agent ETL pipeline"""
response = client.messages.create(
model="claude-sonnet-4-5",
tools=[read_file, bash, postgres_insert],
messages=[{
"role": "user",
"content": f"Extract data from {source}, transform it, and load to {destination}"
}]
)
# Claude orchestrates full ETL
return response.content
最适合:
特点:
最适合:
特点:
from fastapi import FastAPI
from anthropic import Anthropic
app = FastAPI()
client = Anthropic()
@app.post("/agent/task")
async def run_agent_task(task: dict):
response = client.messages.create(
model="claude-sonnet-4-5",
tools=load_tools_for_task(task),
messages=[{
"role": "user",
"content": task["description"]
}]
)
return {"result": response.content}
from langchain_anthropic import ChatAnthropic
from langchain.agents import initialize_agent
llm = ChatAnthropic(model="claude-sonnet-4-5")
agent = initialize_agent(
tools=[tool1, tool2],
llm=llm,
agent_type="structured-chat-zero-shot-react-description"
)
result = agent.run("Complete this task")
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("claude-agent")
def run_agent_with_logging(task):
logger.info(f"Starting task: {task}")
response = client.messages.create(
model="claude-sonnet-4-5",
tools=tools,
messages=[{"role": "user", "content": task}]
)
logger.info(f"Tools used: {extract_tools(response)}")
logger.info(f"Token usage: {response.usage}")
return response
在以下情况下使用 Claude SDK:
在以下情况下考虑替代方案:
官方文档:
GitHub:
本技能确保您使用 Claude 在 2025 年的尖端能力构建强大、自主的代理。
每周安装次数
–
代码仓库
GitHub 星标数
5
首次出现时间
–
安全审计
This skill provides comprehensive guidance on building autonomous AI agents using the Claude Agent SDK (formerly Claude Code SDK), leveraging computer use capabilities, tool orchestration, and MCP integration for production deployments.
The Claude Agent SDK enables building autonomous agents that can interact with computers, write files, run commands, and iterate on their work.
Evolution: Renamed from "Claude Code SDK" to reflect broader capabilities beyond coding.
Core Philosophy: Give Claude a computer to unlock agent effectiveness beyond chat-based interactions.
Revolutionary Feature: Claude can control a computer environment to complete tasks.
What This Enables:
Use Cases:
File Operations:
Read - Read file contentsWrite - Create or overwrite filesEdit - Make targeted edits to existing filesCommand Execution:
Bash - Run shell commands and scriptsSearch & Discovery:
Grep - Search file contents with regexGlob - Find files by patternWeb Access:
WebFetch - Retrieve and analyze web pagesWebSearch - Search the internet for informationAll tools are production-tested and optimized for agent use.
Model Context Protocol Support: Define custom tools via MCP servers.
Benefits:
Example MCP Servers:
Scenario: Agent completes multi-step task without human intervention
Flow:
User Request
↓
Claude analyzes task
↓
Breaks into subtasks
↓
Executes via tools (Read, Bash, Write, etc.)
↓
Iterates on failures
↓
Returns result
Example:
from anthropic import Anthropic
client = Anthropic()
response = client.messages.create(
model="claude-sonnet-4-5",
max_tokens=4096,
tools=[
{"type": "computer_use"},
{"type": "bash"},
{"type": "file_operations"}
],
messages=[{
"role": "user",
"content": "Analyze the last 30 days of sales data and create a summary report"
}]
)
# Claude autonomously:
# 1. Reads sales data files
# 2. Runs analysis scripts
# 3. Generates report
# 4. Saves to file
Scenario: Agent proposes actions, waits for approval before executing
Flow:
Task → Plan → Show to Human → Approve? → Execute → Result
↓ No
Revise Plan
Implementation:
# Step 1: Generate plan
plan_response = client.messages.create(
model="claude-sonnet-4-5",
messages=[{
"role": "user",
"content": "Create a plan to refactor the authentication system"
}]
)
# Step 2: Human reviews plan
if human_approves(plan_response.content):
# Step 3: Execute with tools
execution_response = client.messages.create(
model="claude-sonnet-4-5",
tools=all_tools,
messages=[{
"role": "user",
"content": f"Execute this plan: {plan_response.content}"
}]
)
Scenario: Agent iterates on work based on feedback/errors
Flow:
Attempt 1 → Error → Analyze → Attempt 2 → Error → Analyze → Attempt 3 → Success
Built-in: Claude SDK naturally supports this through computer use - agents can see command outputs and adjust.
Good Tool Design:
# Clear, focused tool
{
"name": "get_customer_orders",
"description": "Retrieve all orders for a specific customer ID",
"input_schema": {
"type": "object",
"properties": {
"customer_id": {
"type": "string",
"description": "The unique customer identifier"
},
"since_date": {
"type": "string",
"description": "ISO date to filter orders from (optional)"
}
},
"required": ["customer_id"]
}
}
Poor Tool Design:
# Too broad, unclear purpose
{
"name": "do_customer_stuff",
"description": "Does various things with customers",
"input_schema": {
"type": "object",
"properties": {
"action": {"type": "string"},
"data": {"type": "object"}
}
}
}
DO: ✅ Provide tools relevant to the task ✅ Use clear, descriptive names ✅ Write detailed descriptions (Claude reads these!) ✅ Define strict input schemas ✅ Implement error handling in tools ✅ Return structured, parseable outputs
DON'T: ❌ Give agents tools they don't need (increases confusion) ❌ Use ambiguous names like "handler" or "processor" ❌ Skip input validation ❌ Return raw error messages without context ❌ Make tools with side effects unclear
# Define MCP server connection
mcp_config = {
"servers": {
"github": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-github"],
"env": {
"GITHUB_TOKEN": os.getenv("GITHUB_TOKEN")
}
},
"postgres": {
"command": "docker",
"args": ["run", "mcp-postgres-server"],
"env": {
"DATABASE_URL": os.getenv("DATABASE_URL")
}
}
}
}
# Claude automatically discovers tools from MCP servers
response = client.messages.create(
model="claude-sonnet-4-5",
mcp_servers=mcp_config,
messages=[{
"role": "user",
"content": "Find all GitHub issues assigned to me and update the project database"
}]
)
# Claude uses both github and postgres MCP tools
# Create custom MCP server for internal API
from mcp import Server, Tool
server = Server("internal-crm")
@server.tool()
def get_customer_data(customer_id: str):
"""Retrieve customer information from internal CRM"""
return crm_api.get_customer(customer_id)
@server.tool()
def update_customer_notes(customer_id: str, notes: str):
"""Add notes to customer record"""
return crm_api.update(customer_id, {"notes": notes})
# Deploy and connect to Claude
Why: Show user progress in real-time, build trust in agent actions
with client.messages.stream(
model="claude-sonnet-4-5",
max_tokens=4096,
tools=tools,
messages=messages
) as stream:
for event in stream:
if event.type == "content_block_delta":
print(event.delta.text, end="", flush=True)
elif event.type == "tool_use":
print(f"\nUsing tool: {event.name}")
Robust Error Management:
try:
response = client.messages.create(
model="claude-sonnet-4-5",
tools=tools,
messages=messages
)
except anthropic.APIError as e:
# Handle API errors
log_error(f"API Error: {e}")
return fallback_response()
except anthropic.RateLimitError:
# Handle rate limits
time.sleep(60)
retry()
except Exception as e:
# Handle tool execution errors
log_error(f"Tool Error: {e}")
return safe_error_message()
Strategies:
Use Claude Haiku for simple tasks, Sonnet for complex reasoning
Implement caching for repetitive contexts
Batch similar requests when possible
Limit max_tokens appropriately
Monitor token usage via callbacks
simple_task_response = client.messages.create( model="claude-haiku-4", # Cheaper, faster messages=[{"role": "user", "content": "Format this JSON"}] )
complex_task_response = client.messages.create( model="claude-sonnet-4-5", # More capable messages=[{"role": "user", "content": "Analyze architectural trade-offs"}] )
Critical Security Measures:
Tool Permissions:
# Restrict file access
safe_file_tools = {
"read": {
"allowed_paths": ["/data/public"],
"denied_paths": ["/etc", "/secrets"]
},
"write": {
"allowed_paths": ["/output"],
"denied_paths": ["/"]
}
}
Input Sanitization:
def sanitize_bash_command(cmd: str) -> str:
"""Prevent dangerous commands"""
dangerous = ["rm -rf", ":(){ :|:& };:", "dd if="]
for danger in dangerous:
if danger in cmd:
raise SecurityError(f"Dangerous command blocked: {danger}")
return cmd
Audit Logging:
def log_agent_action(action: dict):
"""Track all agent actions for security audit"""
audit_log.write({
"timestamp": datetime.now(),
"tool": action["tool_name"],
"input": action["input"],
"user": action["user_id"],
"result": action["result"]
})
Claude can use multiple tools simultaneously when appropriate:
# Claude automatically parallelizes when possible
response = client.messages.create(
model="claude-sonnet-4-5",
tools=[weather_api, stock_api, news_api],
messages=[{
"role": "user",
"content": "Give me weather, stock prices, and news for San Francisco"
}]
)
# Claude calls all 3 APIs in parallel
# Cache system prompts and large contexts
response = client.messages.create(
model="claude-sonnet-4-5",
system=[{
"type": "text",
"text": large_system_prompt,
"cache_control": {"type": "ephemeral"}
}],
messages=messages
)
# System prompt cached for ~5 minutes
def test_customer_lookup_tool():
"""Test individual tool behavior"""
result = get_customer_orders("CUST123")
assert result["customer_id"] == "CUST123"
assert isinstance(result["orders"], list)
def test_agent_workflow():
"""Test agent using multiple tools"""
response = client.messages.create(
model="claude-sonnet-4-5",
tools=[tool1, tool2, tool3],
messages=[{
"role": "user",
"content": "Process order #12345"
}]
)
# Verify expected tool usage
tool_calls = extract_tool_calls(response)
assert "verify_order" in tool_calls
assert "process_payment" in tool_calls
# Use Claude's built-in evaluation
from anthropic import Anthropic
eval_client = Anthropic()
eval_results = eval_client.evaluate(
agent=my_agent,
test_cases=[
{"input": "...", "expected_output": "..."},
# More test cases
],
metrics=["accuracy", "latency", "tool_efficiency"]
)
async def research_agent(query: str):
"""Agent researches topic using multiple sources"""
response = await client.messages.create(
model="claude-sonnet-4-5",
tools=[web_search, web_fetch, summarize],
messages=[{
"role": "user",
"content": f"Research '{query}' and provide comprehensive summary"
}]
)
# Claude: searches → fetches articles → summarizes → synthesizes
return response.content
def code_agent(requirements: str):
"""Agent writes and tests code"""
response = client.messages.create(
model="claude-sonnet-4-5",
tools=[write_file, bash, read_file],
messages=[{
"role": "user",
"content": f"Write and test code for: {requirements}"
}]
)
# Claude: writes code → saves file → runs tests → fixes errors → retries
return response.content
def data_pipeline_agent(source: str, destination: str):
"""Agent ETL pipeline"""
response = client.messages.create(
model="claude-sonnet-4-5",
tools=[read_file, bash, postgres_insert],
messages=[{
"role": "user",
"content": f"Extract data from {source}, transform it, and load to {destination}"
}]
)
# Claude orchestrates full ETL
return response.content
Best For:
Characteristics:
Best For:
Characteristics:
from fastapi import FastAPI
from anthropic import Anthropic
app = FastAPI()
client = Anthropic()
@app.post("/agent/task")
async def run_agent_task(task: dict):
response = client.messages.create(
model="claude-sonnet-4-5",
tools=load_tools_for_task(task),
messages=[{
"role": "user",
"content": task["description"]
}]
)
return {"result": response.content}
from langchain_anthropic import ChatAnthropic
from langchain.agents import initialize_agent
llm = ChatAnthropic(model="claude-sonnet-4-5")
agent = initialize_agent(
tools=[tool1, tool2],
llm=llm,
agent_type="structured-chat-zero-shot-react-description"
)
result = agent.run("Complete this task")
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("claude-agent")
def run_agent_with_logging(task):
logger.info(f"Starting task: {task}")
response = client.messages.create(
model="claude-sonnet-4-5",
tools=tools,
messages=[{"role": "user", "content": task}]
)
logger.info(f"Tools used: {extract_tools(response)}")
logger.info(f"Token usage: {response.usage}")
return response
Use Claude SDK when:
Consider alternatives when:
Official Documentation:
GitHub:
This skill ensures you build powerful, autonomous agents using Claude's cutting-edge capabilities in 2025.
Weekly Installs
–
Repository
GitHub Stars
5
First Seen
–
Security Audits
agent-browser 浏览器自动化工具 - Vercel Labs 命令行网页操作与测试
147,400 周安装