Skip to main content

Overview

Terminal capabilities allow agents to execute shell commands in the client’s environment. Terminals are created, monitored, and eventually released through a series of client methods.

Advertising Terminal Support

Declare terminal support during initialization:
Terminal execution poses significant security risks. Implement careful validation and sandboxing.

Terminal Lifecycle

  1. Create: Agent calls createTerminal to start a command
  2. Monitor: Agent calls terminalOutput to get current output
  3. Wait: Agent calls waitForTerminalExit to wait for completion
  4. Kill (optional): Agent calls killTerminal to terminate the command
  5. Release: Agent calls releaseTerminal to free resources

createTerminal()

Creates a new terminal to execute a command.

Method Signature

string
The session creating the terminal.
string
The command to execute (e.g., "npm", "git").
string[]
Command arguments (e.g., ["install", "lodash"]).
string
Working directory for the command (absolute path).
Record<string, string>
Additional environment variables.
string
required
Unique identifier for this terminal.

Basic Implementation

terminalOutput()

Gets the current output and exit status of a terminal.

Method Signature

string
The session ID.
string
The terminal ID.
string
required
The terminal output (combined stdout/stderr).
ExitStatus
Exit status if the command has completed:
  • { type: "exit_code", code: number }
  • { type: "signal", signal: string }

Implementation

waitForTerminalExit()

Waits for a terminal command to exit and returns its exit status.

Method Signature

string
The session ID.
string
The terminal ID.
ExitStatus
required
The exit status:
  • { type: "exit_code", code: number }
  • { type: "signal", signal: string }

Implementation

killTerminal()

Kills a terminal command without releasing the terminal.

Method Signature

string
The session ID.
string
The terminal ID.
object
Returns an empty object {} or void on success.

Implementation

The terminal remains valid after killing, allowing you to get final output with terminalOutput(). Call releaseTerminal() when done.

releaseTerminal()

Releases a terminal and frees all associated resources.

Method Signature

string
The session ID.
string
The terminal ID to release.
object
Returns an empty object {} or void on success.

Implementation

Always call releaseTerminal() when done to prevent resource leaks.

Complete Implementation

Here’s a complete, production-ready terminal implementation:

Security Considerations

Command Validation

Argument Sanitization

Resource Limits

See Also