Class: ClaudeAgentSDK::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/claude_agent_sdk.rb

Overview

Client for bidirectional, interactive conversations with Claude Code

This client provides full control over the conversation flow with support for streaming, hooks, permission callbacks, and dynamic message sending. The Client class always uses streaming mode for bidirectional communication.

Examples:

Basic usage

Async do
  client = ClaudeAgentSDK::Client.new
  client.connect  # No arguments needed - automatically uses streaming mode

  client.query("What is the capital of France?")
  client.receive_response do |msg|
    puts msg if msg.is_a?(ClaudeAgentSDK::AssistantMessage)
  end

  client.disconnect
end

With hooks

options = ClaudeAgentOptions.new(
  hooks: {
    'PreToolUse' => [
      HookMatcher.new(
        matcher: 'Bash',
        hooks: [
          ->(input, tool_use_id, context) {
            # Return hook output
            {}
          }
        ]
      )
    ]
  }
)
client = ClaudeAgentSDK::Client.new(options: options)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options: nil, transport_class: SubprocessCLITransport, transport_args: {}) ⇒ Client

Returns a new instance of Client.

Parameters:

  • options (ClaudeAgentOptions, nil) (defaults to: nil)

    Configuration options

  • transport_class (Class) (defaults to: SubprocessCLITransport)

    Transport class to use (must implement Transport interface). Defaults to SubprocessCLITransport.

  • transport_args (Hash) (defaults to: {})

    Additional keyword arguments passed to transport_class.new(options, **transport_args)



613
614
615
616
617
618
619
620
621
# File 'lib/claude_agent_sdk.rb', line 613

def initialize(options: nil, transport_class: SubprocessCLITransport, transport_args: {})
  @options = options || ClaudeAgentOptions.new
  @transport_class = transport_class
  @transport_args = transport_args
  @transport = nil
  @query_handler = nil
  @connected = false
  @materialized = nil
end

Instance Attribute Details

#query_handlerObject (readonly)

Returns the value of attribute query_handler.



607
608
609
# File 'lib/claude_agent_sdk.rb', line 607

def query_handler
  @query_handler
end

Class Method Details

.open(prompt = nil, options: nil, transport_class: SubprocessCLITransport, transport_args: {}) ⇒ Object

Note:

In standalone (non-Async) use, break inside the block raises LocalJumpError (teardown still runs) — return a value instead.

Block-scoped Client lifecycle, mirroring Python's async with ClaudeSDKClient() as client and File.open ergonomics: connects, yields the client, and always disconnects (block exceptions propagate). Kernel#Sync runs inline inside an existing reactor and creates one otherwise, so this works standalone too. Returns the block's value.

Examples:

ClaudeAgentSDK::Client.open(options: options) do |client|
  client.query('Hello')
  client.receive_response { |msg| puts msg }
end

Parameters:

  • prompt (String, Enumerator, nil) (defaults to: nil)

    Optional initial prompt (same as #connect)

Raises:

  • (ArgumentError)


638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
# File 'lib/claude_agent_sdk.rb', line 638

def self.open(prompt = nil, options: nil, transport_class: SubprocessCLITransport, transport_args: {})
  raise ArgumentError, 'Client.open requires a block' unless block_given?

  Sync do
    client = new(options: options, transport_class: transport_class, transport_args: transport_args)
    # connect failures self-clean via connect's rescue -> disconnect ->
    # raise, and disconnect is idempotent — no double-teardown.
    client.connect(prompt)
    begin
      yield client
    ensure
      client.disconnect
    end
  end
end

Instance Method Details

#connect(prompt = nil) ⇒ Object

Connect to Claude with optional initial prompt.

Client always uses streaming mode for bidirectional communication. If you pass a String, it will be sent as an initial user message after the connection is established. If you pass an Enumerator, it should yield JSONL messages (e.g., from ClaudeAgentSDK::Streaming.user_message); the stream is consumed in the BACKGROUND (connect returns immediately) and stdin closes when it is exhausted, so the stream is the session's input — a later #query after exhaustion will fail. Enumerator code runs on the reactor: use a producer Thread + Thread::Queue for blocking reads (Queue#pop is scheduler-aware). Stream errors are reported via Observer#on_error and logged, not raised out of connect.

Parameters:

  • prompt (String, Enumerator, nil) (defaults to: nil)

    Initial prompt or message stream

Raises:

  • (ArgumentError)


668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
# File 'lib/claude_agent_sdk.rb', line 668

def connect(prompt = nil)
  return if @connected

  raise ArgumentError, "prompt must be a String, an Enumerator, or nil (got #{prompt.class})" unless prompt.nil? || prompt.is_a?(String) || prompt.respond_to?(:each)

  # Validate and configure permission settings
  configured_options = @options
  if @options.can_use_tool
    # can_use_tool and permission_prompt_tool_name are mutually exclusive
    raise ArgumentError, 'can_use_tool callback cannot be used with permission_prompt_tool_name' if @options.permission_prompt_tool_name

    # Set permission_prompt_tool_name to stdio for control protocol
    configured_options = @options.dup_with(permission_prompt_tool_name: 'stdio')
  end

  # Fail fast on invalid session_store combinations before spawning the CLI.
  # Configuration validation is a usage error, like the ArgumentErrors
  # above — deliberately outside the on_error notify scope.
  SessionStores.validate_session_store_options(configured_options)

  # Resolve observers before the first failable runtime step so
  # connect-phase failures (including resume materialization) can be
  # notified via on_error.
  @resolved_observers = ClaudeAgentSDK.resolve_observers(@options.observers)

  # If anything from materialization onward fails, tear down (closes the
  # subprocess and removes the materialized temp config dir) before
  # surfacing the error, so a partial connect never leaks a temp dir
  # holding a credential copy.
  begin
    # Resume-from-store: materialize the session from the store into a
    # temp CLAUDE_CONFIG_DIR BEFORE spawn, then repoint options at it.
    # Inside the instrumented begin so store IO failures fire on_error
    # (matching the one-shot query() path) and disconnect cleans up.
    configured_options = materialize_resume(configured_options)

    connect_inner(configured_options, prompt)
  rescue Exception => e # rubocop:disable Lint/RescueException
    # Pre-handshake failures (@connected still false) are notified here;
    # post-handshake String-prompt send failures were already notified by
    # the instrumented #query — the gate keeps on_error exactly-once.
    # (The enumerator branch streams in the background and cannot raise
    # out of connect.) No on_close follows for pre-handshake failures
    # (disconnect gates it on @connected): the session never opened.
    notify_error(e) if e.is_a?(StandardError) && !@connected
    # Tear down the partial connect, but never let a cleanup failure (e.g. a
    # custom transport whose #close raises) mask the original connect error.
    # Rescue Exception (not StandardError) so reactor cancellation
    # (Async::Stop < Exception) after materialize_resume set @materialized
    # still runs disconnect -> @materialized.cleanup, never leaking the temp
    # CLAUDE_CONFIG_DIR that holds the redacted .credentials.json copy.
    begin
      disconnect
    rescue StandardError => cleanup_error
      warn "Claude SDK: cleanup after failed connect raised: #{cleanup_error.message}"
    end
    raise
  end
end

#disconnectObject

Disconnect from Claude



904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
# File 'lib/claude_agent_sdk.rb', line 904

def disconnect
  ClaudeAgentSDK.notify_observers(@resolved_observers || [], :on_close) if @connected
  # Tear down whatever exists — robust to a partial/failed connect, where
  # @connected is still false but a transport and/or materialized temp dir
  # were already created. #close on the query handler also closes the
  # transport (flushing the mirror batcher first); the extra @transport
  # close covers a failure before the query handler was built (idempotent).
  #
  # The nested ensures guarantee that even a raising close (e.g. a custom
  # transport whose #close raises) still runs the transport close, resets
  # state, and removes the materialized temp dir (which holds a redacted
  # .credentials.json copy) — so disconnect can never leave the client
  # half-open or leak the temp dir. The original error still propagates.
  # Keep a handle on the query handler past the nil-out below: whether the
  # mirror dropped batches is only final AFTER #close ran its last flush,
  # and the materialized-dir decision at the bottom needs to ask it.
  query_handler = @query_handler
  begin
    @query_handler&.close
  ensure
    @query_handler = nil
    begin
      @transport&.close
    ensure
      @transport = nil
      @connected = false
      # Remove the materialized resume temp dir AFTER the subprocess
      # exited — unless the mirror dropped batches: the store copy is then
      # incomplete and the temp dir holds the only copy of the dropped
      # turns, so it is preserved (scrubbed of credentials) with a warning
      # instead of deleted.
      if @materialized
        if query_handler&.mirror_batches_dropped?
          @materialized.preserve_transcripts
        else
          @materialized.cleanup
        end
        @materialized = nil
      end
    end
  end
end

#get_context_usageHash

Get a breakdown of current context window usage by category. Returns token counts per category (system prompt, tools, messages, etc.), total/max tokens, model info, MCP tools, memory files, and more.

Returns:

  • (Hash)

    Context usage response

Raises:



884
885
886
887
# File 'lib/claude_agent_sdk.rb', line 884

def get_context_usage
  raise CLIConnectionError, 'Not connected. Call connect() first' unless @connected
  @query_handler.get_context_usage
end

#get_mcp_statusHash

Get current MCP server connection status (only works with streaming mode)

Returns:

  • (Hash)

    MCP status information, including mcpServers list

Raises:



891
892
893
894
# File 'lib/claude_agent_sdk.rb', line 891

def get_mcp_status
  raise CLIConnectionError, 'Not connected. Call connect() first' unless @connected
  @query_handler.get_mcp_status
end

#get_server_infoHash

Get server initialization info including available commands and output styles

Returns:

  • (Hash)

    Server info

Raises:



898
899
900
901
# File 'lib/claude_agent_sdk.rb', line 898

def get_server_info
  raise CLIConnectionError, 'Not connected. Call connect() first' unless @connected
  server_info
end

#interruptObject

Send interrupt signal

Raises:



824
825
826
827
# File 'lib/claude_agent_sdk.rb', line 824

def interrupt
  raise CLIConnectionError, 'Not connected. Call connect() first' unless @connected
  @query_handler.interrupt
end

#query(prompt, session_id: 'default') ⇒ Object

Send a query to Claude

Parameters:

  • prompt (String, Enumerable)

    The prompt to send — a String, or an Enumerable of message Hashes / JSONL Strings streamed inline (blocks until exhausted, like Python's async-for). Hashes lacking a session_id are stamped with the session_id: argument; JSONL Strings pass through VERBATIM — generate them with the matching session_id (Streaming.user_message defaults to 'default'). Bare Hashes are rejected (they would iterate as key-value pairs).

  • session_id (String) (defaults to: 'default')

    Session identifier

Raises:



737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
# File 'lib/claude_agent_sdk.rb', line 737

def query(prompt, session_id: 'default')
  raise CLIConnectionError, 'Not connected. Call connect() first' unless @connected
  # A bare Hash responds to #each and would silently iterate [key, value]
  # pairs (Python's async-for over a dict raises TypeError).
  raise ArgumentError, 'prompt must be a String or an Enumerable of message Hashes/JSONL Strings (got Hash)' if prompt.is_a?(Hash)

  begin
    if prompt.is_a?(String)
      ClaudeAgentSDK.notify_observers(@resolved_observers, :on_user_prompt, prompt)
      message = {
        type: 'user',
        message: { role: 'user', content: prompt },
        parent_tool_use_id: nil,
        session_id: session_id
      }
      writeln(JSON.generate(message))
    elsif prompt.respond_to?(:each)
      # Inline iteration on the caller, Python client.py parity — NOT
      # Query#stream_input, whose ensure always ends input after
      # exhaustion (correct for connect-time sole-input streams, fatal
      # for a mid-session query). Blocks until the iterable is exhausted,
      # identical to Python's async-for.
      stream_query_messages(prompt, session_id)
    else
      raise ArgumentError, "prompt must be a String or respond to #each (got #{prompt.class})"
    end
  rescue StandardError => e
    notify_error(e)
    raise
  end
end

#receive_messages {|Message| ... } ⇒ Enumerator

Note:

#next/#peek either raise FiberError or hang depending on message timing, and can kill the session's read loop, leaving the client unusable; iterate with a block or each-driven Enumerable methods (#first, #take) inside the Async block instead.

Receive all messages from Claude

Yields:

  • (Message)

    Each message received

Returns:

  • (Enumerator)

    when no block is given (internal iteration only)

Raises:



776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
# File 'lib/claude_agent_sdk.rb', line 776

def receive_messages(&block)
  return enum_for(:receive_messages) unless block

  raise CLIConnectionError, 'Not connected. Call connect() first' unless @connected

  begin
    @query_handler.receive_messages do |data|
      message = MessageParser.parse(data)
      next unless message

      ClaudeAgentSDK.notify_observers(@resolved_observers, :on_message, message)
      signal = FiberBoundary.invoke_iteration(block, message)
      break signal.value if signal.is_a?(FiberBoundary::Break)
    end
  rescue StandardError => e
    notify_error(e)
    raise
  end
end

#receive_response {|Message| ... } ⇒ Object

Receive messages until a ResultMessage is received

Yields:

  • (Message)

    Each message received

Raises:



798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
# File 'lib/claude_agent_sdk.rb', line 798

def receive_response(&block)
  return enum_for(:receive_response) unless block

  raise CLIConnectionError, 'Not connected. Call connect() first' unless @connected

  # Keep loop control on the same fiber as the underlying dequeue: both
  # the SDK's ResultMessage break and the user's translated break happen
  # here, never inside the FiberBoundary hop (break in a proc on a
  # foreign thread raises LocalJumpError).
  begin
    @query_handler.receive_messages do |data|
      message = MessageParser.parse(data)
      next unless message

      ClaudeAgentSDK.notify_observers(@resolved_observers, :on_message, message)
      signal = FiberBoundary.invoke_iteration(block, message)
      break signal.value if signal.is_a?(FiberBoundary::Break)
      break if message.is_a?(ResultMessage)
    end
  rescue StandardError => e
    notify_error(e)
    raise
  end
end

#reconnect_mcp_server(server_name) ⇒ Object

Reconnect a failed MCP server

Parameters:

  • server_name (String)

    Name of the MCP server to reconnect

Raises:



845
846
847
848
# File 'lib/claude_agent_sdk.rb', line 845

def reconnect_mcp_server(server_name)
  raise CLIConnectionError, 'Not connected. Call connect() first' unless @connected
  @query_handler.reconnect_mcp_server(server_name)
end

#rewind_files(user_message_uuid) ⇒ Object

Rewind files to a previous checkpoint (v0.1.15+) Restores file state to what it was at the given user message Requires enable_file_checkpointing to be true in options

Parameters:

  • user_message_uuid (String)

    The UUID of the UserMessage to rewind to

Raises:



869
870
871
872
# File 'lib/claude_agent_sdk.rb', line 869

def rewind_files(user_message_uuid)
  raise CLIConnectionError, 'Not connected. Call connect() first' unless @connected
  @query_handler.rewind_files(user_message_uuid)
end

#server_infoHash?

Get server initialization info

Returns:

  • (Hash, nil)

    Server info or nil



876
877
878
# File 'lib/claude_agent_sdk.rb', line 876

def server_info
  @query_handler&.instance_variable_get(:@initialization_result)
end

#set_model(model) ⇒ Object

Change the AI model during conversation

Parameters:

  • model (String, nil)

    Model name or nil for default

Raises:



838
839
840
841
# File 'lib/claude_agent_sdk.rb', line 838

def set_model(model)
  raise CLIConnectionError, 'Not connected. Call connect() first' unless @connected
  @query_handler.set_model(model)
end

#set_permission_mode(mode) ⇒ Object

Change permission mode during conversation

Parameters:

  • mode (String)

    Permission mode ('default', 'acceptEdits', 'bypassPermissions')

Raises:



831
832
833
834
# File 'lib/claude_agent_sdk.rb', line 831

def set_permission_mode(mode)
  raise CLIConnectionError, 'Not connected. Call connect() first' unless @connected
  @query_handler.set_permission_mode(mode)
end

#stop_task(task_id) ⇒ Object

Stop a running background task

Parameters:

  • task_id (String)

    The ID of the task to stop

Raises:



860
861
862
863
# File 'lib/claude_agent_sdk.rb', line 860

def stop_task(task_id)
  raise CLIConnectionError, 'Not connected. Call connect() first' unless @connected
  @query_handler.stop_task(task_id)
end

#toggle_mcp_server(server_name, enabled) ⇒ Object

Enable or disable an MCP server

Parameters:

  • server_name (String)

    Name of the MCP server

  • enabled (Boolean)

    Whether to enable or disable

Raises:



853
854
855
856
# File 'lib/claude_agent_sdk.rb', line 853

def toggle_mcp_server(server_name, enabled)
  raise CLIConnectionError, 'Not connected. Call connect() first' unless @connected
  @query_handler.toggle_mcp_server(server_name, enabled)
end