Skip to main content
Glama

write_to_terminal

Execute commands or write text directly to your active iTerm terminal session through the iTerm MCP server.

Instructions

Writes text to the active iTerm terminal - often used to run a command in the terminal

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
commandYesThe command to run or text to write to the terminal

Implementation Reference

  • Handler for the 'write_to_terminal' tool: executes the command via CommandExecutor, measures output lines using TtyOutputReader, and returns a descriptive message.
    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.`
        }]
      };
    }
  • Tool specification including name, description, and input schema for 'write_to_terminal'.
    {
      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:25-72 (registration)
    Registration of all tools including 'write_to_terminal' via the ListToolsRequestHandler.
    server.setRequestHandler(ListToolsRequestSchema, async () => {
      return {
        tools: [
          {
            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"]
            }
          },
          {
            name: "read_terminal_output",
            description: "Reads the output from the active iTerm terminal",
            inputSchema: {
              type: "object",
              properties: {
                linesOfOutput: {
                  type: "number",
                  description: "The number of lines of output to read."
                },
              },
              required: ["linesOfOutput"]
            }
          },
          {
            name: "send_control_character",
            description: "Sends a control character to the active iTerm terminal (e.g., Control-C)",
            inputSchema: {
              type: "object",
              properties: {
                letter: {
                  type: "string",
                  description: "The letter corresponding to the control character (e.g., 'C' for Control-C)"
                },
              },
              required: ["letter"]
            }
          }
        ]
      };
    });
  • Core implementation in CommandExecutor.executeCommand: escapes command and uses osascript to write to iTerm2 current session, waits for completion.
    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}`);
      }
    }

Schema Changelog

Changes observed during successful MCP inspections.

  1. First observed

TDQS

A3.7/5.0
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

No annotations, and description lacks behavioral details such as whether the command waits for completion, effect on terminal state, or authentication needs. Critical for a command execution tool.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

One succinct sentence with no unnecessary information. Front-loaded with core action.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness3/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Adequate for a simple tool, but lacks deeper context about execution behavior (synchronous? interactive?). Without annotations, description could do more to clarify usage.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Single parameter 'command' with schema description 'The command to run or text to write to the terminal'. Description adds marginal value beyond schema, but schema coverage is 100%.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

Clearly states it writes text to the active iTerm terminal, often to run a command. Distinguishes from siblings (read_terminal_output and send_control_character) by its write action.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines4/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

Implies use for running commands and writing text. Context with siblings suggests when not to use (reading output or sending control characters), but no explicit exclusions.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.