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
impl ClaudeSDKClient
pub fn new(options: ClaudeAgentOptions) -> Self
Sourcepub fn add_mcp_server(
&mut self,
name: impl Into<String>,
server: SdkMcpServer,
) -> Result<()>
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).
Sourcepub async fn connect(&mut self, initial_prompt: Option<&str>) -> Result<()>
pub async fn connect(&mut self, initial_prompt: Option<&str>) -> Result<()>
Connect to the Claude CLI. Optionally send an initial prompt.
Sourcepub async fn query(&self, prompt: &str, session_id: Option<&str>) -> Result<()>
pub async fn query(&self, prompt: &str, session_id: Option<&str>) -> Result<()>
Send a query/prompt. Optionally provide a session_id for resuming.
Sourcepub fn receive_messages(&mut self) -> MessageStream<'_>
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.
Sourcepub async fn receive_response(&mut self) -> Result<Vec<Message>>
pub async fn receive_response(&mut self) -> Result<Vec<Message>>
Collect all messages until the next ResultMessage.
Sourcepub async fn set_permission_mode(&self, mode: &str) -> Result<Value>
pub async fn set_permission_mode(&self, mode: &str) -> Result<Value>
Change the permission mode.
Sourcepub async fn rewind_files(&self, user_message_id: &str) -> Result<Value>
pub async fn rewind_files(&self, user_message_id: &str) -> Result<Value>
Rewind file changes to a specific user message.
Sourcepub async fn get_mcp_status(&self) -> Result<Value>
pub async fn get_mcp_status(&self) -> Result<Value>
Get MCP server status.
Sourcepub async fn get_server_info(&self) -> Option<Value>
pub async fn get_server_info(&self) -> Option<Value>
Get server info from the init handshake.
Sourcepub async fn disconnect(&mut self) -> Result<()>
pub async fn disconnect(&mut self) -> Result<()>
Disconnect from the CLI.
Sourcepub fn is_connected(&self) -> bool
pub fn is_connected(&self) -> bool
Check if connected.
Auto Trait Implementations§
impl Freeze for ClaudeSDKClient
impl !RefUnwindSafe for ClaudeSDKClient
impl Send for ClaudeSDKClient
impl Sync for ClaudeSDKClient
impl Unpin for ClaudeSDKClient
impl !UnwindSafe for ClaudeSDKClient
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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