Module 18: Terminal Operations
Execute shell commands on the client through the terminal API.What You’ll Learn
- The four-step terminal lifecycle: create, wait, output, release, plus
killto end a command early - Implementing terminal handlers on the client, all five: advertising
terminalneeds every one - Using the terminal API from the agent side
The Code
Client: Implement terminal handlers
initialize(InitializeRequest) is removed as of 0.80.0: capabilities come from the client builder,
either derived from registered handlers (as here) or set explicitly with clientCapabilities(..).
See the 0.80.0 migration guide.Advertising
terminal needs all five handlers registered; with only some of them, the client logs a
warning naming the missing ones and advertises no terminal at all, rather than a partial one.
createTerminalHandler starts a background thread that drains the process’s output as it arrives, so
terminalOutputHandler can answer with everything printed so far even while the command is still
running; waitForTerminalExitHandler joins that thread before answering, so a terminal/output call
right after exit still sees the command’s last lines.Agent: Use terminal API
Terminal Lifecycle
client().killTerminal() / killTerminalHandler ends a still-running command early, outside this four-step
sequence: the terminal stays valid afterward, so its output can still be read and it’s released the
same as any other terminal. All five handlers are required to advertise terminal at all, not only
the four in the table above.
The agent requests command execution, but the client controls what actually runs. This keeps command execution under the user’s control — the client decides whether to allow, sandbox, or deny terminal requests.
The SDK also provides
context.execute() as a convenience method that combines all four steps.