terminal-driver-mcp
Enables AI agents to spawn persistent terminal sessions, send keystrokes, read screen state, and assert output, allowing them to interact with stateful terminal applications like vim, htop, and gdb.
README
terminal-driver-mcp
A PTY-backed terminal MCP server that gives AI agents a real interactive terminal: spawn persistent sessions, read a clean text snapshot of the rendered screen, send keystrokes, wait for patterns or idle, and assert screen state — so agents can drive and test stateful TUI applications (vim, htop, gdb, interactive git) the way a human does.
Architecture
- node-pty allocates a real pseudo-terminal, so applications detect a TTY and behave fully interactively (colors, redraws, SIGWINCH).
- @xterm/headless ingests the raw ANSI byte stream and maintains a stateful in-memory 2D screen grid.
- @xterm/addon-serialize + buffer iteration extract the grid as clean plain text (or raw VT sequences), eliminating "ANSI garbage".
- Sessions live in an in-memory registry and persist across tool calls; PTY children are hard-killed on server exit — no zombies.
- Local-first: stdio transport only, zero network access.
Tools
| Tool | Purpose |
|---|---|
execute_command(command, cwd?, timeout_ms=30000) |
One-shot: run a command to completion in a fresh PTY, return full output + exit code, auto-cleanup. Kills the process and returns partial output on timeout |
session_create(session_id, command?, cwd?, cols=120, rows=30) |
Spawn a persistent PTY session (command via your shell, or an interactive shell) |
session_read(session_id, format=text|raw, scrollback_lines=0) |
Snapshot the rendered screen; scrollback_lines also returns output that scrolled off the top (long logs) |
session_write(session_id, input?, special_keys[]?, raw_hex?) |
Type text, special keys (enter, escape, arrows, ctrl+c, f-keys, chords, …), and/or arbitrary raw bytes; returns the resulting screen |
session_wait(session_id, pattern, timeout_ms) |
Poll until a regex matches the screen |
session_wait_idle(session_id, idle_ms=80, timeout_ms, mode=silence|stable_screen) |
Wait until output quiesces (byte silence) or the rendered screen stops changing |
session_assert(session_id, expected_text, exact_row?, exact_col?) |
Pass/fail screen assertion with contextual diff; exact_col pins the text to a starting column |
session_region(session_id, row, col, width, height) |
Extract a rectangle of the screen (a pane, status bar, or widget) |
session_resize(session_id, cols, rows) |
Resize PTY + emulator (SIGWINCH reflow) |
session_list() |
List sessions with pid/status/age |
session_kill(session_id) |
Terminate and clean up a session |
run_test(file | test_json) |
Replay a JSON test script deterministically (see below) |
Every screen header includes the cursor position (cursor row:col, 0-based, matching screen row numbering).
Arrow keys are DECCKM-aware: when a full-screen app (vim, less) enables application cursor mode, arrows are sent as SS3 sequences automatically. Control chords use the byte the target understands: ctrl+<letter> and symbol chords (ctrl+], ctrl+\) send their legacy C0 code (works everywhere), while chords with no legacy encoding (shift+escape, ctrl+enter, ...) fall back to CSI-u (fixterms/kitty). For anything no key name covers, raw_hex sends arbitrary bytes (e.g. raw_hex: "1b5b41" for ESC[A).
Typing a key name as literal text is a common mistake, so input values containing {enter}-style names or backslash escapes like \r are rejected with a hint pointing at special_keys.
The emulator also answers terminal queries (DA1, DSR cursor reports, ...) on the application's behalf, so query-happy TUIs (neovim and friends) behave as they would in a real terminal instead of hanging on a probe.
Synchronization caveats
Both session_wait_idle modes are best-effort: continuously-animating UIs (spinners, progress bars, htop's periodic redraws) never go quiet, so those calls run to timeout — which still returns the current screen. When you know what you're waiting for, session_wait with a pattern is the reliable primitive. stable_screen mode helps with apps that emit bytes without visual change (cursor pings, identical redraws).
When a session's screen header says lines have scrolled off (e.g. after a long build), read them back with session_read(scrollback_lines: N) — up to 1000 lines are retained.
Session recordings
Every persistent session is recorded to an asciicast v2 file in ~/.terminal-driver-mcp/recordings/ (override with TERMINAL_DRIVER_MCP_RECORDING_DIR). Replay any session after the fact with asciinema play <file>.cast — full fidelity, including agent keystrokes (as input events), resizes, and emulator query responses (non-standard "q" events, ignored by players). The recording path is reported by session_create and session_kill. Recording is best-effort and never fails a session; execute_command runs are not recorded (their full output is already returned).
Deterministic test replay
The agent drives your TUI interactively once, then writes a JSON test script that replays forever with no LLM in the loop — in CI via:
node dist/index.js run tests/*.json # exit 0 = all pass, 1 = failures
or ad-hoc via the run_test tool. Example script:
{
"name": "editor smoke test",
"command": "vim -u NONE /tmp/t.txt",
"cols": 100,
"rows": 24,
"steps": [
{ "wait": "~", "timeout_ms": 8000 },
{ "write": "ihello", "keys": ["escape"] },
{ "assert": "hello", "row": 0, "col": 0 },
{ "resize": [60, 15] },
{ "write": ":q!", "keys": ["enter"] },
{ "expect_exit": 0 }
]
}
Step types: {"wait": "<regex>"}, {"idle_ms": N, "mode"?: "silence"|"stable_screen"}, {"write": "text", "keys": [...]}, {"assert": "text", "row"?, "col"?}, {"resize": [cols, rows]}, {"sleep_ms": N}, {"expect_exit": code}. Execution stops at the first failing step and the report includes the final screen.
Setup
git clone https://github.com/funkyfunc/terminal-driver-mcp.git
cd terminal-driver-mcp
npm install
npm run build
Register with Claude Code (from the repo directory):
claude mcp add terminal --scope user -- node "$(pwd)/dist/index.js"
Or, once published to npm:
claude mcp add terminal --scope user -- npx -y terminal-driver-mcp
Then /mcp inside Claude Code to confirm the connection.
Recommended agent workflow (Assert–Act–Assert)
- Observe —
session_readfor a fresh snapshot. - Verify —
session_wait/session_wait_idleuntil the expected state is visible and stable. - Act —
session_writewith precise keystrokes. - Re-verify —
session_assertthat the action produced the expected change before proceeding.
Development
npm run dev # tsc --watch
npm test # build + unit + e2e + gauntlet suites
npm run lint # biome check
npm run format # biome format --write
npm run inspector # hand-drive tools in the MCP Inspector UI
Three test layers: test/unit-screen.mjs pins screen-reading invariants (flush-before-read, wide-char columns) fast and deterministically; test/e2e.mjs drives every tool over a real stdio MCP connection (including real vim); and test/gauntlet.mjs runs an adversarial torture-tui.mjs through the server in lockstep — capability probes, byte-split escape sequences, wide characters, an output firehose, a live-redrawing alternate screen, exact keystroke-byte verification (arrows in both cursor modes, CSI-u chords), SIGWINCH, and a slow dialog. The gauntlet is where the hard terminal-compatibility bugs surface.
Git hooks (activated automatically by npm install via core.hooksPath): pre-commit runs lint + typecheck, pre-push runs the full test suite. CI runs the same on Ubuntu and macOS.
Note: postinstall restores the execute bit on node-pty's prebuilt spawn-helper (npm strips it on macOS, which otherwise causes posix_spawnp failed).
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。