Overview
TheClientSideConnection class provides the client’s view of an ACP connection, allowing clients (such as code editors) to communicate with agents. It implements the Agent interface and manages the bidirectional JSON-RPC communication channel.
Class Definition
- Wraps the low-level
Connectionfor client-to-agent communication - Routes incoming agent requests to the appropriate
Clienthandler methods - Provides methods for clients to call agent endpoints (sessions, prompts, authentication)
- Handles protocol-level concerns like request validation and method routing
Constructor
ClientSideConnection
Creates a new client-side connection to an agent.A function that creates a Client handler to process incoming agent requests. The function receives the connection instance and returns a Client implementation.
The bidirectional message stream for communication. Typically created using
ndJsonStream for stdio-based connections.Agent Interface Methods
The following methods implement theAgent interface and allow clients to make requests to the agent.
initialize
Establishes the connection with an agent and negotiates protocol capabilities.The initialization request containing protocol version and client capabilities
Contains the agent’s protocol version, capabilities, and available authentication methods
newSession
Creates a new conversation session with the agent.Request parameters for creating a new session
Contains the unique session ID and session configuration
loadSession
Loads an existing session to resume a previous conversation.Request parameters containing the session ID to load
Contains loaded session information
prompt
Sends a user prompt to the agent for processing.The prompt request containing session ID, messages, and context
Contains the final stop reason and any result data
cancel
Cancels ongoing operations for a session.Notification containing the session ID to cancel
Session Management Methods
setSessionMode
Sets the operational mode for a session.Request containing the session ID and desired mode
Confirms the mode change
setSessionConfigOption
Sets the current value for a session configuration option.Request containing the session ID, option key, and new value
Contains the updated configuration
authenticate
Authenticates the client using the specified authentication method.Request containing the authentication method and credentials
Confirms authentication success
Unstable Methods
unstableListSessions
Lists existing sessions from the agent.Request parameters for listing sessions
List of available sessions
unstableForkSession
Forks an existing session to create a new independent session.Request containing the source session ID and fork parameters
The new forked session information
unstableResumeSession
Resumes an existing session without replaying previous messages.Request containing the session ID to resume
The resumed session information
setSessionModel
Selects the model for a given session.Request containing the session ID and model identifier
Confirms the model selection
Extension Methods
extMethod
Sends an arbitrary request that is not part of the ACP spec.The extension method name
Method parameters
Extension method response
extNotification
Sends an arbitrary notification that is not part of the ACP spec.The extension notification name
Notification parameters
Protocol-Level Methods
sendCancelRequest
Sends the protocol-level$/cancel_request notification.
Cancellation notification with request ID and optional metadata
cancelPendingRequest
Cancels a pending outbound request and sends$/cancel_request.
true if the request ID was still pending locally.
The ID of the request to cancel
Optional metadata to include in the cancellation notification
true if the request was pending and was cancelled, false otherwiseUsage Example
Request Routing
TheClientSideConnection automatically routes incoming JSON-RPC requests to the appropriate Client methods:
Error Handling
The connection automatically handles errors and converts them to JSON-RPC error responses:- Methods returning
nullare treated as not supported and returnMethodNotFound - Unknown methods return
MethodNotFounderrors - Protocol-level notifications starting with
$/may be safely ignored - Extension methods/notifications starting with
_are routed toextMethod()/extNotification()
See Also
- Client - The Client interface implemented by client handlers
- Agent - The Agent interface this class implements
- Connection - The underlying connection class
- AgentSideConnection - The agent-side equivalent