Claude Agent SDK for Rust
π¦ Production-Ready Rust SDK for Claude Agent with type-safe, high-performance API.
The Claude Agent SDK for Rust provides comprehensive programmatic access to Claude's capabilities with zero-cost abstractions, compile-time memory safety, and true concurrent processing.
π Table of Contents
β¨ Features
- π Complete V2 API - Full TypeScript-inspired session-based API
- π Bidirectional Streaming - Real-time communication with Claude
- πͺ Hooks System - Intercept and control Claude's behavior
- π§ Skills System - Enhanced with validation, security audit, and progressive disclosure
- π€ Subagents - Full agent delegation and orchestration support
- π MCP Integration - Model Context Protocol server support
- β‘ Slash Commands - Command registration and execution framework
- π Observability - Comprehensive logging and metrics collection
- π‘οΈ Type Safety - Compile-time guarantees for agent configurations
- β‘ High Performance - Zero-cost abstractions and lock-free architecture
π Quick Start
Prerequisites
- Rust: 1.90 or higher
- Claude Code CLI: Version 2.0.0 or higher
- API Key:
ANTHROPIC_API_KEYenvironment variable set
Installation
Add to your Cargo.toml:
[]
= "0.1"
= { = "1", = ["full"] }
Basic Usage
use ;
async
π§ Core APIs
The SDK provides four main API styles for different use cases:
1. Simple Query API
Best for: One-shot queries, quick prototypes, simple use cases
use ;
async
Key Functions:
query(prompt, options)- Collect all messages into a Vecquery_with_content(content_blocks, options)- Send structured content (images + text)
2. Streaming API
Best for: Memory-efficient processing, real-time responses
use ;
use StreamExt;
async
Key Functions:
query_stream(prompt, options)- Returns a stream of messagesquery_stream_with_content(content_blocks, options)- Stream with structured content
3. Bidirectional Client
Best for: Full control, multi-turn conversations, dynamic control flow
use ;
use StreamExt;
async
4. V2 Session API
Best for: TypeScript-style sessions, clean send/receive pattern
use ;
async
ποΈ Advanced Features
Hooks System
Hooks allow you to intercept and control Claude's behavior at 8 key points in the execution lifecycle.
Available Hooks
| Hook Type | Description | Use Case |
|---|---|---|
PreToolUse |
Before tool execution | Log/modify tool usage |
PostToolUse |
After tool execution | Process tool results |
PreMessage |
Before sending message | Filter/transform messages |
PostMessage |
After receiving message | Log incoming messages |
PromptStart |
When prompt starts | Initialize context |
PromptEnd |
When prompt ends | Cleanup context |
SubagentStop |
When subagent stops | Process subagent results |
PreCompact |
Before conversation compaction | Preserve important context |
Example: Pre-Tool Hook
use ;
use Arc;
let pre_tool_hook = ;
let hooks = vec!;
let options = default
.hooks
.build?;
Example: Post-Message Hook
let post_message_hook = ;
let hooks = vec!;
Hook Context
All hooks receive a context object with:
Skills System
The Skills System provides enhanced capabilities with validation, security auditing, and progressive disclosure.
use ;
// Load and validate SKILL.md
let validator = new;
let skill_file = load?;
let result = validator.validate?;
// Audit for security risks
let auditor = new;
let audit = auditor.audit_skill?;
if audit.has_risky_patterns
MCP Integration
Support for Model Context Protocol (MCP) servers and tools.
use ;
// Define tool handler and create server
let my_tool = tool!;
let server = create_sdk_mcp_server;
Subagents
Delegate tasks to specialized sub-agents.
use ;
use SequentialOrchestrator;
let researcher = new;
let mut registry = new;
registry.register.await?;
Multimodal Support
Query with images and text.
use ;
let image_data = read?;
let base64_image = encode;
let messages = query_with_content.await?;
Examples
Check out the examples/ directory for complete working examples:
π€ Contributing
Contributions are welcome! Please see CONTRIBUTING.md for guidelines.
License
This project is licensed under the MIT License - see the LICENSE.md file for details.