Claude Agent SDK for Rust
Rust SDK for building AI agents powered by Claude Code. Mirrors the TypeScript Claude Agent SDK with idiomatic Rust patterns.
Installation
Prerequisites:
- Rust 1.85.0+ (edition 2024)
- Node.js
- Claude Code 2.0.75+:
npm install -g @anthropic-ai/claude-code
[]
= "0.2"
= { = "1", = ["rt-multi-thread", "macros"] }
Quick Start
use ;
async
Examples
# Core functionality
# Hooks and permissions
# Introspection and runtime
# Security
# OAuth authentication
# Structured output
# MCP (requires --features rmcp for mcp_server)
TypeScript SDK Parity
Hook Events
| Event | TypeScript | Rust SDK |
|---|---|---|
| PreToolUse | ✓ | ✓ |
| PostToolUse | ✓ | ✓ |
| PostToolUseFailure | ✓ | ✓ |
| Notification | ✓ | ✓ |
| UserPromptSubmit | ✓ | ✓ |
| SessionStart | ✓ | ✓ |
| SessionEnd | ✓ | ✓ |
| Stop | ✓ | ✓ |
| SubagentStart | ✓ | ✓ |
| SubagentStop | ✓ | ✓ |
| PreCompact | ✓ | ✓ |
| PermissionRequest | ✓ | ✓ |
Query/Client Methods
| Method | TypeScript | Rust SDK |
|---|---|---|
| interrupt() | ✓ | ✓ |
| setPermissionMode() | ✓ | ✓ |
| setModel() | ✓ | ✓ |
| setMaxThinkingTokens() | ✓ | ✓ |
| supportedCommands() | ✓ | ✓ |
| supportedModels() | ✓ | ✓ |
| mcpServerStatus() | ✓ | ✓ |
| accountInfo() | ✓ | ✓ |
Options
| Option | TypeScript | Rust SDK |
|---|---|---|
| allowedTools | ✓ | ✓ |
| disallowedTools | ✓ | ✓ |
| systemPrompt | ✓ | ✓ |
| mcpServers | ✓ | ✓ |
| permissionMode | ✓ | ✓ |
| canUseTool | ✓ | ✓ |
| hooks | ✓ | ✓ |
| agents | ✓ | ✓ |
| maxTurns | ✓ | ✓ |
| model | ✓ | ✓ |
| cwd | ✓ | ✓ |
| env | ✓ | ✓ |
| resume | ✓ | ✓ |
| forkSession | ✓ | ✓ |
| settingSources | ✓ | ✓ |
| maxBudgetUsd | ✓ | ✓ |
| maxThinkingTokens | ✓ | ✓ |
| fallbackModel | ✓ | ✓ |
| outputFormat | ✓ | ✓ |
| sandbox | ✓ | ✓ |
| plugins | ✓ | ✓ |
| betas | ✓ | ✓ |
| strictMcpConfig | ✓ | ✓ |
| resumeSessionAt | ✓ | ✓ |
| allowDangerouslySkipPermissions | ✓ | ✓ |
| pathToClaudeCodeExecutable | ✓ | ✓ |
| stderr | ✓ | ✓ |
| tools (preset) | ✓ | ✓ |
| enableFileCheckpointing | ✓ | ✓ |
| sessionId | ✓ | ✓ |
MCP Server Types
| Type | TypeScript | Rust SDK |
|---|---|---|
| stdio | ✓ | ✓ |
| sse | ✓ | ✓ |
| http | ✓ | ✓ |
| sdk (in-process) | ✓ | ✓ |
Result Message Fields
| Field | TypeScript | Rust SDK |
|---|---|---|
| modelUsage | ✓ | ✓ |
| permission_denials | ✓ | ✓ |
| structured_output | ✓ | ✓ |
| errors | ✓ | ✓ |
Development
Security
The SDK implements strict security measures:
- Environment variables: Dangerous vars (LD_PRELOAD, PATH, etc.) cause errors
- CLI arguments: Only allowlisted flags permitted, others rejected
- Session binding: Auto-binds on first Result, validates on send
- Buffer limits: Configurable max buffer size (default 1MB)
- 100% safe Rust: No unsafe code
See SECURITY.md for full documentation.
OAuth Authentication
For Claude Max/Pro subscribers, authenticate without API keys:
use OAuthClient;
let client = new?;
let token = client.authenticate.await?;
// Token is cached in platform-specific config directory
See oauth_demo example for full usage including status check and logout.
Documentation
- README_API.md - Full API reference
- SECURITY.md - Security documentation and threat model
License
MIT