mcpField
C
claude-agent-sdk

claude-agent-sdk

github.com/tdiminoAgent workflows4138 installsUpdated 1w ago
Repository

About claude-agent-sdk

Claude Agent SDK is a Python and TypeScript SDK for building agents with Claude Code tools, including file operations, commands, and web search. It supports one-shot queries and persistent client sessions, plus custom tools, hooks, permissions, and subagents.

Key features

  1. 01One-shot and continuous query modes
  2. 02File, shell, and web tools
  3. 03Custom tools and hooks
  4. 04Tool permission controls
  5. 05Subagent creation
  6. 06Python and TypeScript SDKs

Use cases

  1. 01Automating one-off file or coding tasks
  2. 02Building conversational agents with follow-ups
  3. 03Creating read-only code review agents
  4. 04Running research with web tools

MCP support

Agents can use MCP servers as custom tools. The SDK supports adding MCP servers to agent configurations.

Install

Copy the command and run it in a terminal.

$npx skills add https://github.com/tdimino/claude-code-minoan --skill claude-agent-sdk

After installing, type/claude-agent-sdkin Claude Code to confirm it loaded.

Bundled files

References and scripts installed along with SKILL.md.

README.md
references/
best-practices.md
browser-control.md
built-in-tools.md
custom-tools.md
hooks.md
message-types.md
permissions.md
python-sdk.md
subagents.md
troubleshooting.md
templates/
agent-with-tracking-python.py
continuous-chat-python.py
custom-tool-python.py
hook-example-python.py
research-agent-python.py
session-api-typescript.ts
simple-agent-python.py
workflows/
add-custom-tools.md
add-hooks.md
create-agent.md

Details

From the project README

<essential_principles>

Claude Agent SDK Overview

The Claude Agent SDK gives agents the same tools that power Claude Code: file operations, bash commands, web search, and more. Build autonomous agents that read files, run commands, search the web, edit code, and verify their work.

Two Ways to Query
FunctionSessionBest For
query()New each timeOne-off tasks, automation
ClaudeSDKClientContinuousConversations, follow-ups, hooks

Key Difference: query() is simpler but doesn't support hooks, interrupts, or custom tools. Use ClaudeSDKClient for advanced features.

Agent Loop Pattern

Agents operate in a feedback loop:

gather context → take action → verify work → repeat
Installation

Python:

uv add claude-agent-sdk

TypeScript:

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

Requirements:

  • Claude Code CLI installed (npm install -g @anthropic-ai/claude-code)
  • ANTHROPIC_API_KEY environment variable set
Quick Start
import asyncio
from claude_agent_sdk import query, ClaudeAgentOptions

async def main():
    async for message in query(
        prompt="Find and fix bugs in auth.py",
        options=ClaudeAgentOptions(allowed_tools=["Read", "Edit", "Bash"])
    ):
        print(message)

asyncio.run(main())

</essential_principles>

What would you like to do?

  1. Create a new agent - Build an agent from scratch
  2. Add custom tools - Create tools with @tool decorator or MCP servers
  3. Add hooks - Intercept tool calls for logging, validation, or modification
  4. Configure permissions - Control what tools can do
  5. Create subagents - Define specialized agents for parallel work
  6. Control browsers - Chrome extension, dev-browser skill
  7. Get API reference - Python or TypeScript SDK details

Wait for response before proceeding.

ResponseLoad
1, "create", "new", "build"workflows/create-agent.md
2, "tools", "custom", "@tool"references/custom-tools.md
3, "hooks", "intercept"references/hooks.md
4, "permissions", "can_use_tool"references/permissions.md
5, "subagent", "parallel"references/subagents.md
6, "browser", "chrome", "automation"references/browser-control.md
7, "python", "reference"references/python-sdk.md
7, "typescript", "reference"references/typescript-sdk.md
"built-in", "tools"references/built-in-tools.md
"message", "types"references/message-types.md
"best practices", "patterns"references/best-practices.md
"error", "troubleshoot"references/troubleshooting.md

<quick_reference>

ClaudeAgentOptions (Key Fields)

ClaudeAgentOptions(
    # Tools
    allowed_tools=["Read", "Write", "Bash"],  # Built-in tools to enable
    disallowed_tools=["WebSearch"],           # Tools to block

    # Prompts
    system_prompt="You are...",               # Custom system prompt
    # Or use preset: {"type": "preset", "preset": "claude_code", "append": "..."}

    # MCP Servers
    mcp_servers={"calc": my_server},          # MCP server configs

    # Permissions
    permission_mode="acceptEdits",            # default | acceptEdits | plan | bypassPermissions
    can_use_tool=my_handler,                  # Custom permission callback

    # Execution
    cwd="/path/to/project",                   # Working directory
    max_turns=10,                             # Limit iterations
    env={"API_KEY": "..."},                   # Environment variables

    # Advanced
    hooks={"PreToolUse": [...]},              # Behavior hooks
    agents={"researcher": AgentDef(...)},     # Subagents
    sandbox={"enabled": True},                # Sandbox settings
    setting_sources=["project"],              # Load .claude settings
)

Built-in Tools

ToolPurpose
ReadRead files (text, images, PDFs, notebooks)
WriteCreate new files
EditModify existing files with search/replace
BashRun terminal commands
GlobFind files by pattern (**/*.ts)
GrepSearch file contents with regex
WebSearchSearch the web
WebFetchFetch and parse web pages
TaskSpawn subagents
NotebookEditEdit Jupyter notebooks
TodoWriteManage task lists
KillShellKill background shells
ExitPlanModeExit planning mode

Message Types

# All messages
Message = UserMessage | AssistantMessage | SystemMessage | ResultMessage

# Content blocks in AssistantMessage
ContentBlock = TextBlock | ThinkingBlock | ToolUseBlock | ToolResultBlock

Error Types

from claude_agent_sdk import (
    CLINotFoundError,      # Claude Code CLI not installed
    CLIConnectionError,    # Connection failed
    ProcessError,          # CLI process failed
    CLIJSONDecodeError,    # JSON parsing failed
)

</quick_reference>

<thinking_config>

Thinking & Effort Configuration

Control agent thinking depth via the effort parameter rather than prompt-level instructions like "think carefully":

Use CaseEffortNotes
Quick lookups, simple editslowMinimal thinking overhead
Standard developmentmediumDefault for most agents
Complex architecture, securityhighDeeper reasoning
Deep research, long-horizonmaxMaximum thinking budget
# Adaptive thinking (recommended for Claude 4.6)
options = ClaudeAgentOptions(
    model="claude-opus-4-6",  # or claude-sonnet-4-6
    # Effort is controlled at the API level, not in the agent options
)

Avoid adding "think carefully" or "be thorough" to agent system prompts—Claude 4.6 calibrates thinking depth automatically based on task complexity.

</thinking_config>

Common Patterns

File Operations Agent
options = ClaudeAgentOptions(
    allowed_tools=["Read", "Write", "Edit", "Glob", "Grep"],
    permission_mode="acceptEdits",
    cwd="/path/to/project"
)
Code Review Agent
options = ClaudeAgentOptions(
    allowed_tools=["Read", "Glob", "Grep"],  # Read-only
    system_prompt="Review code for bugs, security issues, and style"
)
Research Agent
options = ClaudeAgentOptions(
    allowed_tools=["WebSearch", "WebFetch", "Write"],
    max_turns=20  # Allow more iterations for research
)
Interactive Chat with Tools
async with ClaudeSDKClient(options) as client:
    await client.query("What files are in this directory?")
    async for msg in client.receive_response():
        print(msg)

    # Follow-up - Claude remembers context
    await client.query("Show me the largest one")
    async for msg in client.receive_response():
        print(msg)

Comments

Markdown supported