Claude Agent SDK for Rust
Rust SDK for Claude Agent. A Rust implementation mirroring the Python Claude Agent SDK with idiomatic Rust patterns and best practices.
Status
✅ Feature Parity - v0.1.0 - Full feature parity with Python SDK
Completed Features
- ✅ Core error types with
thiserror - ✅ Comprehensive type system with newtypes for type safety
- ✅ Transport layer with subprocess CLI integration (lock-free architecture)
- ✅ Basic
query()function for simple queries - ✅ Message parsing with full message type support
- ✅ Builder pattern for options
- ✅ Control protocol handler (16 integration tests)
- ✅ ClaudeSDKClient for bidirectional communication (10 tests, working demo)
- ✅ Hook system - Fully integrated with automatic handling
- ✅ Permission callbacks - Fully integrated with automatic handling
- ✅ SDK MCP server - In-process custom tools with JSONRPC protocol
- ✅ Comprehensive test suite (70 tests: 42 unit + 28 doc, all passing)
- ✅ Full documentation with examples and API docs (6,200+ LOC)
Examples
simple_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 server
Installation
Prerequisites:
- Rust 1.75.0 or later
- Node.js
- Claude Code:
npm install -g @anthropic-ai/claude-code
Add to your Cargo.toml:
[]
= "0.1.0"
= { = "1", = ["full"] }
= "0.3"
Quick Start
use query;
use StreamExt;
async
Basic Usage: query()
query() is an async function for querying Claude Code. It returns a Stream of response messages.
use ;
use StreamExt;
async
With Options
use ;
let options = builder
.system_prompt
.max_turns
.permission_mode
.add_allowed_tool
.add_allowed_tool
.build;
let stream = query.await?;
Architecture
The SDK follows Rust best practices and idiomatic patterns:
Type Safety
- Newtypes for IDs (
SessionId,ToolName,RequestId) - Strong typing prevents mixing incompatible values
- Builder pattern for complex configuration
Error Handling
- Uses
thiserrorfor ergonomic error types - All errors are
Result<T, ClaudeError> - Rich error context with exit codes and stderr
Async/Await
- Built on
tokioruntime async-traitfor trait methods- Streams for message handling
Modularity
src/
├── 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
Features Comparison with Python SDK
| Feature | Rust | Python |
|---|---|---|
query() function |
✅ | ✅ |
ClaudeSDKClient |
✅ | ✅ |
| Custom Tools (SDK MCP) | ✅ | ✅ |
| Hooks | ✅ | ✅ |
| Permission callbacks | ✅ | ✅ |
| Type safety | ✅✅ | ✅ |
| Error handling | ✅✅ | ✅ |
| Documentation | ✅✅ | ✅ |
| Performance | ✅✅ | ✅ |
Legend: ✅ = Supported, ✅✅ = Enhanced implementation
Examples
Run examples with:
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 server
Development
Building
Testing
Linting
Documentation
Design Principles
This SDK follows principles from:
- Rust Security Handbook
- Rust Error Handling Guide
- Rust Ownership Best Practices
- Rust Newtype Pattern
- Hexagonal Architecture in Rust
Key principles applied:
- Type safety with newtypes
- Memory safety without unsafe code
- Clear error propagation with Result types
- Ownership patterns for resource management
- Builder pattern for complex types
- Trait abstraction for extensibility
Security
This SDK has undergone security hardening with multiple layers of protection:
- Environment variable filtering - Blocks dangerous variables like
LD_PRELOAD,PATH,NODE_OPTIONS - Argument validation - Allowlist for CLI flags prevents injection attacks
- Timeout protection - All I/O operations have configurable timeouts
- Buffer limits - Prevents memory exhaustion from unbounded growth
- Secure defaults - Conservative defaults for all security-sensitive options
- No unsafe code - 100% safe Rust with compiler guarantees
For details on security improvements, see:
fixes.md- Complete security audit reportSECURITY_FIXES_APPLIED.md- Implementation details
Contributing
This is a reference implementation demonstrating Rust SDK development best practices. See PLAN.md for the complete implementation roadmap.
License
MIT