Skip to main content

iterm2-mcp

PyPI version Python 3.12+ License: MIT

English | 简体中文

An iTerm2 MCP server built around visible execution: every command is injected into a terminal session as typed text (send text) — exactly as if the user typed it — so everything stays on screen and under your supervision. There is no exec / silent-execution tool of any kind.

Inspired by electerm's MCP integration and ferrislucas/iterm-mcp, customized for multi-server profile management.

Features

  • Connect via saved iTerm2 profiles: open a new tab by profile name (or GUID); duplicate profile names raise an error listing all GUIDs — it never silently connects to the wrong machine
  • Visible execution: send_command is equivalent to typing a command and pressing Enter; send_text feeds input step by step for REPLs / interactive programs. There is no execution channel that bypasses the terminal screen
  • Address sessions by name: after a tab is created, its session is automatically registered under the profile name; the agent can rename it at any time, and all other tools address sessions by name
  • Auto-login: when a profile auto-types its password, the server detects the password prompt and sends the confirming Enter for you
  • File transfer: upload_file / download_file drive scp from a local shell session (Mac-initiated outbound connection — works with public-internet servers); distribute_key sends your Mac's public key into an already-logged-in server session (no password needed), after which transfers run fully automatically; check_transfer verifies md5 on both ends
  • Multi-session management: list / focus (switch tab) / close

Tools

Tool Description
list_profiles List all iTerm2 profile names (duplicates flagged)
list_sessions List all window/tab/session entries (id, name, MCP name)
create_session Open a new tab by profile name; session auto-named after the profile
create_session_by_guid Open a tab by profile GUID (for disambiguating duplicate names)
focus_session Focus the iTerm2 window on a session (select its tab and bring to front)
close_session Close a session (by name or id)
rename_session Rename a session
send_command Send one line of command to a session (auto \n, visible as typed)
send_text Send raw text/keystrokes to a session (for REPLs; no trailing newline)
read_screen Read a session's current screen contents (line by line)
interrupt Send Ctrl+C to a session
create_local_session Open a local shell tab registered as local-shell (used by transfer tools)
upload_file Upload a local file to a server via scp (Mac-initiated)
download_file Download a server file to local via scp (Mac-initiated)
distribute_key Send Mac's public key into a logged-in server session + pre-scan host key (one-time, passwordless afterwards)
check_transfer Verify transfer integrity by comparing remote/local md5

Every tool that accepts a name also accepts a session id — the name_or_id parameter auto-detects.

Requirements

  • macOS + iTerm2 ≥ 3.5, with Settings → General → Magic → Enable Python API turned on
  • Python 3.12+

Install & Register

Prerequisite: pipx or uv (pick one, if not installed yet)

Method 1 / Method 2 depend on the pipx / uvx commands. Install one first (via Homebrew on macOS):

brew install pipx          # https://pipx.pypa.io
# or
brew install uv            # https://docs.astral.sh/uv/ (provides uvx)

On machines without Homebrew, use the official installers at the links above — or skip ahead to Method 3.

pipx install iterm2-mcp-zanlan

Then register (Claude Desktop / .mcp.json JSON config):

{
  "mcpServers": {
    "iterm2": {
      "command": "iterm2-mcp"
    }
  }
}

One-liner for Claude Code:

claude mcp add iterm2 -- iterm2-mcp

Method 2: uvx, zero install

{
  "mcpServers": {
    "iterm2": {
      "command": "uvx",
      "args": ["iterm2-mcp-zanlan"]
    }
  }
}

Method 3: from source

git clone https://github.com/Zane-lan/iterm2-mcp.git
cd iterm2-mcp
pip install iterm2 "mcp[cli]>=1,<2"   # homebrew Python: add --user --break-system-packages
{
  "mcpServers": {
    "iterm2": {
      "command": "/path/to/python3.12",
      "args": ["/path/to/iterm2-mcp/src/iterm2_mcp/server.py"]
    }
  }
}

Design decisions

  1. Duplicate-profile protection: opening a tab by name that matches multiple profiles raises an error listing every GUID — the user picks one (or deletes the duplicates in iTerm2); a GUID-based tool is provided as the escape hatch
  2. Auto-naming on creation: a tab opened via a profile is registered under the profile name, so later tools can address it by name
  3. Name addressing uses an MCP-maintained registry (~/.config/iterm2-mcp/registry.json) — in practice, shell integration resets the session title back to an automatic name (e.g. ~ (-zsh)) after every command via OSC escape sequences, so session.name is unreliable; set_name is still attempted for a readable tab title, but addressing never depends on it; a live session.name prefix match serves as fallback when the registry misses
  4. Send-only, no exec: no tool in the set bypasses the terminal screen

Verification

The end-to-end integration test runs against a real iTerm2 (opens tabs, sends commands, Ctrl+C, closes tabs):

python3.12 test_e2e.py

Implementation notes (pitfalls found the hard way)

  • iTerm2 3.6.x no longer listens on TCP 1912; the Python API moved to a Unix Domain Socket (~/Library/Application Support/iTerm2/private/socket), which the official iterm2 package auto-detects
  • When driving the iterm2 package from a custom event loop, you must also start the Connection._async_dispatch_forever dispatch loop — otherwise every RPC hangs forever
  • iterm2 2.30: entry point is iterm2.run_until_complete(coro); ScreenContents uses the line(i) method; App.current_session is gone
  • FastMCP's lifespan must be passed to the FastMCP(...) constructor; setting settings.lifespan afterwards has no effect

License

MIT

Metadata

Release files for iterm2-mcp-zanlan 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for iterm2-mcp-zanlan 0.2.0
File Size Uploaded Provenance Actions
iterm2_mcp_zanlan-0.2.0.tar.gz 16.7 kB Without provenance Details for iterm2_mcp_zanlan-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for iterm2-mcp-zanlan 0.2.0
File Interpreter ABI Platform Actions
iterm2_mcp_zanlan-0.2.0-py3-none-any.whl Python 3 none any Details for iterm2_mcp_zanlan-0.2.0-py3-none-any.whl

Total release size: 29.3 kB

Release files / iterm2_mcp_zanlan-0.2.0.tar.gz

Download URL iterm2_mcp_zanlan-0.2.0.tar.gz
Size 16.7 kB
Tags Source
SHA-256 checksum
How to use checksums
76f05971e77516d84effe519e922d90c3fe521a34d5374d28349279122a9be2f
BLAKE2b-256 checksum
How to use checksums
93ce9f4659f503d21b1e2ea6c25472d772760e7a2c122cc96fc6b112e10fa8f4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.13

Release files / iterm2_mcp_zanlan-0.2.0-py3-none-any.whl

Download URL iterm2_mcp_zanlan-0.2.0-py3-none-any.whl
Size 12.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
db188d293b90d7376f34ccddfedf08a9d2e8c483bd0e64de47ccbaa4f1564c41
BLAKE2b-256 checksum
How to use checksums
505df5c1477e5cff1c9246fe9fe738c810ffe8570d9ff84cfc2e2d140d0e7bf3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.13

Security for iterm2-mcp-zanlan 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page