claude-agents-sdk 0.1.7

Rust SDK for building agents with Claude Code CLI
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
# claude-agents-sdk

A Rust SDK for building agents that interact with the Claude Code CLI.

Inspired by the [Python Claude Agent SDK](https://github.com/anthropics/claude-agent-sdk), providing similar functionality with idiomatic Rust APIs.

## Features

- **Simple Query API**: One-shot queries with `query()` function
- **Streaming Client**: Full bidirectional `ClaudeClient` for complex interactions
- **Tool Permissions**: Control which tools Claude can use with callbacks
- **Hooks**: Register callbacks for various lifecycle events
- **MCP Tools**: Define custom tools that run in-process
- **Type Safety**: Strongly-typed messages, content blocks, and options
- **Async/Await**: Built on Tokio for efficient async operations

## Installation

Add to your `Cargo.toml`:

```toml
[dependencies]
claude-agents-sdk = "0.1"
tokio = { version = "1", features = ["full"] }
tokio-stream = "0.1"
```

For MCP tool support:

```toml
[dependencies]
claude-agents-sdk = { version = "0.1", features = ["mcp"] }
```

## Prerequisites

- [Claude Code CLI]https://docs.anthropic.com/en/docs/claude-code installed and authenticated
- Rust 1.75 or later

## Quick Start

### Simple Query

```rust
use claude_agents_sdk::{query, ClaudeAgentOptions, Message};
use tokio_stream::StreamExt;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let options = ClaudeAgentOptions::new()
        .with_max_turns(3);

    let mut stream = query("What is 2 + 2?", Some(options), None).await?;

    while let Some(message) = stream.next().await {
        match message? {
            Message::Assistant(msg) => print!("{}", msg.text()),
            Message::Result(result) => {
                println!("\nCost: ${:.4}", result.total_cost_usd.unwrap_or(0.0));
            }
            _ => {}
        }
    }

    Ok(())
}
```

### Streaming Client

```rust
use claude_agents_sdk::{ClaudeClient, ClaudeAgentOptions};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut client = ClaudeClient::new(None, None);
    client.connect().await?;

    // First query
    client.query("What is the capital of France?").await?;
    let (response, _) = client.receive_response().await?;
    println!("Response: {}", response);

    // Follow-up query
    client.query("What's its population?").await?;
    let (response, _) = client.receive_response().await?;
    println!("Response: {}", response);

    client.disconnect().await?;
    Ok(())
}
```

### With Tool Permissions

```rust
use claude_agents_sdk::{ClaudeClientBuilder, PermissionResult, PermissionMode};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut client = ClaudeClientBuilder::new()
        .permission_mode(PermissionMode::Default)
        .can_use_tool(|tool_name, input, _ctx| async move {
            println!("Tool requested: {} with {:?}", tool_name, input);

            // Allow Read, deny dangerous Bash commands
            if tool_name == "Bash" {
                if let Some(cmd) = input.get("command").and_then(|v| v.as_str()) {
                    if cmd.contains("rm -rf") {
                        return PermissionResult::deny_with_message("Dangerous command");
                    }
                }
            }

            PermissionResult::allow()
        })
        .build();

    client.connect().await?;
    // ... use client
    Ok(())
}
```

## API Reference

### Entry Points

- `query(prompt, options, transport)` - One-shot query returning a message stream
- `query_all(prompt, options)` - Collect all messages from a query
- `query_result(prompt, options)` - Get final response and result metadata

### ClaudeClient

```rust
let mut client = ClaudeClient::new(options, transport);
client.connect().await?;
client.query("Hello").await?;
let (response, result) = client.receive_response().await?;
client.disconnect().await?;
```

Methods:
- `connect()` - Connect to CLI
- `query(prompt)` - Send a query
- `receive_messages()` - Stream of messages
- `receive_response()` - Collect response and result
- `interrupt()` - Interrupt current operation
- `set_permission_mode(mode)` - Change permission mode
- `set_model(model)` - Change model
- `rewind_files(message_id)` - Rewind to checkpoint
- `disconnect()` - Disconnect from CLI

### ClaudeAgentOptions

```rust
let options = ClaudeAgentOptions::new()
    .with_model("claude-3-sonnet")
    .with_system_prompt("You are helpful.")
    .with_max_turns(10)
    .with_permission_mode(PermissionMode::AcceptEdits)
    .with_allowed_tools(vec!["Bash".into(), "Read".into()])
    .with_partial_messages();
```

### Message Types

```rust
enum Message {
    User(UserMessage),
    Assistant(AssistantMessage),
    System(SystemMessage),
    Result(ResultMessage),
    StreamEvent(StreamEvent),
}

// AssistantMessage has helpful methods
let text = assistant_msg.text();
let tool_uses = assistant_msg.tool_uses();
```

### Content Blocks

```rust
enum ContentBlock {
    Text(TextBlock),
    Thinking(ThinkingBlock),
    ToolUse(ToolUseBlock),
    ToolResult(ToolResultBlock),
}
```

## Examples

Run the examples:

```bash
# Simple one-shot query
cargo run --example simple_query

# Streaming client with multiple queries
cargo run --example streaming_client

# Tool permission callbacks
cargo run --example tool_permission_callback

# Lifecycle hooks (PreToolUse, PostToolUse)
cargo run --example hooks

# Error handling patterns
cargo run --example error_handling

# Streaming with progress indicators
cargo run --example streaming_progress

# System prompt configuration
cargo run --example system_prompt

# Budget limits
cargo run --example max_budget_usd

# MCP tools (requires --features mcp)
cargo run --example mcp_calculator --features mcp
```

See `examples/` directory for the full list.

## Testing

### Unit Tests

Unit tests run without authentication:

```bash
cargo test
# or
make test
```

### Integration Tests

Integration tests run against the real Claude API in Docker. They require authentication.

**Setup (one-time):**

```bash
# Generate an OAuth token (requires Claude Pro/Max subscription)
claude setup-token

# Create .env file with your token
cp .env.example .env
# Edit .env and paste your token
```

**Run tests:**

```bash
# Run all integration tests
make integration-test

# Run with verbose output
make integration-test-verbose

# Run specific test
./scripts/run-integration-tests.sh test_oneshot

# Interactive shell for debugging
make integration-shell
```

**What's tested:**

- Core query functionality (one-shot and streaming)
- Multi-turn conversations with context
- Tool permission callbacks
- Cancellation and timeout handling
- Resource cleanup (no process leaks)
- Error handling and edge cases

See [TESTING.md](TESTING.md) for the complete testing guide.

## Error Handling

The SDK provides `ClaudeSDKError`, a comprehensive error type for all failure modes.

### Error Types

```rust
use claude_agents_sdk::ClaudeSDKError;

match result {
    // CLI not found or not installed
    Err(ClaudeSDKError::CLINotFound { message }) => {
        eprintln!("Claude CLI not installed: {}", message);
        eprintln!("Install from: https://docs.anthropic.com/en/docs/claude-code");
    }

    // CLI process exited with error
    Err(ClaudeSDKError::Process { exit_code, stderr, .. }) => {
        eprintln!("CLI failed with exit code {:?}", exit_code);
        if let Some(err) = stderr {
            eprintln!("stderr: {}", err);
        }
    }

    // Operation timed out
    Err(ClaudeSDKError::Timeout { duration_ms }) => {
        eprintln!("Operation timed out after {}ms", duration_ms);
    }

    // Connection to CLI failed
    Err(ClaudeSDKError::CLIConnection { message }) => {
        eprintln!("Failed to connect to CLI: {}", message);
    }

    // Invalid configuration
    Err(ClaudeSDKError::Configuration { message }) => {
        eprintln!("Configuration error: {}", message);
    }

    // Message parsing failed (malformed JSON from CLI)
    Err(ClaudeSDKError::MessageParse { message, .. }) => {
        eprintln!("Failed to parse CLI message: {}", message);
    }

    // Control protocol error
    Err(ClaudeSDKError::ControlProtocol { message, .. }) => {
        eprintln!("Control protocol error: {}", message);
    }

    // All other errors
    Err(e) => eprintln!("Error: {}", e),

    Ok(_) => {}
}
```

### Recoverable Errors

Some errors can be retried:

```rust
if error.is_recoverable() {
    // Can retry: CLIConnection, Timeout, Channel errors
    tokio::time::sleep(Duration::from_secs(1)).await;
    // ... retry operation
}
```

### Timeout Configuration

Configure timeouts to prevent indefinite hangs:

```rust
let options = ClaudeAgentOptions::new()
    .with_timeout_secs(60)  // 60 second timeout
    .with_max_turns(5);

// Or disable timeout (not recommended)
let options = ClaudeAgentOptions::new()
    .with_timeout_secs(0);  // No timeout
```

Default timeout is 300 seconds (5 minutes).

### Stream Error Handling

When consuming message streams, handle errors per-message:

```rust
while let Some(result) = stream.next().await {
    match result {
        Ok(Message::Assistant(msg)) => {
            println!("{}", msg.text());
        }
        Ok(Message::Result(result)) => {
            if result.is_error {
                eprintln!("Query failed: {:?}", result.result);
            }
        }
        Err(e) => {
            eprintln!("Stream error: {}", e);
            break;
        }
        _ => {}
    }
}
```

### Result Type

All SDK functions return `Result<T, ClaudeSDKError>`:

```rust
use claude_agents_sdk::Result;

async fn my_function() -> Result<String> {
    let mut client = ClaudeClient::new(None, None);
    client.connect().await?;  // Propagates errors with ?
    // ...
    Ok("success".to_string())
}
```

## Architecture

The SDK communicates with the Claude Code CLI via a subprocess:

```
┌─────────────────┐     stdin/stdout      ┌──────────────┐
│  Rust SDK       │ ◄──── JSON-RPC ────► │  Claude CLI  │
│  (your code)    │                       │  (subprocess)│
└─────────────────┘                       └──────────────┘
```

Key internal components:
- `Transport` - Abstract communication layer
- `SubprocessTransport` - Subprocess implementation
- `Query` - Control protocol handler
- `InternalClient` - Core query processing

## Comparison with Python SDK

| Feature | Python | Rust |
|---------|--------|------|
| One-shot query | `query()` | `query()` |
| Streaming client | `ClaudeSDKClient` | `ClaudeClient` |
| Tool callback | `can_use_tool` | `can_use_tool` |
| Hooks | `hooks` dict | `hooks` HashMap |
| Async | asyncio | tokio |
| Type safety | TypedDict | Enums + Structs |

## License

MIT License - see [LICENSE](LICENSE) for details.