pub struct ClaudeSdkClient { /* private fields */ }Expand description
Session-based client for multi-turn Claude Code interactions.
ClaudeSdkClient maintains a connection to the Claude Code CLI subprocess,
allowing multiple queries within the same conversation context. The session
preserves conversation history across calls.
§Lifecycle
- Create a client with
new() - Call
connect()to start the session - Send queries with
query()and receive responses withreceive_message()orreceive_response() - Call
disconnect()when done
§Concurrency
After connection, query(), interrupt(),
and control methods take &self, allowing concurrent operations from different
tasks. Only connect(), disconnect(),
and receive_message() require &mut self.
§Example
let mut client = ClaudeSdkClient::new(None, None);
client.connect(None).await?;
client.query(InputPrompt::Text("Hello!".into()), "session-1").await?;
let messages = client.receive_response().await?;
client.disconnect().await?;Implementations§
Source§impl ClaudeSdkClient
impl ClaudeSdkClient
Sourcepub fn new(
options: Option<ClaudeAgentOptions>,
transport_factory: Option<Box<dyn TransportFactory>>,
) -> Self
pub fn new( options: Option<ClaudeAgentOptions>, transport_factory: Option<Box<dyn TransportFactory>>, ) -> Self
Creates a new ClaudeSdkClient with optional configuration and transport factory.
§Arguments
options— OptionalClaudeAgentOptionsfor configuring the session. IfNone, defaults are used.transport_factory— OptionalTransportFactoryfor creating transport instances on eachconnect()call. IfNone, the defaultSubprocessCliTransportis used. Using a factory enables reconnect after disconnect with the same client instance.
§Example
use claude_code::ClaudeSdkClient;
let _client = ClaudeSdkClient::new(None, None);Sourcepub fn new_with_transport(
options: Option<ClaudeAgentOptions>,
transport: Box<dyn Transport>,
) -> Self
pub fn new_with_transport( options: Option<ClaudeAgentOptions>, transport: Box<dyn Transport>, ) -> Self
Creates a new ClaudeSdkClient with a single-use custom transport.
The transport is consumed on the first connect(). Subsequent
connect() calls after disconnect() will return an error.
For reconnect support with custom transports, use new() with a
TransportFactory.
§Example
use claude_code::transport::subprocess_cli::{Prompt, SubprocessCliTransport};
use claude_code::ClaudeSdkClient;
let transport = SubprocessCliTransport::new(Prompt::Messages, Default::default()).unwrap();
let _client = ClaudeSdkClient::new_with_transport(None, Box::new(transport));Sourcepub async fn connect(&mut self, prompt: Option<InputPrompt>) -> Result<()>
pub async fn connect(&mut self, prompt: Option<InputPrompt>) -> Result<()>
Establishes a connection to the Claude Code CLI and starts the session.
If an existing connection exists, it is disconnected first.
§Arguments
prompt— Optional initial prompt to send upon connection. When usingcan_use_tool, this must beInputPrompt::Messages, notText.
§Errors
Returns an error if:
- The CLI executable is not found
can_use_toolis set with aTextprompt (requiresMessages)can_use_toolis set alongsidepermission_prompt_tool_name- The subprocess fails to start
§Example
use claude_code::{ClaudeSdkClient, InputPrompt};
let mut client = ClaudeSdkClient::new(None, None);
client.connect(Some(InputPrompt::Text("Hello".to_string()))).await?;
client.disconnect().await?;Sourcepub async fn connect_with_messages<S>(&mut self, prompt: S) -> Result<()>
pub async fn connect_with_messages<S>(&mut self, prompt: S) -> Result<()>
Establishes a connection and sends initial prompt messages from a stream.
This is a Rust-idiomatic equivalent of Python SDK connect(AsyncIterable).
The stream is consumed in a background task so this method returns once
connection is established. Unlike one-off query streaming helpers, this
keeps stdin open so the session can continue with follow-up
query() calls.
To synchronously wait for stream completion and surface stream write
errors, call wait_for_initial_messages().
§Errors
Returns the same errors as connect(), plus errors
when starting the background stream task.
§Example
use claude_code::ClaudeSdkClient;
use futures::stream;
use serde_json::json;
let mut client = ClaudeSdkClient::new(None, None);
client.connect_with_messages(stream::iter(vec![
json!({"type":"user","message":{"role":"user","content":"hello"}}),
])).await?;
client.wait_for_initial_messages().await?;
client.disconnect().await?;Sourcepub async fn wait_for_initial_messages(&mut self) -> Result<()>
pub async fn wait_for_initial_messages(&mut self) -> Result<()>
Waits for completion of the initial background message stream task.
This is only relevant after calling connect_with_messages().
If no background stream is active, this returns immediately.
§Example
use claude_code::ClaudeSdkClient;
let mut client = ClaudeSdkClient::new(None, None);
client.wait_for_initial_messages().await?;Sourcepub async fn query(&self, prompt: InputPrompt, session_id: &str) -> Result<()>
pub async fn query(&self, prompt: InputPrompt, session_id: &str) -> Result<()>
Sends a query within the current session.
The session must be connected first via connect().
After sending, use receive_message() or
receive_response() to get the response.
§Arguments
prompt— The prompt to send (text or structured messages).session_id— Session identifier for the query.
§Errors
Returns CLIConnectionError if not connected.
§Example
use claude_code::{ClaudeSdkClient, InputPrompt};
let mut client = ClaudeSdkClient::new(None, None);
client.connect(None).await?;
client.query(InputPrompt::Text("Summarize this repo".into()), "default").await?;
client.disconnect().await?;Sourcepub async fn query_stream<S>(&self, prompt: S, session_id: &str) -> Result<()>
pub async fn query_stream<S>(&self, prompt: S, session_id: &str) -> Result<()>
Streams JSON message prompts within the current session.
§Errors
Returns CLIConnectionError if not connected.
§Example
use claude_code::ClaudeSdkClient;
use futures::stream;
use serde_json::json;
let mut client = ClaudeSdkClient::new(None, None);
client.connect(None).await?;
client
.query_stream(
stream::iter(vec![json!({"type":"user","message":{"role":"user","content":"hello"}})]),
"default",
)
.await?;
client.disconnect().await?;Sourcepub async fn receive_message(&mut self) -> Result<Option<Message>>
pub async fn receive_message(&mut self) -> Result<Option<Message>>
Receives a single message from the current query.
Returns None when no more messages are available.
§Errors
Returns CLIConnectionError if not connected.
§Example
use claude_code::ClaudeSdkClient;
let mut client = ClaudeSdkClient::new(None, None);
client.connect(None).await?;
let _next = client.receive_message().await?;
client.disconnect().await?;Sourcepub async fn receive_response(&mut self) -> Result<Vec<Message>>
pub async fn receive_response(&mut self) -> Result<Vec<Message>>
Receives all messages for the current query until a Message::Result is received.
§Errors
Returns CLIConnectionError if not connected.
§Example
use claude_code::{ClaudeSdkClient, InputPrompt};
let mut client = ClaudeSdkClient::new(None, None);
client.connect(None).await?;
client.query(InputPrompt::Text("Hi".into()), "default").await?;
let _messages = client.receive_response().await?;
client.disconnect().await?;Sourcepub async fn interrupt(&self) -> Result<()>
pub async fn interrupt(&self) -> Result<()>
Interrupts the current operation.
§Errors
Returns CLIConnectionError if not connected.
§Example
use claude_code::ClaudeSdkClient;
let mut client = ClaudeSdkClient::new(None, None);
client.connect(None).await?;
client.interrupt().await?;
client.disconnect().await?;Sourcepub async fn set_permission_mode(&self, mode: &str) -> Result<()>
pub async fn set_permission_mode(&self, mode: &str) -> Result<()>
Changes the permission mode for the current session.
§Errors
Returns CLIConnectionError if not connected.
§Example
use claude_code::ClaudeSdkClient;
let mut client = ClaudeSdkClient::new(None, None);
client.connect(None).await?;
client.set_permission_mode("plan").await?;
client.disconnect().await?;Sourcepub async fn set_model(&self, model: Option<&str>) -> Result<()>
pub async fn set_model(&self, model: Option<&str>) -> Result<()>
Changes the model used for the current session.
§Errors
Returns CLIConnectionError if not connected.
§Example
use claude_code::ClaudeSdkClient;
let mut client = ClaudeSdkClient::new(None, None);
client.connect(None).await?;
client.set_model(Some("sonnet")).await?;
client.disconnect().await?;Sourcepub async fn rewind_files(&self, user_message_id: &str) -> Result<()>
pub async fn rewind_files(&self, user_message_id: &str) -> Result<()>
Rewinds file changes to a specific user message checkpoint.
§Errors
Returns CLIConnectionError if not connected.
§Example
use claude_code::ClaudeSdkClient;
let mut client = ClaudeSdkClient::new(None, None);
client.connect(None).await?;
client.rewind_files("user-msg-1").await?;
client.disconnect().await?;Sourcepub async fn get_mcp_status(&self) -> Result<McpStatusResponse>
pub async fn get_mcp_status(&self) -> Result<McpStatusResponse>
Queries the status of connected MCP servers.
§Errors
Returns CLIConnectionError if not connected.
§Example
use claude_code::ClaudeSdkClient;
let mut client = ClaudeSdkClient::new(None, None);
client.connect(None).await?;
let _status = client.get_mcp_status().await?;
client.disconnect().await?;Sourcepub async fn reconnect_mcp_server(&self, server_name: &str) -> Result<()>
pub async fn reconnect_mcp_server(&self, server_name: &str) -> Result<()>
Reconnects a disconnected or failed MCP server.
§Errors
Returns CLIConnectionError if not connected.
Sourcepub fn get_server_info(&self) -> Result<Option<Value>>
pub fn get_server_info(&self) -> Result<Option<Value>>
Returns the server initialization response, if available.
§Errors
Returns CLIConnectionError if not connected.
§Example
use claude_code::ClaudeSdkClient;
let mut client = ClaudeSdkClient::new(None, None);
client.connect(None).await?;
let _info = client.get_server_info()?;
client.disconnect().await?;