A TypeScript SDK for building custom Agent workflows with Claude Code CLI. This open-source project provides an easy-to-use interface for integrating Claude AI capabilities into your TypeScript applications.
Choose your language: English | 中文 | 日本語 | 한국어
Claude Agent SDK TS enables you to:
- Bi-directional streaming communication with Claude Code via the
ClaudeAgentSDKClient, supporting continuous message exchange - Subprocess-based CLI transport (
SubprocessCLITransport) for hosting Claude Code CLI with full parameter, working directory, and environment variable support - Dynamic runtime configuration with
setPermissionModeandsetModelfor adjusting permissions and model versions mid-session - Type-safe abstractions with TypeScript types for permissions, Hook callbacks, MCP configuration, and more
- Comprehensive test coverage using Vitest to ensure critical control request logic is correct
This project is based on the following official resources:
- Agent SDK Overview Documentation
- Official Python SDK Repository
- Python SDK PR #171 - Permission Modes & Model Switching
npm install claude-agent-sdk-ts- Node.js 18 or higher
- Claude Code CLI installed:
npm install -g @anthropic-ai/claude-code
import { ClaudeAgentSDKClient } from "claude-agent-sdk-ts";
const client = new ClaudeAgentSDKClient();
await client.connect();
// Set permissions and model
await client.setPermissionMode("acceptEdits");
await client.setModel("claude-sonnet-4.1");
// Send a query
await client.query("Please review this TypeScript function implementation");
// Receive streaming responses
for await (const message of client.receiveMessages()) {
console.log(message);
}The main client class for interacting with Claude Code:
// Create client with custom configuration
const client = new ClaudeAgentSDKClient({
cliPath: "claude-code",
workingDirectory: "/path/to/project",
env: { /* custom environment variables */ }
});
// Connect to Claude Code
await client.connect();
// Send user message and get streaming response
await client.query("Your message here");
// Iterate through streaming messages
for await (const msg of client.receiveMessages()) {
// Handle each message
}
// Dynamic configuration
await client.setPermissionMode("blockAll"); // or "acceptAll", "acceptEdits"
await client.setModel("claude-opus-4");Handles subprocess communication with Claude Code CLI:
- Manages CLI process lifecycle
- Handles standard I/O streams
- Supports environment variable injection
- Provides error handling and timeout management
Implements the control protocol for:
- Control request/response handling
- Hook callbacks for tool permissions and user interaction
- MCP (Model Context Protocol) configuration
- Runtime state management
claude-agent-sdk-ts/
├── src/
│ ├── client.ts # Main SDK client
│ ├── transport.ts # CLI subprocess transport layer
│ ├── query.ts # Control protocol implementation
│ ├── types.ts # TypeScript type definitions
│ ├── errors.ts # Custom error classes
│ ├── index.ts # Package exports
│ ├── version.ts # Version information
│ └── utils/
│ └── asyncQueue.ts # Async queue utilities
├── tests/
│ └── ... # Vitest unit tests
├── package.json
├── tsconfig.json
└── README.md
Key TypeScript types provided by the SDK:
// Permission modes
type PermissionMode = "blockAll" | "acceptEdits" | "acceptAll";
// Message types
interface ControlRequest {
type: string;
// ... additional fields
}
interface ControlResponse {
type: string;
// ... additional fields
}
// Hook definitions
interface HookCallback {
type: "permission" | "interaction";
// ... hook-specific data
}connect(): Promise<void>- Establish connection to Claude Codequery(message: string): Promise<void>- Send a user messagereceiveMessages(): AsyncIterable<any>- Get streaming message iteratorsetPermissionMode(mode: PermissionMode): Promise<void>- Update permissionssetModel(model: string): Promise<void>- Switch AI modeldisconnect(): Promise<void>- Close connection
npm run buildnpm run lintnpm testnpm run cleanFor Claude Code CLI integration:
# Install Claude Code CLI globally
npm install -g @anthropic-ai/claude-code
# Verify installation
claude-code --versionconst client = new ClaudeAgentSDKClient({
cliPath: "claude-code", // Path to Claude Code CLI
workingDirectory: process.cwd(), // Working directory for CLI
env: { ...process.env } // Environment variables
});blockAll- Block all tool execution (most restrictive)acceptEdits- Allow file edits and basic operationsacceptAll- Allow all operations (most permissive)
claude-opus-4- Most capable modelclaude-sonnet-4.1- Fast and efficientclaude-haiku-4.5-20251001- Lightweight
const client = new ClaudeAgentSDKClient();
await client.connect();
await client.setPermissionMode("acceptEdits");
await client.query("Review my TypeScript code and suggest improvements");
for await (const msg of client.receiveMessages()) {
console.log(msg);
}const client = new ClaudeAgentSDKClient({
workingDirectory: "/path/to/project"
});
await client.connect();
await client.setModel("claude-opus-4");
await client.query("Analyze the test coverage of this project");
for await (const msg of client.receiveMessages()) {
console.log(msg);
}const client = new ClaudeAgentSDKClient();
await client.connect();
// Start restrictive
await client.setPermissionMode("blockAll");
await client.query("List files (read-only)");
// Then allow edits
await client.setPermissionMode("acceptEdits");
await client.query("Create a new test file");try {
const client = new ClaudeAgentSDKClient();
await client.connect();
await client.query("Your message");
for await (const msg of client.receiveMessages()) {
// Process message
}
} catch (error) {
if (error instanceof ClaudeAgentSDKError) {
console.error("SDK Error:", error.message);
} else {
console.error("Unexpected error:", error);
}
}Contributions are welcome! Please feel free to submit a Pull Request. When contributing:
- Follow the existing code style
- Add tests for new functionality
- Update documentation as needed
- Ensure all tests pass:
npm test - Lint your code:
npm run lint
# Clone the repository
git clone https://github.com/anthropics/claude-agent-sdk-ts.git
cd claude-agent-sdk-ts
# Install dependencies
npm install
# Make your changes
# ...
# Run tests
npm test
# Build
npm run build
# Submit a pull requestThis project is licensed under the MIT License. See the LICENSE file for details.
We encourage community contributions and extensions of advanced features (such as deep Hook/MCP support, message parsers, etc.) while complying with the official terms of use.
For issues, questions, or suggestions:
- Open an Issue
- Check existing discussions
- Read the Claude documentation
See CHANGELOG.md for version history and updates.
Made with ❤️ for the open-source community by Anthropic