claude-code-rs
Rust rewrite of the official Anthropic Claude Agent SDK (Python).
Wraps the Claude Code CLI via subprocess with a bidirectional JSON streaming protocol. Full async, typed messages, hooks, permissions, and in-process MCP server support.
Requirements
- Claude Code CLI >= 2.0.0 installed and authenticated
- Rust 1.70+
Quick Start
[]
= "0.1"
= { = "1", = ["full"] }
use ;
async
APIs
One-shot query
// Stream messages
let mut stream = query.await?;
// Collect all messages
let messages = query_collect.await?;
// Get text only
let text = query_text.await?;
Stateful client (multi-turn)
use ;
use StreamExt;
let mut client = new;
client.connect.await?;
client.query.await?;
let messages = client.receive_response.await?;
client.query.await?;
let messages = client.receive_response.await?;
client.disconnect.await?;
Hooks
use *;
use HookInput;
let hook = HookDefinition ;
let options = ClaudeAgentOptions ;
In-process MCP tools
use *;
let tool = new_tool;
let server = create_sdk_mcp_server;
let mut client = new;
client.add_mcp_server;
Architecture
Mirrors the Python SDK's architecture:
| Layer | Python | Rust |
|---|---|---|
| Transport | SubprocessCLITransport |
SubprocessTransport + TransportWriter |
| Protocol | Query |
Query (spawn_router task) |
| Messages | dataclasses | enums + serde |
| One-shot API | query() |
query() / query_text() / query_collect() |
| Stateful API | ClaudeSDKClient |
ClaudeSDKClient |
| MCP | SdkMcpServer |
SdkMcpServer + JSONRPC router |
All communication is newline-delimited JSON over stdin/stdout with a bidirectional control protocol for hooks, permissions, MCP routing, and interrupts.
Examples
License
MIT - same as the original Python SDK.