ClaudeAgentSDK.Query.ClientStream (claude_agent_sdk v0.6.9)

View Source

Wraps the ClaudeAgentSDK.Client GenServer to provide a Stream interface.

This module enables ClaudeAgentSDK.query/2 to work with SDK MCP servers (and other control-protocol features like hooks and permission callbacks) by using the control client internally while maintaining the same Stream API.

Why This Exists

SDK MCP servers require bidirectional communication (control protocol) to work. The simple Process.stream approach is unidirectional and cannot handle control_request messages from the CLI. This module bridges the gap by:

  1. Starting a Client GenServer (which handles control protocol)
  2. Sending the query message
  3. Wrapping Client.stream_messages as a Stream
  4. Cleaning up the Client when done

Usage

This module is used internally by ClaudeAgentSDK.Query and should not be called directly. Use ClaudeAgentSDK.query/2 as normal.

Summary

Functions

Creates a Stream backed by a Client GenServer.

Functions

stream(prompt, options)

Creates a Stream backed by a Client GenServer.

This function starts a Client, sends the prompt, and returns a Stream that yields ClaudeAgentSDK.Message structs from the client's mailbox. The Client is automatically stopped after the stream is exhausted.

Parameters

  • prompt - The prompt to send to Claude
  • options - Configuration options (must contain SDK MCP servers)

Returns

A Stream of ClaudeAgentSDK.Message structs.