pub struct Query { /* private fields */ }Expand description
Low-level query session handler for Claude Code CLI communication.
Manages the bidirectional JSON stream protocol between the SDK and the CLI. On startup, a background tokio task is spawned to continuously read messages from the transport and route them:
- Control responses are delivered to the waiting control-request caller via oneshot channels.
- Control requests (permissions, hooks, MCP) are handled by the background task.
- SDK messages (user, assistant, system, result) are parsed and delivered via an mpsc channel.
This architecture mirrors the Python SDK’s task-group model and enables concurrent send and receive operations.
Implementations§
Source§impl Query
impl Query
Sourcepub async fn initialize(
&mut self,
hooks_config: Map<String, Value>,
) -> Result<Option<Value>>
pub async fn initialize( &mut self, hooks_config: Map<String, Value>, ) -> Result<Option<Value>>
Sends the initialization handshake to the CLI.
Registers hook callbacks and agent definitions with the CLI process, and waits for the initialization response.
Returns the initialization response payload, or None if not in streaming mode.
§Example
use claude_code::Query;
use serde_json::Map;
use serde_json::Value;
let _ = query.initialize(Map::<String, Value>::new()).await?;Sourcepub fn initialization_result(&self) -> Option<Value>
pub fn initialization_result(&self) -> Option<Value>
Returns the initialization result from the CLI handshake.
Returns None if initialize() has not been called yet.
§Example
use claude_code::Query;
fn demo(query: &Query) {
let _info = query.initialization_result();
}Sourcepub async fn send_raw_message(&self, message: Value) -> Result<()>
pub async fn send_raw_message(&self, message: Value) -> Result<()>
Sourcepub async fn send_input_from_stream<S>(&self, messages: S) -> Result<()>
pub async fn send_input_from_stream<S>(&self, messages: S) -> Result<()>
Sourcepub fn spawn_input_from_stream<S>(
&self,
messages: S,
) -> Result<JoinHandle<Result<()>>>
pub fn spawn_input_from_stream<S>( &self, messages: S, ) -> Result<JoinHandle<Result<()>>>
Spawns a background task that streams input messages to the CLI.
This is useful for long-lived or unbounded input streams where the caller should continue processing messages concurrently.
The returned task completes when the input stream ends or a write error occurs. It does not close stdin.
§Example
use claude_code::Query;
use futures::stream;
use serde_json::json;
let handle = query.spawn_input_from_stream(stream::iter(vec![
json!({"type":"user","message":{"role":"user","content":"hello"}}),
]))?;
handle.await??;Sourcepub async fn stream_input(&self, messages: Vec<Value>) -> Result<()>
pub async fn stream_input(&self, messages: Vec<Value>) -> Result<()>
Streams multiple messages to the CLI and closes the input stream.
If SDK MCP servers or hooks are present, stdin close is deferred until the first result message is received (or a timeout expires).
§Example
use claude_code::Query;
use serde_json::json;
query
.stream_input(vec![json!({"type":"user","message":{"role":"user","content":"hello"}})])
.await?;Sourcepub async fn stream_input_from_stream<S>(&self, messages: S) -> Result<()>
pub async fn stream_input_from_stream<S>(&self, messages: S) -> Result<()>
Sourcepub async fn receive_next_message(&mut self) -> Result<Option<Message>>
pub async fn receive_next_message(&mut self) -> Result<Option<Message>>
Receives the next content message from the CLI.
Messages are delivered by the background reader task via an mpsc channel. Control messages are handled transparently by the background task.
Returns None when the stream is exhausted (no more messages).
§Example
use claude_code::Query;
while let Some(message) = query.receive_next_message().await? {
println!("{message:?}");
}Sourcepub async fn get_mcp_status(&self) -> Result<McpStatusResponse>
pub async fn get_mcp_status(&self) -> Result<McpStatusResponse>
Sourcepub async fn set_permission_mode(&self, mode: &str) -> Result<()>
pub async fn set_permission_mode(&self, mode: &str) -> Result<()>
Sourcepub async fn rewind_files(&self, user_message_id: &str) -> Result<()>
pub async fn rewind_files(&self, user_message_id: &str) -> Result<()>
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.