Overview
The ACP SDK allows agents to create terminals and execute commands in the client’s environment. This enables agents to run build tools, tests, scripts, and other command-line operations.Terminal operations are only available if the client advertises the
terminal capability during initialization.Creating Terminals
ThecreateTerminal method executes a command in a new terminal:
string
required
The session ID creating the terminal
string
required
The command to execute
string[]
Optional command arguments
string
Optional working directory for the command
Record<string, string>
Optional environment variables
object
required
A handle to control and monitor the terminal
TerminalHandle that can be used to get output, wait for exit, kill the command, or release the terminal.
Example
TerminalHandle Class
TheTerminalHandle class provides methods to control and monitor a terminal:
currentOutput
Gets the current terminal output without waiting for the command to exit.string
required
The current output from the terminal
number
The exit code if the command has already exited
string
The signal that terminated the process, if applicable
Example
waitForExit
Waits for the terminal command to complete and returns its exit status.number
The exit code of the process
string
The signal that terminated the process, if applicable
Example
kill
Kills the terminal command without releasing the terminal.- Get the final output with
currentOutput() - Check the exit status
- Release the terminal when done
Example
release
Releases the terminal and frees all associated resources.Example with await using
The TerminalHandle class supports async disposal via Symbol.asyncDispose, allowing automatic cleanup:
Embedding Terminals in Tool Calls
Terminals can be embedded in tool calls by using their ID inToolCallContent with type “terminal”:
Error Handling
Handle errors appropriately when working with terminals:Complete Example
Here’s a complete example that demonstrates terminal operations:Best Practices
Always Release
Always call
release() when done to free resources, or use await usingHandle Timeouts
Implement timeouts using
kill() to prevent commands from running indefinitelyCheck Capabilities
Verify the client supports terminal operations before attempting to use them
Capture Output
Get final output after
waitForExit() for complete command results