Skip to main content

Overview

This example demonstrates how to build a functional ACP Client using the TypeScript SDK. The client spawns an agent as a subprocess, establishes a connection, manages sessions, and handles user interactions including permission requests.

What This Example Demonstrates

Agent Process Management

Spawning and communicating with an agent subprocess

Connection Lifecycle

Initializing connections and negotiating capabilities

Permission Handling

Prompting users for approval on sensitive operations

Session Updates

Receiving and displaying real-time agent updates

Complete Code

Here’s the full implementation from src/examples/client.ts:

Code Walkthrough

1

Implement the Client Interface

Create a class that implements the acp.Client interface with all required methods:
2

Handle Permission Requests

Implement interactive permission prompts for sensitive operations:
3

Handle Session Updates

Display real-time updates from the agent:
4

Implement File System Operations

Provide file system capabilities to the agent:
5

Spawn the Agent Process

Launch the agent as a subprocess:
We use npx tsx to run TypeScript files directly. Adjust this based on your agent’s runtime requirements.
6

Create Communication Streams

Set up bidirectional streams for client-agent communication:
7

Initialize the Connection

Create the connection and negotiate capabilities:
8

Create a Session and Send Prompts

Start a new session and interact with the agent:
9

Cleanup

Properly terminate the agent process:

Running the Example

Prerequisites

Ensure you have the SDK installed:

Run the Complete Example

This will:
  1. Spawn the example agent
  2. Initialize the connection
  3. Create a session
  4. Send a test prompt
  5. Display the agent’s response
  6. Handle any permission requests

Expected Output

Key Concepts

Process Management

The client is responsible for:
  • Spawning the agent process
  • Managing stdio streams
  • Handling process lifecycle
  • Cleaning up on exit

Bidirectional Communication

The client and agent communicate through:
  • Client β†’ Agent: Requests (initialize, newSession, prompt)
  • Agent β†’ Client: Notifications (sessionUpdate) and requests (requestPermission)

Capability Negotiation

During initialization, the client declares its capabilities:
Agents can query these capabilities to determine what operations are available.

Permission Model

The client controls what the agent can do:
  • Auto-approved: Read operations, low-risk actions
  • User approval: File modifications, deletions, sensitive operations
  • Blocked: Operations not supported by the client

Session Lifecycle

  1. Initialize: Establish connection and exchange capabilities
  2. New Session: Create isolated conversation context
  3. Prompt: Send user requests and receive responses
  4. Updates: Handle real-time agent notifications
  5. Cleanup: Close session and terminate agent

Platform Considerations

This example handles Windows compatibility by detecting the platform and using the appropriate npx command:

Next Steps

Simple Agent Example

Build an agent that works with this client

API Reference

Explore the full ClientSideConnection API

Production Examples

See how production clients integrate ACP

Client Capabilities

Learn about all available client capabilities