Skip to main content
Connections are the foundation of communication in ACP. The SDK provides two connection classes that handle bidirectional message passing between agents and clients.

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

The AgentSideConnection 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

The ClientSideConnection 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 the Stream interface for message passing:
src/stream.ts

Creating Streams

The SDK provides ndJsonStream() 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
Once a connection is closed, it cannot be reopened. You must create a new connection.

Closing Connections

Connections close when:
  1. Stream ends normally - Agent process exits or stream is closed
  2. Error occurs - Network error, parse error, or protocol violation
  3. 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
Always call release() on terminals to free resources, or use await using for automatic cleanup.

Best Practices

Always monitor connection status and handle cleanup:
The SDK automatically validates messages using Zod schemas. Validation errors are returned as JSON-RPC errors.
Send frequent updates to keep users informed:
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