Skip to main content
Glama

send_escape_sequence

Send raw escape sequences to iTerm2 sessions. Supports Python string escapes for control codes like clear screen or cursor home.

Instructions

Send a raw escape sequence to an iTerm2 session.

Python string escapes in sequence are interpreted before sending, so values like \x1b[2J (clear screen) or \x1b[H (cursor home) work as expected.

:param sequence: The escape sequence string. :param session_id: Target session UUID. Defaults to the active session.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
sequenceYes
session_idNo

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault
resultYes

Implementation Reference

  • The async function `send_escape_sequence` implementing the tool logic. It takes a `sequence` string and optional `session_id`, interprets Python escape sequences via latin-1 round-trip, and sends the result via iTerm2's async_send_text.
    @mcp.tool()
    async def send_escape_sequence(
        sequence: str,
        session_id: str | None = None,
    ) -> str:
        """Send a raw escape sequence to an iTerm2 session.
    
        Python string escapes in ``sequence`` are interpreted before sending, so
        values like ``\\x1b[2J`` (clear screen) or ``\\x1b[H`` (cursor home) work
        as expected.
    
        :param sequence: The escape sequence string.
        :param session_id: Target session UUID. Defaults to the active session.
        """
        # Round-trip through latin-1 so Python interprets escape sequences like \x1b.
        # This only works for characters in the latin-1 range (0x00-0xFF).
        cooked = sequence.encode("latin-1").decode("unicode_escape")
        sess = await _session(session_id)
        await sess.async_send_text(cooked, suppress_broadcast=True)
        return f"Sent {len(cooked)} bytes to session {sess.session_id}"
  • The `@mcp.tool()` decorator registers `send_escape_sequence` as an MCP tool on the FastMCP instance named 'iterm2-mcp'.
    @mcp.tool()
  • The function signature defines the schema: `sequence: str` (required) and `session_id: str | None` (optional, defaults to active session).
    async def send_escape_sequence(
        sequence: str,
        session_id: str | None = None,
    ) -> str:
  • The `_session()` helper resolves the session_id or falls back to the currently active session, used by send_escape_sequence and other tools.
    async def _session(session_id: str | None) -> iterm2.Session:
        """Resolve a session by ID, falling back to the currently active session.
    
        :param session_id: A specific session UUID to target, or ``None`` to use the
            currently focused window/tab/pane.
        """
        app = await _app()
        if session_id:
            sess = app.get_session_by_id(session_id)
            if sess is None:
                raise ValueError(f"No session found with ID {session_id!r}")
            return sess
        win = app.current_terminal_window
        if win is None:
            raise RuntimeError("No active iTerm2 window.")
        tab = win.current_tab
        if tab is None:
            raise RuntimeError("No active tab in the current window.")
        sess = tab.current_session
        if sess is None:
            raise RuntimeError("No active session in the current tab.")
        return sess
Behavior4/5

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

No annotations are provided, so the description carries full burden. It discloses that Python string escapes are interpreted before sending, a key behavioral trait. It also notes the default for session_id. It does not mention potential error handling or side effects, but overall provides good transparency.

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 concise, consisting of three sentences that front-load the purpose and then detail parameters. Every sentence adds value without redundancy.

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

Completeness4/5

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

Given the tool has an output schema (not shown), the description covers the essential context: purpose, parameter semantics, and escape interpretation behavior. It misses any mention of prerequisites or error conditions, but for a simple tool with two parameters, it is fairly complete.

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

Parameters5/5

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

Schema description coverage is 0%, yet the description includes parameter details: 'sequence' is the escape sequence string with Python escape interpretation, and 'session_id' is target session UUID with default. It adds significant meaning beyond the schema's bare type/name.

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?

The description clearly states it sends a raw escape sequence to an iTerm2 session, with examples like clear screen and cursor home. It distinguishes from siblings such as send_control_character and write_to_terminal by specifying 'raw escape sequence' and explaining Python escape interpretation.

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?

The description explains that Python string escapes are interpreted, which guides usage. It also indicates session_id defaults to active session. However, it does not explicitly state when to use this over alternatives like send_control_character or write_to_terminal.

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/lorencarvalho/iterm2-mcp'

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