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 fromsrc/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
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
- Pending: Tool call is created with
tool_callupdate - Executing: Tool is running (optional progress updates)
- Permission Check: For sensitive operations, request user approval
- Completed/Failed: Final status sent via
tool_call_update
Permission Flow
For operations that modify files or perform sensitive actions:- Send initial tool call update
- Call
connection.requestPermission()with options - Wait for user response
- Handle approval or rejection accordingly
Streaming Updates
The agent streams responses in real-time usingsessionUpdate notifications:
agent_message_chunk: Text responsestool_call: New tool executiontool_call_update: Tool status changesagent_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