Connection Classes
The SDK provides two connection classes, one for each side:AgentSideConnection
Used by agents to communicate with clients
ClientSideConnection
Used by clients to communicate with agents
AgentSideConnection
TheAgentSideConnection class provides the agent’s view of an ACP connection.
Creating a Connection
src/acp.ts
The agent factory function receives the connection instance, allowing your agent to send requests back to the client.
Agent → Client Methods
The connection provides methods for agents to communicate with clients:ClientSideConnection
TheClientSideConnection class provides the client’s view of an ACP connection.
Creating a Connection
src/acp.ts
Client → Agent Methods
Stream-Based Communication
Both connection classes use theStream interface for message passing:
src/stream.ts
Creating Streams
The SDK providesndJsonStream() to create streams from stdin/stdout:
Messages are sent as newline-delimited JSON (NDJSON), with each message on a single line.
Message Format
All messages follow the JSON-RPC 2.0 format:Connection Lifecycle
Connections have a well-defined lifecycle with monitoring capabilities:Initialization Flow
Monitoring Connection Status
Both connection classes provide properties to monitor connection status:src/acp.ts
Closing Connections
Connections close when:- Stream ends normally - Agent process exits or stream is closed
- Error occurs - Network error, parse error, or protocol violation
- Abort signal triggered - External cancellation
Error Handling
Connections handle errors according to JSON-RPC 2.0 specification:Error Response Format
Extension Methods
Both connection classes support custom methods beyond the ACP specification:Extension methods should be prefixed with a unique identifier to avoid conflicts (e.g., domain name).
Terminal Handling
Agents can create and manage terminals through the connection:src/acp.ts
Best Practices
Handle Connection Closure
Handle Connection Closure
Always monitor connection status and handle cleanup:
Validate Messages
Validate Messages
The SDK automatically validates messages using Zod schemas. Validation errors are returned as JSON-RPC errors.
Stream Progress
Stream Progress
Send frequent updates to keep users informed:
Use AbortSignal
Use AbortSignal
Pass the connection signal to operations for automatic cancellation:
Learn More
Protocol Overview
Understanding the ACP specification
Agents and Clients
Learn about the two sides of ACP
Sessions
Managing conversation contexts
Build an Agent
Complete agent implementation guide