claude-cli-sdk 0.5.1

Rust SDK for programmatic interaction with the 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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
//! Transport layer — spawning and communicating with the Claude CLI.
//!
//! The [`Transport`] trait abstracts the communication channel between the SDK
//! and the Claude Code process.  [`CliTransport`] is the production
//! implementation that spawns the CLI as a subprocess and uses NDJSON over
//! stdin/stdout.
//!
//! # Architecture
//!
//! `CliTransport::connect()` spawns the CLI, then starts a background reader
//! task that reads NDJSON lines from stdout and forwards parsed JSON values
//! through a `tokio::sync::mpsc` channel.  `read_messages()` returns a
//! `ReceiverStream` — no mutex is held on the hot path.

use std::collections::HashMap;
use std::path::PathBuf;
use std::pin::Pin;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::Duration;

use async_trait::async_trait;
use futures_core::Stream;
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
use tokio::process::{Child, ChildStdin};
use tokio::sync::mpsc;
use tokio::task::JoinHandle;
use tokio_stream::wrappers::ReceiverStream;

use crate::errors::{Error, Result};

// ── Transport Trait ──────────────────────────────────────────────────────────

/// Abstraction over the communication channel to the Claude CLI.
///
/// Implementations must be `Send + Sync` for use across async tasks.
#[async_trait]
pub trait Transport: Send + Sync {
    /// Establish the connection (e.g., spawn the CLI process).
    async fn connect(&self) -> Result<()>;

    /// Write a line to the CLI's stdin.
    async fn write(&self, data: &str) -> Result<()>;

    /// Return a stream of parsed JSON values from the CLI's stdout.
    fn read_messages(&self) -> Pin<Box<dyn Stream<Item = Result<serde_json::Value>> + Send>>;

    /// Signal end-of-input to the CLI (close stdin).
    async fn end_input(&self) -> Result<()>;

    /// Send an interrupt signal (SIGINT on Unix).
    async fn interrupt(&self) -> Result<()>;

    /// Returns `true` if the transport is connected and ready.
    fn is_ready(&self) -> bool;

    /// Close the transport and wait for the process to exit.
    ///
    /// Returns the exit code if available.
    async fn close(&self) -> Result<Option<i32>>;
}

// ── CliTransport ─────────────────────────────────────────────────────────────

/// Production transport that spawns the Claude CLI as a subprocess.
///
/// Uses NDJSON over stdin/stdout. A background reader task forwards parsed
/// JSON values through a bounded channel.
pub struct CliTransport {
    cli_path: PathBuf,
    args: Vec<String>,
    cwd: PathBuf,
    env: HashMap<String, String>,
    close_timeout: Option<Duration>,
    end_input_on_connect: bool,
    // `process` uses `std::sync::Mutex` because it must be accessed in the
    // synchronous `Drop` impl. The remaining fields use `tokio::sync::Mutex`
    // because they are only accessed in async contexts.
    process: std::sync::Mutex<Option<Child>>,
    stdin: tokio::sync::Mutex<Option<ChildStdin>>,
    message_rx: std::sync::Mutex<Option<mpsc::Receiver<Result<serde_json::Value>>>>,
    stderr_callback: Option<Arc<dyn Fn(String) + Send + Sync>>,
    reader_handle: tokio::sync::Mutex<Option<JoinHandle<()>>>,
    ready: AtomicBool,
}

impl std::fmt::Debug for CliTransport {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("CliTransport")
            .field("cli_path", &self.cli_path)
            .field("cwd", &self.cwd)
            .field("ready", &self.ready.load(Ordering::Relaxed))
            .finish_non_exhaustive()
    }
}

impl CliTransport {
    /// Create a new `CliTransport` with the given configuration.
    ///
    /// This does NOT spawn the process — call [`connect()`](Transport::connect) first.
    #[must_use]
    pub fn new(
        cli_path: PathBuf,
        args: Vec<String>,
        cwd: PathBuf,
        env: HashMap<String, String>,
        stderr_callback: Option<Arc<dyn Fn(String) + Send + Sync>>,
    ) -> Self {
        Self {
            cli_path,
            args,
            cwd,
            env,
            close_timeout: None,
            end_input_on_connect: false,
            process: std::sync::Mutex::new(None),
            stdin: tokio::sync::Mutex::new(None),
            message_rx: std::sync::Mutex::new(None),
            stderr_callback,
            reader_handle: tokio::sync::Mutex::new(None),
            ready: AtomicBool::new(false),
        }
    }

    /// Create from a [`ClientConfig`](crate::config::ClientConfig).
    pub fn from_config(config: &crate::config::ClientConfig) -> Result<Self> {
        let cli_path = match &config.cli_path {
            Some(p) => p.clone(),
            None => crate::discovery::find_cli()?,
        };

        let cwd = config
            .cwd
            .clone()
            .unwrap_or_else(|| std::env::current_dir().unwrap_or_else(|_| PathBuf::from(".")));

        let args = config.to_cli_args();
        let env = config.to_env();

        let mut transport = Self::new(cli_path, args, cwd, env, config.stderr_callback.clone());
        transport.close_timeout = config.close_timeout;
        transport.end_input_on_connect = config.end_input_on_connect;
        Ok(transport)
    }

    /// Internal connect logic without timeout wrapping.
    async fn connect_inner(&self) -> Result<()> {
        use std::process::Stdio;

        let mut cmd = tokio::process::Command::new(&self.cli_path);
        cmd.args(&self.args)
            .current_dir(&self.cwd)
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .stderr(Stdio::piped());

        for (k, v) in &self.env {
            cmd.env(k, v);
        }

        // Strip Claude Code env vars so the child CLI process doesn't think
        // it's nested inside another Claude Code session. This happens when
        // the SDK is used from a daemon that was itself started from Claude Code.
        // - CLAUDECODE: Presence triggers "cannot launch inside another session" error
        // - CLAUDE_CODE_SSE_PORT: Causes child to connect to parent's SSE port
        // - CLAUDE_CODE_ENTRYPOINT: Inherited entrypoint context from parent
        // NOTE: These removes run AFTER the user env loop so they can never be
        // re-added by a caller passing them through ClientConfig::env.
        cmd.env_remove("CLAUDECODE");
        cmd.env_remove("CLAUDE_CODE_SSE_PORT");
        cmd.env_remove("CLAUDE_CODE_ENTRYPOINT");

        // On Windows, create the child in its own process group so that
        // interrupt signals can target it without affecting the host process.
        #[cfg(windows)]
        {
            use windows_sys::Win32::System::Threading::CREATE_NEW_PROCESS_GROUP;
            cmd.creation_flags(CREATE_NEW_PROCESS_GROUP);
        }

        let mut child = cmd.spawn().map_err(Error::SpawnFailed)?;

        let child_stdin = child
            .stdin
            .take()
            .ok_or_else(|| Error::Transport("failed to capture child stdin".into()))?;

        let child_stdout = child
            .stdout
            .take()
            .ok_or_else(|| Error::Transport("failed to capture child stdout".into()))?;

        let child_stderr = child.stderr.take();

        // Channel for NDJSON messages from the reader task.
        let (tx, rx) = mpsc::channel(256);

        // Background reader task: read NDJSON lines from stdout.
        let reader_handle = tokio::spawn(async move {
            let reader = BufReader::new(child_stdout);
            let mut lines = reader.lines();

            loop {
                match lines.next_line().await {
                    Ok(Some(line)) => {
                        let line = line.trim().to_string();
                        if line.is_empty() {
                            continue;
                        }

                        let result =
                            serde_json::from_str::<serde_json::Value>(&line).map_err(|e| {
                                Error::ParseError {
                                    message: e.to_string(),
                                    line: if line.len() > 200 {
                                        format!("{}...", &line[..200])
                                    } else {
                                        line.clone()
                                    },
                                }
                            });

                        if tx.send(result).await.is_err() {
                            break; // Receiver dropped
                        }
                    }
                    Ok(None) => break, // Stream ended
                    Err(e) => {
                        let _ = tx.send(Err(Error::Io(e))).await;
                        break;
                    }
                }
            }
        });

        // Optional stderr reader task.
        if let Some(stderr) = child_stderr {
            let stderr_cb = self.stderr_callback.clone();
            tokio::spawn(async move {
                let reader = BufReader::new(stderr);
                let mut lines = reader.lines();
                while let Ok(Some(line)) = lines.next_line().await {
                    if let Some(cb) = &stderr_cb {
                        cb(line);
                    }
                }
            });
        }

        // Store state.
        *self.process.lock().expect("process lock") = Some(child);
        *self.stdin.lock().await = Some(child_stdin);
        *self.message_rx.lock().expect("message_rx lock") = Some(rx);
        *self.reader_handle.lock().await = Some(reader_handle);
        self.ready.store(true, Ordering::Release);

        // Close stdin if configured — required for --print mode with stream-json
        // where the CLI waits for stdin EOF before processing.
        if self.end_input_on_connect {
            self.end_input().await?;
        }

        Ok(())
    }
}

#[async_trait]
impl Transport for CliTransport {
    async fn connect(&self) -> Result<()> {
        self.connect_inner().await
    }

    async fn write(&self, data: &str) -> Result<()> {
        let mut guard = self.stdin.lock().await;
        let stdin = guard.as_mut().ok_or(Error::NotConnected)?;
        stdin
            .write_all(data.as_bytes())
            .await
            .map_err(|e| Error::Transport(format!("write failed: {e}")))?;
        stdin
            .write_all(b"\n")
            .await
            .map_err(|e| Error::Transport(format!("write newline failed: {e}")))?;
        stdin
            .flush()
            .await
            .map_err(|e| Error::Transport(format!("flush failed: {e}")))?;
        Ok(())
    }

    fn read_messages(&self) -> Pin<Box<dyn Stream<Item = Result<serde_json::Value>> + Send>> {
        // Take the receiver out of the mutex. This can only be called once
        // (subsequent calls return an empty stream).
        let rx = self.message_rx.lock().expect("message_rx lock").take();

        match rx {
            Some(rx) => Box::pin(ReceiverStream::new(rx)),
            None => {
                tracing::debug!("read_messages() called after receiver was already taken");
                Box::pin(tokio_stream::empty())
            }
        }
    }

    async fn end_input(&self) -> Result<()> {
        let mut guard = self.stdin.lock().await;
        // Drop the stdin handle to signal EOF.
        *guard = None;
        Ok(())
    }

    async fn interrupt(&self) -> Result<()> {
        let guard = self.process.lock().expect("process lock");
        if let Some(child) = guard.as_ref() {
            if let Some(pid) = child.id() {
                #[cfg(unix)]
                {
                    use nix::sys::signal::{Signal, kill};
                    use nix::unistd::Pid;
                    let _ = kill(Pid::from_raw(pid as i32), Signal::SIGINT);
                }
                #[cfg(windows)]
                {
                    use windows_sys::Win32::System::Console::{
                        CTRL_BREAK_EVENT, GenerateConsoleCtrlEvent,
                    };
                    // CTRL_BREAK_EVENT is used because CTRL_C_EVENT is ignored by
                    // processes created with CREATE_NEW_PROCESS_GROUP. CTRL_BREAK
                    // is delivered to the specific process group (the child's PID
                    // serves as the group ID).
                    let _ = unsafe { GenerateConsoleCtrlEvent(CTRL_BREAK_EVENT, pid) };
                }
            }
        }
        Ok(())
    }

    fn is_ready(&self) -> bool {
        self.ready.load(Ordering::Acquire)
    }

    async fn close(&self) -> Result<Option<i32>> {
        self.ready.store(false, Ordering::Release);

        // Close stdin to signal the CLI to exit.
        self.end_input().await?;

        // Wait for the reader task to finish.
        if let Some(handle) = self.reader_handle.lock().await.take() {
            let _ = handle.await;
        }

        // Take the child out of the mutex before awaiting.
        let maybe_child = {
            let mut guard = self.process.lock().expect("process lock");
            guard.take()
        };

        let exit_code = if let Some(mut child) = maybe_child {
            match self.close_timeout {
                Some(d) => {
                    match tokio::time::timeout(d, child.wait()).await {
                        Ok(Ok(status)) => status.code(),
                        Ok(Err(e)) => {
                            return Err(Error::Transport(format!("wait failed: {e}")));
                        }
                        Err(_) => {
                            // Timed out waiting — kill the process.
                            let _ = child.kill().await;
                            return Err(Error::Timeout(format!(
                                "close timed out after {}s, process killed",
                                d.as_secs_f64()
                            )));
                        }
                    }
                }
                None => {
                    let status = child
                        .wait()
                        .await
                        .map_err(|e| Error::Transport(format!("wait failed: {e}")))?;
                    status.code()
                }
            }
        } else {
            None
        };

        Ok(exit_code)
    }
}

impl Drop for CliTransport {
    fn drop(&mut self) {
        if let Ok(mut guard) = self.process.lock() {
            if let Some(mut child) = guard.take() {
                let _ = child.start_kill();
            }
        }
    }
}

// ── Tests ────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn cli_transport_debug() {
        let t = CliTransport::new(
            PathBuf::from("/usr/bin/claude"),
            vec!["--print".into(), "hi".into()],
            PathBuf::from("/tmp"),
            HashMap::new(),
            None,
        );
        let debug = format!("{t:?}");
        assert!(debug.contains("claude"));
        assert!(debug.contains("/tmp"));
    }

    #[test]
    fn cli_transport_not_ready_before_connect() {
        let t = CliTransport::new(
            PathBuf::from("/usr/bin/claude"),
            vec![],
            PathBuf::from("/tmp"),
            HashMap::new(),
            None,
        );
        assert!(!t.is_ready());
    }

    #[test]
    fn cli_transport_read_messages_returns_empty_without_connect() {
        let t = CliTransport::new(
            PathBuf::from("/usr/bin/claude"),
            vec![],
            PathBuf::from("/tmp"),
            HashMap::new(),
            None,
        );
        // Should not panic — returns empty stream.
        let _stream = t.read_messages();
    }

    #[tokio::test]
    async fn cli_transport_write_fails_when_not_connected() {
        let t = CliTransport::new(
            PathBuf::from("/usr/bin/claude"),
            vec![],
            PathBuf::from("/tmp"),
            HashMap::new(),
            None,
        );
        let err = t.write("test").await.unwrap_err();
        assert!(matches!(err, Error::NotConnected));
    }

    #[tokio::test]
    async fn cli_transport_end_input_ok_when_not_connected() {
        let t = CliTransport::new(
            PathBuf::from("/usr/bin/claude"),
            vec![],
            PathBuf::from("/tmp"),
            HashMap::new(),
            None,
        );
        // Should not error — just no-op.
        assert!(t.end_input().await.is_ok());
    }

    #[tokio::test]
    async fn cli_transport_interrupt_ok_when_not_connected() {
        let t = CliTransport::new(
            PathBuf::from("/usr/bin/claude"),
            vec![],
            PathBuf::from("/tmp"),
            HashMap::new(),
            None,
        );
        assert!(t.interrupt().await.is_ok());
    }

    #[tokio::test]
    async fn cli_transport_close_ok_when_not_connected() {
        let t = CliTransport::new(
            PathBuf::from("/usr/bin/claude"),
            vec![],
            PathBuf::from("/tmp"),
            HashMap::new(),
            None,
        );
        let result = t.close().await;
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), None);
    }
}