| Crates.io | claude-agent-sdk |
| lib.rs | claude-agent-sdk |
| version | 0.1.1 |
| created_at | 2025-09-30 20:41:13.418047+00 |
| updated_at | 2025-09-30 21:10:18.463618+00 |
| description | Idiomatic Rust SDK for building AI agents powered by Claude Code with full async support, type safety, and security hardening |
| homepage | |
| repository | https://github.com/anthropics/claude-agent-sdk-rust |
| max_upload_size | |
| id | 1861709 |
| size | 286,384 |
Rust SDK for Claude Agent. A Rust implementation mirroring the Python Claude Agent SDK with idiomatic Rust patterns and best practices.
✅ Feature Parity - v0.1.0 - Full feature parity with Python SDK
thiserrorquery() function for simple queriessimple_query.rs - Basic query usageinteractive_client.rs - Interactive conversationbidirectional_demo.rs - Concurrent operations demonstrationhooks_demo.rs - Hook system with 3 examplespermissions_demo.rs - Permission system with 3 examplesmcp_demo.rs - Custom tools with SDK MCP serverPrerequisites:
npm install -g @anthropic-ai/claude-codeAdd to your Cargo.toml:
[dependencies]
claude-agent-sdk = "0.1.0"
tokio = { version = "1", features = ["full"] }
futures = "0.3"
use claude_agent_sdk::query;
use futures::StreamExt;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let stream = query("What is 2 + 2?", None).await?;
let mut stream = Box::pin(stream);
while let Some(message) = stream.next().await {
println!("{:?}", message?);
}
Ok(())
}
query() is an async function for querying Claude Code. It returns a Stream of response messages.
use claude_agent_sdk::{query, ClaudeAgentOptions, Message, ContentBlock};
use futures::StreamExt;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Simple query
let stream = query("Hello Claude", None).await?;
let mut stream = Box::pin(stream);
while let Some(message) = stream.next().await {
match message? {
Message::Assistant { message, .. } => {
for block in &message.content {
if let ContentBlock::Text { text } = block {
println!("Claude: {}", text);
}
}
}
Message::Result { total_cost_usd, .. } => {
if let Some(cost) = total_cost_usd {
println!("Cost: ${:.4}", cost);
}
}
_ => {}
}
}
Ok(())
}
use claude_agent_sdk::{query, ClaudeAgentOptions, PermissionMode};
let options = ClaudeAgentOptions::builder()
.system_prompt("You are a helpful coding assistant")
.max_turns(5)
.permission_mode(PermissionMode::AcceptEdits)
.add_allowed_tool("Read")
.add_allowed_tool("Write")
.build();
let stream = query("Create a hello.py file", Some(options)).await?;
The SDK follows Rust best practices and idiomatic patterns:
SessionId, ToolName, RequestId)thiserror for ergonomic error typesResult<T, ClaudeError>tokio runtimeasync-trait for trait methodssrc/
├── error.rs # Error types
├── types.rs # Type definitions
├── transport/ # Communication layer
│ ├── mod.rs # Transport trait
│ └── subprocess.rs # CLI subprocess
├── message/ # Message parsing
├── query.rs # Simple query function
└── lib.rs # Public API
| Feature | Rust | Python |
|---|---|---|
query() function |
✅ | ✅ |
ClaudeSDKClient |
✅ | ✅ |
| Custom Tools (SDK MCP) | ✅ | ✅ |
| Hooks | ✅ | ✅ |
| Permission callbacks | ✅ | ✅ |
| Type safety | ✅✅ | ✅ |
| Error handling | ✅✅ | ✅ |
| Documentation | ✅✅ | ✅ |
| Performance | ✅✅ | ✅ |
Legend: ✅ = Supported, ✅✅ = Enhanced implementation
Run examples with:
cargo run --example simple_query
See examples/ directory for more:
simple_query.rs - Basic usage with optionsinteractive_client.rs - Interactive conversationbidirectional_demo.rs - Concurrent operations demonstrationhooks_demo.rs - Hook system with 3 examplespermissions_demo.rs - Permission system with 3 examplesmcp_demo.rs - Custom tools with SDK MCP servercargo build
cargo test
cargo clippy
cargo fmt
cargo doc --open
This SDK follows principles from:
Key principles applied:
This SDK has undergone security hardening with multiple layers of protection:
LD_PRELOAD, PATH, NODE_OPTIONSFor details on security improvements, see:
fixes.md - Complete security audit reportSECURITY_FIXES_APPLIED.md - Implementation detailsThis is a reference implementation demonstrating Rust SDK development best practices. See PLAN.md for the complete implementation roadmap.
MIT