Skip to main content

Overview

This example demonstrates how to build a functional ACP Agent using the TypeScript SDK. The agent implements all required protocol methods and showcases key features including session management, tool calls, and permission requests.

What This Example Demonstrates

Session Management

Creating and managing agent sessions with unique IDs

Tool Calls

Executing tool calls with different permission requirements

Permission Requests

Requesting user approval for sensitive operations

Streaming Updates

Sending real-time text chunks and status updates to the client

Complete Code

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

Code Walkthrough

1

Session State Management

The agent maintains a map of active sessions, each with an AbortController to handle cancellation:
2

Initialize Method

Returns the protocol version and agent capabilities to the client:
3

New Session Method

Creates a unique session ID and initializes session state:
4

Prompt Method

Handles user prompts with cancellation support:
5

Sending Text Chunks

Stream text responses to the client using sessionUpdate:
6

Tool Calls Without Permission

Execute read-only tool calls that don’t require user approval:
7

Permission Requests

Request user permission for sensitive operations:
8

Cancellation Support

Handle cancellation requests from the client:
9

Connection Setup

Initialize the agent connection using stdio streams:

Running the Example

This agent is designed to be launched by an ACP client. It communicates via stdin/stdout using the newline-delimited JSON format.

Option 1: Run with the Example Client

This will automatically spawn the agent and demonstrate a complete interaction.

Option 2: Run from an ACP Client

Configure your ACP client (like Zed) to launch this agent:

Option 3: Test Standalone

While the agent expects client communication via stdio, you can verify it runs:
The agent will wait for JSON-RPC messages on stdin. Without a client, it will simply idle.

Key Concepts

Session Management

Each agent session is independent and has its own state. Sessions are identified by unique IDs and can be cancelled independently.

Tool Call Lifecycle

  1. Pending: Tool call is created with tool_call update
  2. Executing: Tool is running (optional progress updates)
  3. Permission Check: For sensitive operations, request user approval
  4. Completed/Failed: Final status sent via tool_call_update

Permission Flow

For operations that modify files or perform sensitive actions:
  1. Send initial tool call update
  2. Call connection.requestPermission() with options
  3. Wait for user response
  4. Handle approval or rejection accordingly

Streaming Updates

The agent streams responses in real-time using sessionUpdate notifications:
  • agent_message_chunk: Text responses
  • tool_call: New tool execution
  • tool_call_update: Tool status changes
  • agent_thought_chunk: Internal reasoning (optional)

Next Steps

Simple Client Example

See how to build a client that connects to this agent

API Reference

Explore the full AgentSideConnection API

Production Examples

Study real-world agent implementations

Protocol Overview

Learn more about the ACP protocol