claude-cli-sdk
Rust SDK for programmatic interaction with the Claude Code CLI.
Disclaimer: This is an unofficial, community-developed SDK and is not affiliated with, endorsed by, or sponsored by Anthropic, PBC. "Claude" and "Claude Code" are trademarks of Anthropic. This crate interacts with the Claude Code CLI but does not contain any Anthropic proprietary code.
Provides strongly-typed, async-first access to Claude Code sessions via the CLI's NDJSON stdio protocol. Supports one-shot queries, streaming responses, multi-turn sessions, permission callbacks, lifecycle hooks, and MCP server configuration.
Platform support: macOS, Linux, and Windows.
Features
query()— one-shot: send a prompt, collect all response messages.query_stream()— streaming: yield messages as they arrive.Client— stateful multi-turn sessions withsend()andreceive_messages().- Permission callbacks —
CanUseToolCallbackfor programmatic tool approval. - Lifecycle hooks —
HookMatcheronPreToolUse/PostToolUse/Stopevents. - MCP server config — attach external MCP servers to a session.
- Message callback — observe or filter messages before they reach your code.
testingfeature —MockTransport,ScenarioBuilder, and message builders for unit tests without a live CLI.
Quick Start
Add to Cargo.toml:
[]
= "0.1"
= { = "1", = ["macros", "rt-multi-thread"] }
One-shot query
use ;
async
Streaming query
use ;
use StreamExt;
async
Multi-turn session
use ;
use StreamExt;
async
Permission callback
use ;
use Arc;
let config = builder
.prompt
.permission_mode
.can_use_tool
.build;
ClientConfig Options
| Field | Type | Description |
|---|---|---|
prompt |
String |
Required. Initial prompt text. |
model |
Option<String> |
Model name, e.g. "claude-opus-4-6". |
cwd |
Option<PathBuf> |
Working directory for the CLI process. |
max_turns |
Option<u32> |
Maximum agentic turns before stopping. |
max_budget_usd |
Option<f64> |
Cost cap for the session. |
permission_mode |
PermissionMode |
Default, AcceptEdits, Plan, or BypassPermissions. |
can_use_tool |
Option<CanUseToolCallback> |
Per-tool permission callback. |
system_prompt |
Option<SystemPrompt> |
Text or preset system prompt. |
allowed_tools |
Vec<String> |
Allowlist of tool names. |
disallowed_tools |
Vec<String> |
Blocklist of tool names. |
mcp_servers |
McpServers |
External MCP server definitions. |
hooks |
Vec<HookMatcher> |
Lifecycle hook registrations. |
resume |
Option<String> |
Resume an existing session by ID. |
verbose |
bool |
Enable verbose CLI output. |
connect_timeout |
Option<Duration> |
Deadline for spawn + init (default: 30s). |
close_timeout |
Option<Duration> |
Deadline for graceful shutdown (default: 10s). |
read_timeout |
Option<Duration> |
Per-message recv deadline (default: None). |
default_hook_timeout |
Duration |
Hook callback fallback timeout (default: 30s). |
version_check_timeout |
Option<Duration> |
--version check deadline (default: 5s). |
Timeout configuration
use Duration;
use ClientConfig;
let config = builder
.prompt
.connect_timeout // Wait up to 60s for init
.read_timeout // Detect hung processes after 5min
.close_timeout // Kill after 5s on close
.default_hook_timeout // Hook callbacks get 10s
.build;
Set any Option<Duration> timeout to None to wait indefinitely.
Lifecycle hooks
Register callbacks for tool execution events:
use ;
use Arc;
let config = builder
.prompt
.hooks
.build;
MCP server configuration
Attach external MCP servers to a session:
use ;
let mut servers = new;
servers.insert;
let config = builder
.prompt
.mcp_servers
.build;
Testing with MockTransport
Enable the testing feature for unit tests without a live CLI:
[]
= { = "0.1", = ["testing"] }
use Arc;
use Client;
use ClientConfig;
use ;
let transport = new
.exchange
.build;
let transport = new;
let mut client = with_transport.unwrap;
Feature Flags
| Feature | Description |
|---|---|
testing |
MockTransport, ScenarioBuilder, and message builder helpers for unit tests |
efficiency |
Reserved for future throughput optimizations |
integration |
Integration test helpers (requires a live CLI) |
License
Licensed under either of Apache License, Version 2.0 or MIT License at your option.