class ACP::Client

Defined in:

acp/client.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(transport : Transport, client_name : String = "acp-crystal", client_version : String = ACP::VERSION, client_capabilities : Protocol::ClientCapabilities = Protocol::ClientCapabilities.new) #

Creates a new ACP client connected to the given transport.

  • #transport — the transport to use for communication.
  • client_name — human-readable name of the client application.
  • client_version — version string of the client.
  • capabilities — client capabilities to advertise. Default: minimal.

[View source]

Instance Method Detail

def agent_capabilities : Protocol::AgentCapabilities | Nil #

Agent capabilities received during initialization.


[View source]
def agent_info : Protocol::AgentInfo | Nil #

Agent info received during initialization.


[View source]
def auth_methods : Array(JSON::Any) | Nil #

Authentication methods the agent supports.


[View source]
def authenticate(method_id : String, credentials : JSON::Any | Nil = nil) : Nil #

Authenticates with the agent using the specified method ID.

This should be called after #initialize_connection if the agent reported authMethods in its initialize response.

Raises InvalidStateError if not in the Initialized state. Raises JsonRpcError on authentication failure.


[View source]
def client_capabilities : Protocol::ClientCapabilities #

Client capabilities to advertise during initialization.


[View source]
def client_capabilities=(client_capabilities : Protocol::ClientCapabilities) #

Client capabilities to advertise during initialization.


[View source]
def client_info : Protocol::ClientInfo #

Client info to send during initialization.


[View source]
def client_info=(client_info : Protocol::ClientInfo) #

Client info to send during initialization.


[View source]
def close : Nil #

Closes the client, stopping the dispatcher and closing the transport.


[View source]
def closed? : Bool #

Returns true if the client has been closed.


[View source]
def initialize_connection : Protocol::InitializeResult #

Performs the ACP initialize handshake with the agent.

Sends the client's protocol version, capabilities, and info. Receives the agent's protocol version, capabilities, auth methods, and info. Validates protocol version compatibility.

Raises VersionMismatchError if the agent's protocol version is incompatible. Raises JsonRpcError if the agent returns an error.


[View source]
def negotiated_protocol_version : UInt16 | Nil #

The protocol version negotiated with the agent.


[View source]
def on_agent_request : AgentRequestHandler | Nil #

Callback invoked for agent-initiated method calls that the client must respond to (e.g., session/request_permission). The handler receives the method name and raw params, and must return the result as JSON::Any. If not set, agent requests receive an error.


[View source]
def on_agent_request=(on_agent_request : AgentRequestHandler | Nil) #

Callback invoked for agent-initiated method calls that the client must respond to (e.g., session/request_permission). The handler receives the method name and raw params, and must return the result as JSON::Any. If not set, agent requests receive an error.


[View source]
def on_create_terminal : CreateTerminalHandler | Nil #

Handler for terminal/create — create a new terminal and execute a command. Set this when advertising the terminal capability.


[View source]
def on_create_terminal=(on_create_terminal : CreateTerminalHandler | Nil) #

Handler for terminal/create — create a new terminal and execute a command. Set this when advertising the terminal capability.


[View source]
def on_disconnect : -> Nil | Nil #

Callback invoked when the transport connection is lost.


[View source]
def on_disconnect=(on_disconnect : -> Nil | Nil) #

Callback invoked when the transport connection is lost.


[View source]
def on_kill_terminal : KillTerminalHandler | Nil #

Handler for terminal/kill — kill terminal command without releasing. Set this when advertising the terminal capability.


[View source]
def on_kill_terminal=(on_kill_terminal : KillTerminalHandler | Nil) #

Handler for terminal/kill — kill terminal command without releasing. Set this when advertising the terminal capability.


[View source]
def on_notification : NotificationHandler | Nil #

Callback invoked for any notification that is NOT session/update. Useful for handling custom or future notification types.


[View source]
def on_notification=(on_notification : NotificationHandler | Nil) #

Callback invoked for any notification that is NOT session/update. Useful for handling custom or future notification types.


[View source]
def on_read_text_file : ReadTextFileHandler | Nil #

Handler for fs/read_text_file — read file contents from the editor. Set this when advertising fs.readTextFile capability.


[View source]
def on_read_text_file=(on_read_text_file : ReadTextFileHandler | Nil) #

Handler for fs/read_text_file — read file contents from the editor. Set this when advertising fs.readTextFile capability.


[View source]
def on_release_terminal : ReleaseTerminalHandler | Nil #

Handler for terminal/release — release a terminal. Set this when advertising the terminal capability.


[View source]
def on_release_terminal=(on_release_terminal : ReleaseTerminalHandler | Nil) #

Handler for terminal/release — release a terminal. Set this when advertising the terminal capability.


[View source]
def on_terminal_output : TerminalOutputHandler | Nil #

Handler for terminal/output — get terminal output and exit status. Set this when advertising the terminal capability.


[View source]
def on_terminal_output=(on_terminal_output : TerminalOutputHandler | Nil) #

Handler for terminal/output — get terminal output and exit status. Set this when advertising the terminal capability.


[View source]
def on_update : UpdateHandler | Nil #

Callback invoked for every session/update notification. Set this to handle streaming agent responses, tool calls, etc.


[View source]
def on_update=(on_update : UpdateHandler | Nil) #

Callback invoked for every session/update notification. Set this to handle streaming agent responses, tool calls, etc.


[View source]
def on_wait_for_terminal_exit : WaitForTerminalExitHandler | Nil #

Handler for terminal/wait_for_exit — wait for terminal command to exit. Set this when advertising the terminal capability.


[View source]
def on_wait_for_terminal_exit=(on_wait_for_terminal_exit : WaitForTerminalExitHandler | Nil) #

Handler for terminal/wait_for_exit — wait for terminal command to exit. Set this when advertising the terminal capability.


[View source]
def on_write_text_file : WriteTextFileHandler | Nil #

Handler for fs/write_text_file — write file contents. Set this when advertising fs.writeTextFile capability.


[View source]
def on_write_text_file=(on_write_text_file : WriteTextFileHandler | Nil) #

Handler for fs/write_text_file — write file contents. Set this when advertising fs.writeTextFile capability.


[View source]
def request_timeout : Float64 | Nil #

Default timeout for requests (in seconds). Nil means no timeout.


[View source]
def request_timeout=(request_timeout : Float64 | Nil) #

Default timeout for requests (in seconds). Nil means no timeout.


[View source]
def respond_to_agent(id : Protocol::RequestId, result : JSON::Any) : Nil #

Responds to an agent-initiated request.

  • id — the request ID from the agent's request.
  • result — the result to send back.

[View source]
def respond_to_agent_error(id : Protocol::RequestId, code : Int32, error_message : String) : Nil #

Responds to an agent-initiated request with an error.

  • id — the request ID from the agent's request.
  • code — the JSON-RPC error code.
  • error_message — human-readable error description.

[View source]
def send_notification(method : String, params : JSON::Serializable) : Nil #

Sends a JSON-RPC notification (no response expected).

  • method — the notification method name.
  • params — the notification parameters.

[View source]
def send_request(method : String, params : JSON::Serializable, timeout : Float64 | Nil | Nil = @request_timeout) : JSON::Any #

Sends a JSON-RPC request and waits for the response.

  • method — the RPC method name.
  • params — the request parameters (JSON::Serializable).
  • timeout — optional timeout in seconds (nil = use default).

Returns the result field from the response as JSON::Any. Raises JsonRpcError if the response contains an error. Raises RequestTimeoutError if the request times out. Raises ConnectionClosedError if the transport is closed.


[View source]
def session_active? : Bool #

Returns true if a session is currently active.


[View source]
def session_cancel(session_id : String | Nil = nil) : Nil #

Sends a session/cancel notification to abort the current operation in the active session.

This is a fire-and-forget notification (no response expected).


[View source]
def session_config_options : Array(Protocol::ConfigOption) | Nil #

Config options available for the current session.


[View source]
def session_id : String | Nil #

The active session ID, if any.


[View source]
def session_load(session_id : String, cwd : String, mcp_servers : Array(JSON::Any) = [] of JSON::Any) : Protocol::SessionLoadResult #

Loads (resumes) a previous session by ID.

  • #session_id — the session ID to load.
  • cwd — optional working directory override.

Raises InvalidStateError if not initialized. Raises JsonRpcError if the agent can't load the session.


[View source]
def session_modes : Protocol::SessionModeState | Nil #

Mode state for the current session.


[View source]
def session_new(cwd : String, mcp_servers : Array(JSON::Any) = [] of JSON::Any) : Protocol::SessionNewResult #

Creates a new session with the agent.

  • cwd — the absolute path to the working directory.
  • mcp_servers — optional list of MCP servers to connect to.

Returns the session creation result with the session ID, modes, and config options. Stores the session ID for subsequent calls.

Raises InvalidStateError if not initialized.


[View source]
def session_prompt(prompt : Array(Protocol::ContentBlock), session_id : String | Nil = nil) : Protocol::SessionPromptResult #

Sends a prompt to the agent in the active session.

  • prompt — an array of content blocks forming the prompt.
  • #session_id — optional session ID override (uses active session if nil).

This is a blocking call that waits for the agent to finish processing the prompt. While waiting, session/update notifications will be dispatched to the #on_update callback.

Returns the prompt result with the stop reason.

Raises NoActiveSessionError if no session is active.


[View source]
def session_prompt_text(text : String, session_id : String | Nil = nil) : Protocol::SessionPromptResult #

Convenience: sends a simple text prompt.

  • text — the text content to send as a prompt.
  • #session_id — optional session ID override.

[View source]
def session_set_config_option(config_id : String, value : String, session_id : String | Nil = nil) : Protocol::SessionSetConfigOptionResult #

Changes a configuration option for the active session.

  • config_id — the ID of the configuration option to change.
  • value — the new value to set.
  • #session_id — optional session ID override.

Returns the full set of configuration options and their current values. See: https://agentclientprotocol.com/protocol/session-config-options#from-the-client


[View source]
def session_set_mode(mode_id : String, session_id : String | Nil = nil) : Nil #

Changes the mode for the active session.

  • mode_id — the mode ID to switch to.
  • #session_id — optional session ID override.

[View source]
def state : ClientState #

The current client state.


[View source]
def transport : Transport #

The transport used for communication with the agent.


[View source]