claude-code-sdk-rust 0.1.0

Async Rust SDK for the Claude Code CLI: streaming agent turns, tool use, and sessions.
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
use crate::types::*;
use std::collections::HashMap;

/// Builder for ClaudeAgentOptions
#[derive(Debug, Default)]
pub struct ClaudeAgentOptionsBuilder {
    options: ClaudeAgentOptions,
}

impl ClaudeAgentOptionsBuilder {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn tools(mut self, tools: Vec<String>) -> Self {
        self.options.tools = tools;
        self.options.tools_set = true;
        self
    }

    pub fn tools_preset(mut self, preset: ToolsPreset) -> Self {
        self.options.tools_preset = Some(preset);
        self.options.tools_set = false;
        self
    }

    pub fn allowed_tools(mut self, tools: Vec<String>) -> Self {
        self.options.allowed_tools = tools;
        self
    }

    pub fn system_prompt(mut self, prompt: impl Into<String>) -> Self {
        self.options.system_prompt = Some(prompt.into());
        self
    }

    pub fn system_prompt_preset(mut self, preset: SystemPromptPreset) -> Self {
        self.options.system_prompt_preset = Some(preset);
        self
    }

    pub fn system_prompt_file(mut self, file: SystemPromptFile) -> Self {
        self.options.system_prompt_file = Some(file);
        self
    }

    pub fn mcp_server(mut self, name: impl Into<String>, config: MCPServerConfig) -> Self {
        self.options.mcp_servers.insert(name.into(), config);
        self
    }

    pub fn sdk_mcp_server(
        mut self,
        name: impl Into<String>,
        server: crate::mcp::SimpleMCPServer,
    ) -> Self {
        let name = name.into();
        self.options
            .mcp_servers
            .insert(name.clone(), MCPServerConfig::Sdk { name: name.clone() });
        self.options.sdk_mcp_servers.insert(name, server);
        self
    }

    pub fn mcp_servers(mut self, servers: HashMap<String, MCPServerConfig>) -> Self {
        self.options.mcp_servers = servers;
        self.options.mcp_servers_config = None;
        self
    }

    pub fn mcp_servers_config(mut self, config: impl Into<String>) -> Self {
        self.options.mcp_servers.clear();
        self.options.mcp_servers_config = Some(config.into());
        self
    }

    pub fn permission_mode(mut self, mode: PermissionMode) -> Self {
        self.options.permission_mode = Some(mode);
        self
    }

    pub fn continue_conversation(mut self, continue_conv: bool) -> Self {
        self.options.continue_conversation = continue_conv;
        self
    }

    pub fn resume(mut self, session_id: impl Into<String>) -> Self {
        self.options.resume = Some(session_id.into());
        self
    }

    pub fn session_id(mut self, session_id: impl Into<String>) -> Self {
        self.options.session_id = Some(session_id.into());
        self
    }

    pub fn fork_session(mut self, fork: bool) -> Self {
        self.options.fork_session = fork;
        self
    }

    pub fn max_turns(mut self, turns: i32) -> Self {
        self.options.max_turns = Some(turns);
        self
    }

    pub fn max_budget_usd(mut self, budget: f64) -> Self {
        self.options.max_budget_usd = Some(budget);
        self
    }

    pub fn task_budget(mut self, budget: TaskBudget) -> Self {
        self.options.task_budget = Some(budget);
        self
    }

    pub fn task_budget_total(mut self, total: i32) -> Self {
        self.options.task_budget = Some(TaskBudget { total });
        self
    }

    pub fn disallowed_tools(mut self, tools: Vec<String>) -> Self {
        self.options.disallowed_tools = tools;
        self
    }

    pub fn model(mut self, model: impl Into<String>) -> Self {
        self.options.model = Some(model.into());
        self
    }

    pub fn fallback_model(mut self, model: impl Into<String>) -> Self {
        self.options.fallback_model = Some(model.into());
        self
    }

    pub fn betas(mut self, betas: Vec<SdkBeta>) -> Self {
        self.options.betas = betas;
        self
    }

    pub fn add_beta(mut self, beta: SdkBeta) -> Self {
        self.options.betas.push(beta);
        self
    }

    pub fn permission_prompt_tool_name(mut self, name: impl Into<String>) -> Self {
        self.options.permission_prompt_tool_name = Some(name.into());
        self
    }

    pub fn can_use_tool<F, Fut>(mut self, callback: F) -> Self
    where
        F: Fn(String, serde_json::Map<String, serde_json::Value>, ToolPermissionContext) -> Fut
            + Send
            + Sync
            + 'static,
        Fut: std::future::Future<Output = crate::error::Result<PermissionResult>> + Send + 'static,
    {
        self.options.can_use_tool = Some(CanUseToolCallback::new(callback));
        self
    }

    pub fn hook(mut self, event: impl Into<String>, matcher: HookMatcher) -> Self {
        self.options
            .hooks
            .entry(event.into())
            .or_default()
            .push(matcher);
        self
    }

    pub fn hooks(mut self, hooks: HookMap) -> Self {
        self.options.hooks = hooks;
        self
    }

    pub fn agent(mut self, name: impl Into<String>, agent: AgentDefinition) -> Self {
        self.options.agents.insert(name.into(), agent);
        self
    }

    pub fn agents(mut self, agents: HashMap<String, AgentDefinition>) -> Self {
        self.options.agents = agents;
        self
    }

    pub fn cwd(mut self, cwd: impl Into<String>) -> Self {
        self.options.cwd = Some(cwd.into());
        self
    }

    pub fn cli_path(mut self, path: impl Into<String>) -> Self {
        self.options.cli_path = Some(path.into());
        self
    }

    pub fn settings(mut self, settings: impl Into<String>) -> Self {
        self.options.settings = Some(settings.into());
        self
    }

    pub fn add_dir(mut self, dir: impl Into<String>) -> Self {
        self.options.add_dirs.push(dir.into());
        self
    }

    pub fn add_dirs(mut self, dirs: Vec<String>) -> Self {
        self.options.add_dirs = dirs;
        self
    }

    pub fn env(mut self, env: HashMap<String, String>) -> Self {
        self.options.env = env;
        self
    }

    pub fn env_var(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.options.env.insert(key.into(), value.into());
        self
    }

    pub fn extra_arg(mut self, key: impl Into<String>, value: Option<String>) -> Self {
        self.options.extra_args.insert(key.into(), value);
        self
    }

    pub fn max_buffer_size(mut self, size: usize) -> Self {
        self.options.max_buffer_size = Some(size);
        self
    }

    pub fn user(mut self, user: impl Into<String>) -> Self {
        self.options.user = Some(user.into());
        self
    }

    pub fn include_partial_messages(mut self, include: bool) -> Self {
        self.options.include_partial_messages = include;
        self
    }

    pub fn include_hook_events(mut self, include: bool) -> Self {
        self.options.include_hook_events = include;
        self
    }

    pub fn strict_mcp_config(mut self, strict: bool) -> Self {
        self.options.strict_mcp_config = strict;
        self
    }

    pub fn setting_sources(mut self, sources: Vec<SettingSource>) -> Self {
        self.options.setting_sources = Some(sources);
        self
    }

    pub fn add_setting_source(mut self, source: SettingSource) -> Self {
        self.options
            .setting_sources
            .get_or_insert_with(Vec::new)
            .push(source);
        self
    }

    pub fn skills_all(mut self) -> Self {
        self.options.skills = Some(SkillsConfig::All);
        self
    }

    pub fn skills(mut self, skills: Vec<String>) -> Self {
        self.options.skills = Some(SkillsConfig::Names(skills));
        self
    }

    pub fn sandbox(mut self, sandbox: SandboxSettings) -> Self {
        self.options.sandbox = Some(sandbox);
        self
    }

    pub fn plugin(mut self, plugin: SDKPluginConfig) -> Self {
        self.options.plugins.push(plugin);
        self
    }

    pub fn plugins(mut self, plugins: Vec<SDKPluginConfig>) -> Self {
        self.options.plugins = plugins;
        self
    }

    pub fn max_thinking_tokens(mut self, tokens: i32) -> Self {
        self.options.max_thinking_tokens = Some(tokens);
        self
    }

    pub fn thinking(mut self, thinking: ThinkingConfig) -> Self {
        self.options.thinking = Some(thinking);
        self
    }

    pub fn effort(mut self, effort: impl Into<String>) -> Self {
        self.options.effort = Some(effort.into());
        self
    }

    pub fn output_format(mut self, format: serde_json::Map<String, serde_json::Value>) -> Self {
        self.options.output_format = Some(format);
        self
    }

    pub fn enable_file_checkpointing(mut self, enable: bool) -> Self {
        self.options.enable_file_checkpointing = enable;
        self
    }

    pub fn stderr<F>(mut self, callback: F) -> Self
    where
        F: Fn(String) + Send + Sync + 'static,
    {
        self.options.stderr = Some(StderrCallback::new(callback));
        self
    }

    pub fn session_store<S>(mut self, store: S) -> Self
    where
        S: crate::session_store::SessionStore + 'static,
    {
        self.options.session_store = Some(crate::session_store::SessionStoreHandle::new(store));
        self
    }

    pub fn session_store_handle(mut self, store: crate::session_store::SessionStoreHandle) -> Self {
        self.options.session_store = Some(store);
        self
    }

    pub fn session_store_flush(mut self, mode: SessionStoreFlushMode) -> Self {
        self.options.session_store_flush = mode;
        self
    }

    pub fn load_timeout_ms(mut self, timeout_ms: i32) -> Self {
        self.options.load_timeout_ms = timeout_ms;
        self
    }

    pub fn build(self) -> ClaudeAgentOptions {
        self.options
    }
}

/// Extension trait for ClaudeAgentOptions to add builder method
impl ClaudeAgentOptions {
    pub fn builder() -> ClaudeAgentOptionsBuilder {
        ClaudeAgentOptionsBuilder::new()
    }
}

/// Options for listing sessions
#[derive(Debug, Clone, Default)]
pub struct ListSessionsOptions {
    pub directory: Option<String>,
    pub limit: Option<usize>,
    pub include_worktrees: Option<bool>,
}

impl ListSessionsOptions {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn directory(mut self, dir: impl Into<String>) -> Self {
        self.directory = Some(dir.into());
        self
    }

    pub fn limit(mut self, limit: usize) -> Self {
        self.limit = Some(limit);
        self
    }

    pub fn include_worktrees(mut self, include: bool) -> Self {
        self.include_worktrees = Some(include);
        self
    }
}

/// Options for querying session data
#[derive(Debug, Clone, Default)]
pub struct SessionQueryOptions {
    pub directory: Option<String>,
    pub limit: Option<usize>,
    pub offset: Option<usize>,
}

impl SessionQueryOptions {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn directory(mut self, dir: impl Into<String>) -> Self {
        self.directory = Some(dir.into());
        self
    }

    pub fn limit(mut self, limit: usize) -> Self {
        self.limit = Some(limit);
        self
    }

    pub fn offset(mut self, offset: usize) -> Self {
        self.offset = Some(offset);
        self
    }
}

/// Options for session mutations
#[derive(Debug, Clone, Default)]
pub struct SessionMutationOptions {
    pub directory: Option<String>,
}

impl SessionMutationOptions {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn directory(mut self, dir: impl Into<String>) -> Self {
        self.directory = Some(dir.into());
        self
    }
}