claude-code-sdk-rust
An async Rust SDK for the Claude Code CLI.
Crate name vs. import path: this crate is published on crates.io as
claude-code-sdk-rust, but its library import path isclaude_agent_sdk. Addclaude-code-sdk-rustto yourCargo.tomland writeuse claude_agent_sdk::...;in your code.
This crate provides a Tokio-native SDK for driving an authenticated local claude CLI process with typed options, message parsing, interactive sessions, control requests, SDK MCP servers, and local/session-store helpers. It is modeled after the Python claude-agent-sdk API while using Rust-native async, traits, and type-safe data structures.
Status
This repository is an SDK implementation, not an official Anthropic package. It targets parity with the Python Claude Agent SDK public behavior and is verified by the local test suite.
Verification:
The normal test suite avoids paid/authenticated CLI calls. The ignored e2e_cli smoke test can be run separately when the Claude CLI is authenticated.
Requirements
- Rust 1.70+
- Tokio runtime
- Claude Code CLI installed and authenticated
claudeavailable onPATH, or set viaClaudeAgentOptions::builder().cli_path(...)
Installation
Add the crate from crates.io:
[]
= "0.1"
= { = "1", = ["full"] }
Then import it through its library path, claude_agent_sdk:
use query;
Optional OpenTelemetry propagation support:
[]
= { = "0.1", = ["otel"] }
Or track the development branch directly from Git:
[]
= { = "https://github.com/PandelisZ/claude-agent-sdk-rust" }
Quick start
One-shot query
use query;
async
Full message sequence
use query_messages;
async
Streamed one-shot input
use query_stream_messages;
async
Interactive client
The interactive client mirrors the Python SDK's explicit connection model: create the client, call connect(), send prompts or control requests, receive messages, then disconnect() or close().
use ;
async
Convenience connection helpers are also available:
client.connect_with_prompt.await?;
client.connect_with_stream.await?;
Streaming responses
use ;
async
Configuration
Use ClaudeAgentOptions::builder() for ergonomic configuration.
use ;
use HashMap;
let mut env = new;
env.insert;
let options = builder
.cwd
.model
.system_prompt
.allowed_tools
.permission_mode
.setting_sources
.skills
.env
.mcp_server
.build;
Supported option areas include:
- CLI path, working directory, environment, extra CLI args
- model, fallback model, betas, thinking config, task budgets
- system prompt presets/files, dynamic prompt controls, skills config
- allowed/disallowed tools and permission mode
can_use_toolcallback support for streaming/client mode- hooks and hook event inclusion
- stdio, SSE, HTTP, raw/path MCP server config
- in-process SDK MCP servers
- sandbox settings
- session resume, continue, fork, and session-store mirroring
- stderr callback
- optional OpenTelemetry context propagation with the
otelfeature
SDK MCP servers
The crate can host in-process SDK MCP tools and bridge them through Claude Code control requests.
use ;
let server = create_sdk_mcp_server;
let options = builder
.sdk_mcp_server
.build;
Use create_sdk_mcp_server_with_version and tool_with_annotations when the CLI needs server version metadata or MCP tool annotations.
Permission callbacks
use ;
let options = builder
.can_use_tool
.build;
can_use_tool requires streaming/client mode. String-based one-shot query_messages(...) rejects this callback because there is no live bidirectional control channel after spawning the subprocess.
Hooks
Hooks are represented as callback matchers grouped by event name.
use ;
let callback = new;
let options = builder
.hook
.include_hook_events
.build;
Sessions
Local Claude transcript helpers are available from the crate root and from claude_agent_sdk::sessions.
use ;
async
Session helpers include:
- list, inspect, page, and delete local sessions
- list and read subagent transcripts
- rename and tag sessions by appending metadata events
- fork sessions with remapped IDs
- import local transcripts into a custom session store
- store-backed list/read/mutate/fork helpers
Custom session stores
Implement SessionStore for external persistence. Stores can opt into session listing by overriding supports_list_sessions() and list_sessions(...).
use async_trait;
use ;
;
Error handling
Most functions return claude_agent_sdk::Result<T>, an alias for Result<T, ClaudeSDKError>.
Main error variants include:
CLIConnectionErrorCLINotFoundErrorProcessErrorCLIJSONDecodeErrorMessageParseError- generic SDK/configuration errors
Testing
Run the standard suite:
Run feature verification:
Run the ignored real CLI smoke test only when authenticated and prepared for possible API usage:
Crate layout
client- interactive clientquery- one-shot query helperstypes- public SDK data typesoptions- builder APImcp- SDK MCP server/tool helperssession_store- custom store trait and in-memory storesessions- local and store-backed session helpersinternal- CLI args, transport, protocol, control, parsing, mirroring
License
MIT