Claude Code SDK for Rust
A Rust SDK for interacting with Claude Code CLI, providing both simple query interfaces and full interactive client capabilities with complete feature parity with the official Python SDK.
Features
- 🚀 Simple Query Interface - One-shot queries with the
query()function - 💬 Interactive Client - Stateful conversations with context retention
- 🔄 Streaming Support - Real-time message streaming
- 🛑 Interrupt Capability - Cancel ongoing operations
- 🔧 Full Configuration - Comprehensive options matching Python SDK
- 📦 Type Safety - Strongly typed with serde support
- âš¡ Async/Await - Built on Tokio for async operations
Python SDK Feature Parity
This Rust SDK provides 100% feature parity with the official Python SDK (claude_code_sdk), including:
- ✅ All client methods:
query(),send_message(),receive_response(),interrupt() - ✅ Interactive sessions: Full stateful conversation support
- ✅ Message streaming: Real-time async message handling
- ✅ All configuration options: System prompts, models, permissions, tools, etc.
- ✅ All message types: User, Assistant, System, Result messages
- ✅ Error handling: Comprehensive error types matching Python SDK
- ✅ Session management: Multi-session support with context isolation
The API is designed to be familiar to Python SDK users while leveraging Rust's type safety and performance benefits.
Installation
Add this to your Cargo.toml:
[]
= "0.1.6"
= { = "1.0", = ["full"] }
= "0.3"
Prerequisites
Install Claude Code CLI:
Quick Start
Simple Query (One-shot)
use ;
use StreamExt;
async
Interactive Client
use ;
async
Advanced Usage
use ;
async
Configuration Options
use ;
let options = builder
.system_prompt
.model
.permission_mode
.max_turns
.max_thinking_tokens
.allowed_tools
.cwd
// New in v0.1.6
.settings // Use custom settings file
.add_dir // Add additional working directories
.add_dirs // Add multiple dirs
.build;
API Reference
query()
Simple, stateless query function for one-shot interactions.
pub async
InteractiveClient
Main client for stateful, interactive conversations.
Methods
new(options: ClaudeCodeOptions) -> Result<Self>- Create a new clientconnect() -> Result<()>- Connect to Claude CLIsend_and_receive(prompt: String) -> Result<Vec<Message>>- Send message and wait for complete responsesend_message(prompt: String) -> Result<()>- Send message without waitingreceive_response() -> Result<Vec<Message>>- Receive messages until Result messageinterrupt() -> Result<()>- Cancel ongoing operationdisconnect() -> Result<()>- Disconnect from Claude CLI
Message Types
UserMessage- User input messagesAssistantMessage- Claude's responsesSystemMessage- System notificationsResultMessage- Operation results with timing and cost info
Error Handling
The SDK provides comprehensive error types:
CLINotFoundError- Claude Code CLI not installedCLIConnectionError- Connection failuresProcessError- CLI process errorsInvalidState- Invalid operation state
Examples
Check the examples/ directory for more usage examples:
interactive_demo.rs- Interactive conversation demoquery_simple.rs- Simple query examplefile_operations.rs- File manipulation example
New Features (v0.1.6)
Test the latest features with these examples:
test_settings.rs- Using custom settings filestest_settings_safe.rs- Safe settings file handling with path detectiontest_add_dirs.rs- Adding multiple working directoriestest_combined_features.rs- Combining settings and add_dirstest_new_options.rs- Testing the new builder methods
Example settings files are provided:
examples/claude-settings.json- Basic settings configurationexamples/custom-claude-settings.json- Advanced settings with MCP servers
Note: When running examples from the project root, use:
The settings files use relative paths from the project root (e.g., examples/claude-settings.json)
License
This project is licensed under the MIT License - see the LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.