claude-sdk-rs ๐ฆ
A type-safe, async-first Rust SDK that wraps the Claude Code CLI to provide a powerful programmatic API for interacting with Claude AI. Build AI-powered applications with confidence using Rust's safety guarantees.
๐ Table of Contents
- Key Features
- Installation
- Quick Start
- Documentation
- Usage Examples
- Architecture
- Examples
- Contributing
- Performance
- Security
- Requirements
- License
โจ Key Features
- ๐ Type-Safe API - Strongly typed requests and responses with compile-time guarantees
- โก Async/Await - Built on Tokio for efficient concurrent operations
- ๐ Multiple Response Modes - Simple text, full metadata, or streaming responses
- ๐พ Session Management - Persistent conversations with automatic context preservation
- ๐ ๏ธ Tool Integration - Support for MCP (Model Context Protocol) tools and external services
- ๐ Rich Metadata - Access token usage, costs, session IDs, and timing information
- ๐ฏ Comprehensive Error Handling - Detailed error types with actionable messages
- โ๏ธ Flexible Configuration - Builder patterns for intuitive setup
- ๐ Granular Permissions - Fine-grained tool access control with
Bash(command)and MCP support - ๐ฏ Conversation Control - Limit turns and extend system prompts dynamically
- ๐ก๏ธ Security Features - Configurable permission prompts and tool restrictions
๐ฆ Installation
Prerequisites
-
Install Claude Code CLI (required for SDK operation):
# Install via npm (recommended) # Or install via Homebrew on macOS # Authenticate with your Claude account -
Verify Installation:
Add to Your Project
Add claude-sdk-rs to your Cargo.toml:
[]
= "1.0"
= { = "1.40", = ["full"] }
Feature Flags
The SDK uses feature flags to provide only the functionality you need:
# Core SDK only (default) - minimal dependencies
= "1.0"
# With CLI binary - adds command-line interface
= { = "1.0", = ["cli"] }
# With analytics - usage metrics and performance tracking
= { = "1.0", = ["analytics"] }
# With MCP support - Model Context Protocol for tools
= { = "1.0", = ["mcp"] }
# With SQLite storage - persistent session management
= { = "1.0", = ["sqlite"] }
# Everything enabled - all features
= { = "1.0", = ["full"] }
Install CLI Binary
To install the claude-sdk-rs CLI tool globally:
๐ Quick Start
Basic Usage
use ;
async
๐ Documentation
- ๐ Quick Start Guide - Get up and running in minutes
- ๐ง Development Setup - Build from source and contribute
- ๐ API Documentation - Complete API reference
- ๐ Examples - Working code examples
- ๐๏ธ Architecture - Technical design and internals
- ๐ NVM Compatibility - Using with Node Version Manager
๐ก Usage Examples
API Patterns: Two Ways to Send Queries
The SDK provides two patterns for sending queries to Claude:
1. Builder Pattern (Recommended for new code)
let response = client.query.send.await?;
let full_response = client.query.send_full.await?;
let stream = client.query.stream.await?;
2. Direct Methods (Backward compatible)
let response = client.send.await?;
let full_response = client.send_full.await?;
When to use each pattern:
- Use Builder Pattern for new applications - it's more flexible and supports additional configuration per query
- Use Direct Methods for simple use cases or when migrating existing code
- The builder pattern allows per-query customization (session IDs, output formats, etc.)
Custom Configuration
use ;
let client = builder
.model
.system_prompt
.timeout_secs
.stream_format
.build;
Enhanced Configuration Options
The SDK now supports advanced configuration features for better control:
use ;
// Configure with system prompt extension and tool permissions
let client = builder
.append_system_prompt
.max_turns // Limit conversation turns
.disallowed_tools
.skip_permissions // Require permission prompts
.build;
// Tool-specific permissions
let tools_client = builder
.allowed_tools
.build;
Get Full Response Metadata
// Get response with metadata
let response = client
.query
.send_full
.await?;
println!;
if let Some = response.metadata
Streaming Responses
use StreamExt;
let mut stream = client
.query
.stream
.await?;
while let Some = stream.next.await
Session Management
// Sessions are automatically managed - context is preserved
let client = builder
.stream_format
.build;
// First message
let response1 = client
.query
.send_full
.await?;
// Claude remembers the context
let response2 = client
.query
.send
.await?;
// Response: "Based on our conversation, your favorite programming language is Rust!"
Error Handling
use Error;
match client.query.send.await
๐๏ธ Architecture
The SDK is built as a single crate with modular organization and feature flags:
claude-sdk-rs/
โโโ src/
โ โโโ lib.rs # Main SDK public API
โ โโโ core/ # Core types, config, errors
โ โโโ runtime/ # Process execution and streaming
โ โโโ mcp/ # Model Context Protocol (feature: mcp)
โ โโโ cli/ # CLI interface (feature: cli)
โโโ examples/ # Working examples
โโโ tests/ # Integration tests
โโโ benches/ # Performance benchmarks
Feature Flags
- Default: Core SDK functionality with no extra dependencies
cli: Adds command-line interface and interactive featuresmcp: Enables Model Context Protocol for tool integrationsqlite: Adds SQLite-based session persistenceanalytics: Enables usage analytics (requirescli)full: Enables all features
๐งช Examples
Explore the examples/ directory for complete working examples:
basic_usage.rs- Simple queries and configurationstreaming.rs- Real-time streaming responsessession_management.rs- Multi-turn conversationserror_handling.rs- Comprehensive error handlingconfiguration.rs- Advanced configuration optionssystem_prompts.rs- System prompt extension and conversation control NEWadvanced_permissions.rs- Granular tool permissions and security NEWsession_persistence.rs- SQLite-based session storage (requiressqlitefeature)cli_interactive.rs- Interactive CLI usage (requiresclifeature)
Run examples:
# Basic examples
# New features - advanced configuration
# Examples requiring features
๐ค Contributing
We welcome contributions! Please see our Contributing Guide for details.
Development
# Clone the repository
# Build the crate
# Build with all features
# Run tests
# Run tests with all features
# Run linter
# Format code
# Run benchmarks
๐ Performance
The SDK is designed for minimal overhead:
- Zero-cost abstractions over the Claude CLI
- Efficient streaming with backpressure handling
- Connection pooling for concurrent requests
- Optimized JSON parsing with
serde
๐ Security
- Never logs sensitive data or API responses
- Secure process execution with proper isolation
- Input validation and sanitization
- See SECURITY.md for security policy
๐ Requirements
- Rust: 1.70 or later
- Claude Code CLI: Must be installed and authenticated
- Operating Systems: Linux, macOS, Windows
- Architecture: x86_64, ARM64
๐ License
This project is licensed under the MIT License - see the LICENSE file for details.
๐ Links
๐ Quick Start โข ๐ง Dev Setup โข ๐ API Docs โข ๐ฌ Discussions
Made with โค๏ธ for the Rust community