write_to_terminal
Executes commands or writes text directly to the active iTerm terminal session, enabling automation of tasks through terminal inputs.
Instructions
Writes text to the active iTerm terminal - often used to run a command in the terminal
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| command | Yes | The command to run or text to write to the terminal |
Implementation Reference
- src/index.ts:28-41 (registration)Registration of the 'write_to_terminal' tool in the ListToolsRequestSchema handler, including name, description, and input schema definition.{ name: "write_to_terminal", description: "Writes text to the active iTerm terminal - often used to run a command in the terminal", inputSchema: { type: "object", properties: { command: { type: "string", description: "The command to run or text to write to the terminal" }, }, required: ["command"] } },
- src/index.ts:76-94 (handler)Handler for the 'write_to_terminal' tool within the CallToolRequestSchema switch statement. Instantiates CommandExecutor, executes the command, measures output lines via TtyOutputReader, and returns a response.case "write_to_terminal": { let executor = new CommandExecutor(); const command = String(request.params.arguments?.command); const beforeCommandBuffer = await TtyOutputReader.retrieveBuffer(); const beforeCommandBufferLines = beforeCommandBuffer.split("\n").length; await executor.executeCommand(command); const afterCommandBuffer = await TtyOutputReader.retrieveBuffer(); const afterCommandBufferLines = afterCommandBuffer.split("\n").length; const outputLines = afterCommandBufferLines - beforeCommandBufferLines return { content: [{ type: "text", text: `${outputLines} lines were output after sending the command to the terminal. Read the last ${outputLines} lines of terminal contents to orient yourself. Never assume that the command was executed or that it was successful.` }] }; }
- src/CommandExecutor.ts:12-37 (helper)CommandExecutor.executeCommand method: Escapes the command, uses AppleScript to write to iTerm2 current session, waits for processing completion and user input readiness, retrieves post-command buffer.class CommandExecutor { async executeCommand(command: string): Promise<string> { const escapedCommand = this.escapeForAppleScript(command); try { await execPromise(`/usr/bin/osascript -e 'tell application "iTerm2" to tell current session of current window to write text "${escapedCommand}"'`); // Wait until iterm reports that processing is done while (await this.isProcessing()) { await sleep(100); } const ttyPath = await this.retrieveTtyPath(); while (await this.isWaitingForUserInput(ttyPath) === false) { await sleep(100); } // Give a small delay for output to settle await sleep(200); const afterCommandBuffer = await TtyOutputReader.retrieveBuffer() return afterCommandBuffer } catch (error: unknown) { throw new Error(`Failed to execute command: ${(error as Error).message}`); } }