|
| 1 | +--- |
| 2 | +title: "Terminals" |
| 3 | +description: "Executing and managing terminal commands" |
| 4 | +--- |
| 5 | + |
| 6 | +The terminal methods allow Agents to execute shell commands within the Client's environment. These methods enable Agents to run build processes, execute scripts, and interact with command-line tools while providing real-time output streaming and process control. |
| 7 | + |
| 8 | +## Checking Support |
| 9 | + |
| 10 | +Before attempting to use terminal methods, Agents **MUST** verify that the Client supports this capability by checking the [Client Capabilities](./initialization#client-capabilities) field in the `initialize` response: |
| 11 | + |
| 12 | +```json highlight={7} |
| 13 | +{ |
| 14 | + "jsonrpc": "2.0", |
| 15 | + "id": 0, |
| 16 | + "result": { |
| 17 | + "protocolVersion": 1, |
| 18 | + "clientCapabilities": { |
| 19 | + "terminal": true |
| 20 | + } |
| 21 | + } |
| 22 | +} |
| 23 | +``` |
| 24 | + |
| 25 | +If `terminal` is `false` or not present, the Agent **MUST NOT** attempt to call any terminal methods. |
| 26 | + |
| 27 | +## Executing Commands |
| 28 | + |
| 29 | +The `terminal/create` method starts a command in a new terminal: |
| 30 | + |
| 31 | +```json |
| 32 | +{ |
| 33 | + "jsonrpc": "2.0", |
| 34 | + "id": 5, |
| 35 | + "method": "terminal/create", |
| 36 | + "params": { |
| 37 | + "sessionId": "sess_abc123def456", |
| 38 | + "command": "npm", |
| 39 | + "args": ["test", "--coverage"], |
| 40 | + "env": [ |
| 41 | + { |
| 42 | + "name": "NODE_ENV", |
| 43 | + "value": "test" |
| 44 | + } |
| 45 | + ], |
| 46 | + "cwd": "/home/user/project", |
| 47 | + "outputByteLimit": 1048576 |
| 48 | + } |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +<ParamField path="sessionId" type="SessionId" required> |
| 53 | + The [Session ID](./session-setup#session-id) for this request |
| 54 | +</ParamField> |
| 55 | + |
| 56 | +<ParamField path="command" type="string" required> |
| 57 | + The command to execute |
| 58 | +</ParamField> |
| 59 | + |
| 60 | +<ParamField path="args" type="string[]"> |
| 61 | + Optional array of command arguments |
| 62 | +</ParamField> |
| 63 | + |
| 64 | +<ParamField path="env" type="EnvVariable[]"> |
| 65 | + Optional environment variables for the command. |
| 66 | + |
| 67 | + Each variable has: |
| 68 | + - `name`: The environment variable name |
| 69 | + - `value`: The environment variable value |
| 70 | +</ParamField> |
| 71 | + |
| 72 | +<ParamField path="cwd" type="string"> |
| 73 | + Optional working directory for the command (absolute path) |
| 74 | +</ParamField> |
| 75 | + |
| 76 | +<ParamField path="outputByteLimit" type="number"> |
| 77 | + Optional maximum number of output bytes to retain. Once exceeded, earlier output is truncated to stay within this limit. |
| 78 | +</ParamField> |
| 79 | + |
| 80 | + The Client returns a Terminal ID immediately without waiting for completion: |
| 81 | + |
| 82 | +```json |
| 83 | +{ |
| 84 | + "jsonrpc": "2.0", |
| 85 | + "id": 5, |
| 86 | + "result": { |
| 87 | + "terminalId": "term_xyz789" |
| 88 | + } |
| 89 | +} |
| 90 | +``` |
| 91 | + |
| 92 | +This allows the command to run in the background while the Agent performs other operations. |
| 93 | + |
| 94 | +After creating the terminal, the Agent can use the `terminal/wait_for_exit` method to wait for the command to complete. |
| 95 | + |
| 96 | +<Note> |
| 97 | +The Agent **MUST** release the terminal using `terminal/release` when it's no longer needed. |
| 98 | +</Note> |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | +## Embedding in Tool Calls |
| 103 | + |
| 104 | +Terminals can be embedded directly in [tool calls](./tool-calls) to provide real-time output to users: |
| 105 | + |
| 106 | +```json |
| 107 | +{ |
| 108 | + "jsonrpc": "2.0", |
| 109 | + "method": "session/update", |
| 110 | + "params": { |
| 111 | + "sessionId": "sess_abc123def456", |
| 112 | + "update": { |
| 113 | + "sessionUpdate": "tool_call", |
| 114 | + "toolCallId": "call_002", |
| 115 | + "title": "Running tests", |
| 116 | + "kind": "execute", |
| 117 | + "status": "in_progress", |
| 118 | + "content": [ |
| 119 | + { |
| 120 | + "type": "terminal", |
| 121 | + "terminalId": "term_xyz789" |
| 122 | + } |
| 123 | + ] |
| 124 | + } |
| 125 | + } |
| 126 | +} |
| 127 | +``` |
| 128 | + |
| 129 | +When a terminal is embedded in a tool call, the Client displays live output as it's generated and continues to display it even after the terminal is released. |
| 130 | + |
| 131 | +## Getting Output |
| 132 | + |
| 133 | +The `terminal/output` method retrieves the current terminal output without waiting for the command to complete: |
| 134 | + |
| 135 | +```json |
| 136 | +{ |
| 137 | + "jsonrpc": "2.0", |
| 138 | + "id": 6, |
| 139 | + "method": "terminal/output", |
| 140 | + "params": { |
| 141 | + "sessionId": "sess_abc123def456", |
| 142 | + "terminalId": "term_xyz789" |
| 143 | + } |
| 144 | +} |
| 145 | +``` |
| 146 | + |
| 147 | +The Client responds with the current output and exit status (if the command has finished): |
| 148 | + |
| 149 | +```json |
| 150 | +{ |
| 151 | + "jsonrpc": "2.0", |
| 152 | + "id": 6, |
| 153 | + "result": { |
| 154 | + "output": "Running tests...\n✓ All tests passed (42 total)\n", |
| 155 | + "truncated": false, |
| 156 | + "exitStatus": { |
| 157 | + "exitCode": 0, |
| 158 | + "signal": null |
| 159 | + } |
| 160 | + } |
| 161 | +} |
| 162 | +``` |
| 163 | + |
| 164 | +<ResponseField name="output" type="string" required> |
| 165 | + The terminal output captured so far |
| 166 | +</ResponseField> |
| 167 | + |
| 168 | +<ResponseField name="truncated" type="boolean" required> |
| 169 | + Whether the output was truncated due to byte limits |
| 170 | +</ResponseField> |
| 171 | + |
| 172 | +<ResponseField name="exitStatus" type="TerminalExitStatus"> |
| 173 | + Present only if the command has exited. Contains: |
| 174 | + - `exitCode`: The process exit code (may be null) |
| 175 | + - `signal`: The signal that terminated the process (may be null) |
| 176 | +</ResponseField> |
| 177 | + |
| 178 | +## Waiting for Exit |
| 179 | + |
| 180 | +The `terminal/wait_for_exit` method returns once the command completes: |
| 181 | + |
| 182 | +```json |
| 183 | +{ |
| 184 | + "jsonrpc": "2.0", |
| 185 | + "id": 7, |
| 186 | + "method": "terminal/wait_for_exit", |
| 187 | + "params": { |
| 188 | + "sessionId": "sess_abc123def456", |
| 189 | + "terminalId": "term_xyz789" |
| 190 | + } |
| 191 | +} |
| 192 | +``` |
| 193 | + |
| 194 | +The Client responds once the command exits: |
| 195 | + |
| 196 | +```json |
| 197 | +{ |
| 198 | + "jsonrpc": "2.0", |
| 199 | + "id": 7, |
| 200 | + "result": { |
| 201 | + "exitCode": 0, |
| 202 | + "signal": null |
| 203 | + } |
| 204 | +} |
| 205 | +``` |
| 206 | + |
| 207 | +<ResponseField name="exitCode" type="number"> |
| 208 | + The process exit code (may be null if terminated by signal) |
| 209 | +</ResponseField> |
| 210 | + |
| 211 | +<ResponseField name="signal" type="string"> |
| 212 | + The signal that terminated the process (may be null if exited normally) |
| 213 | +</ResponseField> |
| 214 | + |
| 215 | +## Killing Commands |
| 216 | + |
| 217 | +The `terminal/kill` method terminates a command without releasing the terminal: |
| 218 | + |
| 219 | +```json |
| 220 | +{ |
| 221 | + "jsonrpc": "2.0", |
| 222 | + "id": 8, |
| 223 | + "method": "terminal/kill", |
| 224 | + "params": { |
| 225 | + "sessionId": "sess_abc123def456", |
| 226 | + "terminalId": "term_xyz789" |
| 227 | + } |
| 228 | +} |
| 229 | +``` |
| 230 | + |
| 231 | +After killing a command, the terminal remains valid and can be used with: |
| 232 | +- `terminal/output` to get the final output |
| 233 | +- `terminal/wait_for_exit` to get the exit status |
| 234 | + |
| 235 | +The Agent **MUST** still call `terminal/release` when it's done using it. |
| 236 | + |
| 237 | +### Building a Timeout |
| 238 | + |
| 239 | +Agents can implement command timeouts by combining terminal methods: |
| 240 | + |
| 241 | +1. Create a terminal with `terminal/create` |
| 242 | +2. Start a timer for the desired timeout duration |
| 243 | +3. Concurrently wait for either the timer to expire or `terminal/wait_for_exit` to return |
| 244 | +4. If the timer expires first: |
| 245 | + - Call `terminal/kill` to terminate the command |
| 246 | + - Call `terminal/output` to retrieve any final output |
| 247 | + - Include the output in the response to the model |
| 248 | +5. Call `terminal/release` when done |
| 249 | + |
| 250 | +## Releasing Terminals |
| 251 | + |
| 252 | +The `terminal/release` kills the command if still running and releases all resources: |
| 253 | + |
| 254 | +```json |
| 255 | +{ |
| 256 | + "jsonrpc": "2.0", |
| 257 | + "id": 9, |
| 258 | + "method": "terminal/release", |
| 259 | + "params": { |
| 260 | + "sessionId": "sess_abc123def456", |
| 261 | + "terminalId": "term_xyz789" |
| 262 | + } |
| 263 | +} |
| 264 | +``` |
| 265 | + |
| 266 | +After release the terminal ID becomes invalid for all other `terminal/*` methods. |
| 267 | + |
| 268 | +If the terminal was added to a tool call, the client **SHOULD** continue to display its output after release. |
0 commit comments