Skip to main content

ClaudeSDKClient

Struct ClaudeSDKClient 

Source
pub struct ClaudeSDKClient { /* private fields */ }
Expand description

A stateful client for multi-turn conversations with the Claude CLI.

Unlike query() which is one-shot, the client maintains a connection and supports sending multiple queries, interrupts, and control commands.

§Example

use claude_code_rs::{ClaudeAgentOptions, Message};
use claude_code_rs::client::ClaudeSDKClient;
use tokio_stream::StreamExt;

let mut client = ClaudeSDKClient::new(ClaudeAgentOptions::default());
client.connect(None).await?;

// First query.
client.query("What is Rust?", None).await?;
{
    let mut stream = client.receive_messages();
    while let Some(msg) = stream.next().await {
        let msg = msg?;
        if msg.is_result() { break; }
    }
} // Receiver auto-restores when `stream` drops.

// Follow-up query in same session.
client.query("How does ownership work?", None).await?;
// ...

client.disconnect().await?;

Implementations§

Source§

impl ClaudeSDKClient

Source

pub fn new(options: ClaudeAgentOptions) -> Self

Source

pub fn add_mcp_server( &mut self, name: impl Into<String>, server: SdkMcpServer, ) -> Result<()>

Register an in-process MCP server by name.

Must be called before connect(). Returns an error if the client is already connected (servers are snapshot-cloned during connect).

Source

pub async fn connect(&mut self, initial_prompt: Option<&str>) -> Result<()>

Connect to the Claude CLI. Optionally send an initial prompt.

Source

pub async fn query(&self, prompt: &str, session_id: Option<&str>) -> Result<()>

Send a query/prompt. Optionally provide a session_id for resuming.

Source

pub fn receive_messages(&mut self) -> MessageStream<'_>

Get a stream of messages from the current query.

Messages flow until a ResultMessage signals end of turn. The receiver is automatically restored when the returned MessageStream is dropped, so the client remains usable for follow-up queries.

Source

pub async fn receive_response(&mut self) -> Result<Vec<Message>>

Collect all messages until the next ResultMessage.

Source

pub async fn interrupt(&self) -> Result<Value>

Send an interrupt command.

Source

pub async fn set_permission_mode(&self, mode: &str) -> Result<Value>

Change the permission mode.

Source

pub async fn set_model(&self, model: &str) -> Result<Value>

Change the model.

Source

pub async fn rewind_files(&self, user_message_id: &str) -> Result<Value>

Rewind file changes to a specific user message.

Source

pub async fn get_mcp_status(&self) -> Result<Value>

Get MCP server status.

Source

pub async fn get_server_info(&self) -> Option<Value>

Get server info from the init handshake.

Source

pub async fn disconnect(&mut self) -> Result<()>

Disconnect from the CLI.

Source

pub fn is_connected(&self) -> bool

Check if connected.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more