Skip to main content
Glama
rishabkoul
by rishabkoul

execute-command

Execute commands in iTerm2 terminals to automate terminal operations and manage sessions programmatically.

Instructions

Execute a command in a specific terminal

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
terminalIdYesID of the terminal to execute command in
commandYesCommand to execute

Implementation Reference

  • The handler function that implements the 'execute-command' tool logic: checks for terminal existence, writes the command to the background process stdin, executes an AppleScript to write the command in the iTerm2 GUI terminal, and returns appropriate success or error messages.
    async ({ terminalId, command }) => {
      const terminal = terminals.get(terminalId);
      if (!terminal) {
        return {
          content: [
            {
              type: "text",
              text: `Terminal ${terminalId} not found`,
            },
          ],
        };
      }
    
      // Execute in both GUI and background process
      terminal.process.stdin.write(command + "\n");
    
      const script = `
        tell application "iTerm2"
          tell current session of current window
            write text "${command.replace(/"/g, '\\"')}"
          end tell
        end tell
      `;
    
      try {
        await executeITermScript(script);
        // Give some time for the command to execute and output to be collected
        await new Promise((resolve) => setTimeout(resolve, 100));
    
        return {
          content: [
            {
              type: "text",
              text: `Command executed in ${terminalId}`,
            },
          ],
        };
      } catch (error) {
        return {
          content: [
            {
              type: "text",
              text: `Failed to execute in GUI terminal but command ran in background: ${error.message}`,
            },
          ],
        };
      }
    }
  • Zod schema for the 'execute-command' tool inputs: terminalId (string) and command (string).
      terminalId: z.string().describe("ID of the terminal to execute command in"),
      command: z.string().describe("Command to execute"),
    },
  • index.js:103-158 (registration)
    Registration of the 'execute-command' tool using server.tool(), including name, description, schema, and handler reference.
    server.tool(
      "execute-command",
      "Execute a command in a specific terminal",
      {
        terminalId: z.string().describe("ID of the terminal to execute command in"),
        command: z.string().describe("Command to execute"),
      },
      async ({ terminalId, command }) => {
        const terminal = terminals.get(terminalId);
        if (!terminal) {
          return {
            content: [
              {
                type: "text",
                text: `Terminal ${terminalId} not found`,
              },
            ],
          };
        }
    
        // Execute in both GUI and background process
        terminal.process.stdin.write(command + "\n");
    
        const script = `
          tell application "iTerm2"
            tell current session of current window
              write text "${command.replace(/"/g, '\\"')}"
            end tell
          end tell
        `;
    
        try {
          await executeITermScript(script);
          // Give some time for the command to execute and output to be collected
          await new Promise((resolve) => setTimeout(resolve, 100));
    
          return {
            content: [
              {
                type: "text",
                text: `Command executed in ${terminalId}`,
              },
            ],
          };
        } catch (error) {
          return {
            content: [
              {
                type: "text",
                text: `Failed to execute in GUI terminal but command ran in background: ${error.message}`,
              },
            ],
          };
        }
      }
    );
  • Helper function used by the handler to execute AppleScript commands for interacting with the iTerm terminal GUI.
    async function executeITermScript(script) {
      const execPromise = promisify(exec);
    
      // Simple launch script
      const launchScript = `
        tell application "iTerm"
          activate
        end tell
      `;
    
      try {
        // First try to launch/activate iTerm
        await execPromise(`osascript -e '${launchScript}'`);
    
        // Wait a brief moment
        await new Promise((resolve) => setTimeout(resolve, 1000));
    
        // Now execute the actual script with iTerm instead of iTerm2
        const modifiedScript = script.replace(/iTerm2/g, "iTerm");
        const { stdout } = await execPromise(`osascript -e '${modifiedScript}'`);
        return stdout.trim();
      } catch (error) {
        console.error("iTerm AppleScript error:", error);
        throw error;
      }
    }
Behavior2/5

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

No annotations are provided, so the description carries the full burden of behavioral disclosure. It states the action ('execute a command') but doesn't describe what happens during execution (e.g., synchronous/asynchronous, error handling, side effects, or output retrieval). This is a significant gap for a tool that performs an action without output schema.

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?

The description is a single, efficient sentence with zero waste. It's appropriately sized and front-loaded, directly stating the tool's purpose without unnecessary elaboration.

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

Completeness2/5

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

Given the complexity of executing commands (which can have side effects) and the lack of annotations and output schema, the description is incomplete. It doesn't address behavioral aspects like execution mode, error handling, or how to retrieve results, leaving critical gaps for the agent.

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?

The input schema has 100% description coverage, with clear parameter descriptions. The description adds no additional meaning beyond the schema, such as command format examples or terminalId constraints. Baseline 3 is appropriate when the schema does the heavy lifting.

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

Purpose4/5

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

The description clearly states the action ('execute a command') and the target resource ('in a specific terminal'), which provides a specific verb+resource combination. However, it doesn't differentiate from sibling tools like 'read-output' which might also involve terminal interaction, so it lacks explicit sibling differentiation.

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

Usage Guidelines2/5

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

The description provides no guidance on when to use this tool versus alternatives. It doesn't mention prerequisites (e.g., needing an open terminal), exclusions, or comparisons to siblings like 'read-output' for output retrieval, leaving the agent without usage context.

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

Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/rishabkoul/iTerm-MCP-Server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server