class
ACP::Client
- ACP::Client
- Reference
- Object
Defined in:
acp/client.crConstructors
-
.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.
Instance Method Summary
-
#agent_capabilities : Protocol::AgentCapabilities | Nil
Agent capabilities received during initialization.
-
#agent_info : Protocol::AgentInfo | Nil
Agent info received during initialization.
-
#auth_methods : Array(JSON::Any) | Nil
Authentication methods the agent supports.
-
#authenticate(method_id : String, credentials : JSON::Any | Nil = nil) : Nil
Authenticates with the agent using the specified method ID.
-
#client_capabilities : Protocol::ClientCapabilities
Client capabilities to advertise during initialization.
-
#client_capabilities=(client_capabilities : Protocol::ClientCapabilities)
Client capabilities to advertise during initialization.
-
#client_info : Protocol::ClientInfo
Client info to send during initialization.
-
#client_info=(client_info : Protocol::ClientInfo)
Client info to send during initialization.
-
#close : Nil
Closes the client, stopping the dispatcher and closing the transport.
-
#closed? : Bool
Returns true if the client has been closed.
-
#initialize_connection : Protocol::InitializeResult
Performs the ACP
initializehandshake with the agent. -
#negotiated_protocol_version : UInt16 | Nil
The protocol version negotiated with the agent.
-
#on_agent_request : AgentRequestHandler | Nil
Callback invoked for agent-initiated method calls that the client must respond to (e.g.,
session/request_permission). -
#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). -
#on_create_terminal : CreateTerminalHandler | Nil
Handler for
terminal/create— create a new terminal and execute a command. -
#on_create_terminal=(on_create_terminal : CreateTerminalHandler | Nil)
Handler for
terminal/create— create a new terminal and execute a command. -
#on_disconnect : -> Nil | Nil
Callback invoked when the transport connection is lost.
-
#on_disconnect=(on_disconnect : -> Nil | Nil)
Callback invoked when the transport connection is lost.
-
#on_kill_terminal : KillTerminalHandler | Nil
Handler for
terminal/kill— kill terminal command without releasing. -
#on_kill_terminal=(on_kill_terminal : KillTerminalHandler | Nil)
Handler for
terminal/kill— kill terminal command without releasing. -
#on_notification : NotificationHandler | Nil
Callback invoked for any notification that is NOT
session/update. -
#on_notification=(on_notification : NotificationHandler | Nil)
Callback invoked for any notification that is NOT
session/update. -
#on_read_text_file : ReadTextFileHandler | Nil
Handler for
fs/read_text_file— read file contents from the editor. -
#on_read_text_file=(on_read_text_file : ReadTextFileHandler | Nil)
Handler for
fs/read_text_file— read file contents from the editor. -
#on_release_terminal : ReleaseTerminalHandler | Nil
Handler for
terminal/release— release a terminal. -
#on_release_terminal=(on_release_terminal : ReleaseTerminalHandler | Nil)
Handler for
terminal/release— release a terminal. -
#on_terminal_output : TerminalOutputHandler | Nil
Handler for
terminal/output— get terminal output and exit status. -
#on_terminal_output=(on_terminal_output : TerminalOutputHandler | Nil)
Handler for
terminal/output— get terminal output and exit status. -
#on_update : UpdateHandler | Nil
Callback invoked for every
session/updatenotification. -
#on_update=(on_update : UpdateHandler | Nil)
Callback invoked for every
session/updatenotification. -
#on_wait_for_terminal_exit : WaitForTerminalExitHandler | Nil
Handler for
terminal/wait_for_exit— wait for terminal command to exit. -
#on_wait_for_terminal_exit=(on_wait_for_terminal_exit : WaitForTerminalExitHandler | Nil)
Handler for
terminal/wait_for_exit— wait for terminal command to exit. -
#on_write_text_file : WriteTextFileHandler | Nil
Handler for
fs/write_text_file— write file contents. -
#on_write_text_file=(on_write_text_file : WriteTextFileHandler | Nil)
Handler for
fs/write_text_file— write file contents. -
#request_timeout : Float64 | Nil
Default timeout for requests (in seconds).
-
#request_timeout=(request_timeout : Float64 | Nil)
Default timeout for requests (in seconds).
-
#respond_to_agent(id : Protocol::RequestId, result : JSON::Any) : Nil
Responds to an agent-initiated request.
-
#respond_to_agent_error(id : Protocol::RequestId, code : Int32, error_message : String) : Nil
Responds to an agent-initiated request with an error.
-
#send_notification(method : String, params : JSON::Serializable) : Nil
Sends a JSON-RPC notification (no response expected).
-
#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.
-
#session_active? : Bool
Returns true if a session is currently active.
-
#session_cancel(session_id : String | Nil = nil) : Nil
Sends a
session/cancelnotification to abort the current operation in the active session. -
#session_config_options : Array(Protocol::ConfigOption) | Nil
Config options available for the current session.
-
#session_id : String | Nil
The active session ID, if any.
-
#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_modes : Protocol::SessionModeState | Nil
Mode state for the current session.
-
#session_new(cwd : String, mcp_servers : Array(JSON::Any) = [] of JSON::Any) : Protocol::SessionNewResult
Creates a new session with the agent.
-
#session_prompt(prompt : Array(Protocol::ContentBlock), session_id : String | Nil = nil) : Protocol::SessionPromptResult
Sends a prompt to the agent in the active session.
-
#session_prompt_text(text : String, session_id : String | Nil = nil) : Protocol::SessionPromptResult
Convenience: sends a simple text prompt.
-
#session_set_config_option(config_id : String, value : String, session_id : String | Nil = nil) : Protocol::SessionSetConfigOptionResult
Changes a configuration option for the active session.
-
#session_set_mode(mode_id : String, session_id : String | Nil = nil) : Nil
Changes the mode for the active session.
-
#state : ClientState
The current client state.
-
#transport : Transport
The transport used for communication with the agent.
Constructor Detail
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.
Instance Method Detail
Agent capabilities received during initialization.
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.
Client capabilities to advertise during initialization.
Client capabilities to advertise during initialization.
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.
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.
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.
Handler for terminal/create — create a new terminal and execute a command.
Set this when advertising the terminal capability.
Handler for terminal/create — create a new terminal and execute a command.
Set this when advertising the terminal capability.
Callback invoked when the transport connection is lost.
Handler for terminal/kill — kill terminal command without releasing.
Set this when advertising the terminal capability.
Handler for terminal/kill — kill terminal command without releasing.
Set this when advertising the terminal capability.
Callback invoked for any notification that is NOT session/update.
Useful for handling custom or future notification types.
Callback invoked for any notification that is NOT session/update.
Useful for handling custom or future notification types.
Handler for fs/read_text_file — read file contents from the editor.
Set this when advertising fs.readTextFile capability.
Handler for fs/read_text_file — read file contents from the editor.
Set this when advertising fs.readTextFile capability.
Handler for terminal/release — release a terminal.
Set this when advertising the terminal capability.
Handler for terminal/release — release a terminal.
Set this when advertising the terminal capability.
Handler for terminal/output — get terminal output and exit status.
Set this when advertising the terminal capability.
Handler for terminal/output — get terminal output and exit status.
Set this when advertising the terminal capability.
Callback invoked for every session/update notification.
Set this to handle streaming agent responses, tool calls, etc.
Callback invoked for every session/update notification.
Set this to handle streaming agent responses, tool calls, etc.
Handler for terminal/wait_for_exit — wait for terminal command to exit.
Set this when advertising the terminal capability.
Handler for terminal/wait_for_exit — wait for terminal command to exit.
Set this when advertising the terminal capability.
Handler for fs/write_text_file — write file contents.
Set this when advertising fs.writeTextFile capability.
Handler for fs/write_text_file — write file contents.
Set this when advertising fs.writeTextFile capability.
Default timeout for requests (in seconds). Nil means no timeout.
Default timeout for requests (in seconds). Nil means no timeout.
Responds to an agent-initiated request.
id— the request ID from the agent's request.result— the result to send back.
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.
Sends a JSON-RPC notification (no response expected).
method— the notification method name.params— the notification parameters.
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.
Sends a session/cancel notification to abort the current
operation in the active session.
#session_id— optional session ID override.
This is a fire-and-forget notification (no response expected).
Config options available for the current session.
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.
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.
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.
Convenience: sends a simple text prompt.
text— the text content to send as a prompt.#session_id— optional session ID override.
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
Changes the mode for the active session.
mode_id— the mode ID to switch to.#session_id— optional session ID override.