Overview
TheAgentSideConnection class provides the agent’s view of an ACP connection, allowing agents to communicate with clients. It implements the Client interface and manages the bidirectional JSON-RPC communication channel.
Class Definition
- Wraps the low-level
Connectionfor agent-to-client communication - Routes incoming client requests to the appropriate
Agenthandler methods - Provides methods for agents to call client endpoints (file system, terminals, permissions)
- Handles protocol-level concerns like request validation and method routing
Constructor
AgentSideConnection
Creates a new agent-side connection to a client.A function that creates an Agent handler to process incoming client requests. The function receives the connection instance and returns an Agent implementation.
The bidirectional message stream for communication. Typically created using
ndJsonStream for stdio-based connections.Client Interface Methods
The following methods implement theClient interface and allow agents to make requests to the client.
requestPermission
Requests permission from the user for a tool call operation.The permission request with tool details and options
The user’s permission decision
sessionUpdate
Sends session update notifications to the client.Notification containing session updates, messages, and tool calls
readTextFile
Reads content from a text file in the client’s file system.Request containing the file path to read
The file content
writeTextFile
Writes content to a text file in the client’s file system.Request containing the file path and content to write
Confirmation of the write operation
Terminal Methods
createTerminal
Creates a new terminal to execute a command.Request containing the command and terminal configuration
The terminal ID for future operations
terminalOutput
Gets the current output and exit status of a terminal.Request containing the session ID and terminal ID
Current stdout, stderr, and exit status
releaseTerminal
Releases a terminal and frees all associated resources.Request containing the session ID and terminal ID
Confirmation of terminal release
waitForTerminalExit
Waits for a terminal command to exit and returns its exit status.Request containing the session ID and terminal ID
Exit code and signal information
killTerminal
Kills a terminal command without releasing the terminal.Request containing the session ID and terminal ID
Confirmation of terminal termination
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
TheAgentSideConnection automatically routes incoming JSON-RPC requests to the appropriate Agent methods:
Error Handling
The connection automatically handles errors and converts them to JSON-RPC error responses:RequestErrorinstances are sent directly with their error codesTypeError,FormatException,ArgumentErrorare converted toInvalidParamserrors- Other exceptions become
InternalErrorresponses - Unknown methods return
MethodNotFounderrors
See Also
- Agent - The Agent interface implemented by agent handlers
- Client - The Client interface this class implements
- Connection - The underlying connection class
- ClientSideConnection - The client-side equivalent