terminal-mcp
问题所在
每个 AI 编码工具都会遇到同一个瓶颈:没有真正的终端访问权限。
Claude Code 的 Bash 工具、GitHub Copilot 和 Codex 都在隔离的子进程中运行命令。每个命令都是全新启动。没有状态可以延续。这意味着:
无法使用 SSH 会话 - 无法连接到远程服务器并运行多个命令
无法使用 REPL - 无法交互式地使用 Python、Node 或 Ruby 解释器
无法使用数据库 CLI - 无法维持 psql、mysql 或 redis-cli 连接
无法使用 TUI 应用 - 无法使用方向键导航 htop、vim 或 fzf
无法运行长时间进程 - 无法监控构建、查看日志或运行开发服务器
Related MCP server: Interactive Terminal MCP Server
解决方案
terminal-mcp 为 AI 代理提供了一个真正的终端。持久化的 PTY 会话可以在工具调用之间保持存活。发送命令、读取输出、按下按键、导航 TUI——就像人类在终端前操作一样。
uvx terminal-mcp一条命令。适用于 Claude Code、Claude Desktop、VS Code、Cursor 和 Windsurf。
快速开始
1. 安装(30 秒)
# No install needed - run directly
uvx terminal-mcp
# Or install globally
pip install terminal-mcp2. 连接到你的 AI 客户端
添加到 ~/.claude.json 或项目 .mcp.json:
{
"mcpServers": {
"terminal": {
"command": "uvx",
"args": ["terminal-mcp"]
}
}
}添加到 claude_desktop_config.json:
{
"mcpServers": {
"terminal": {
"command": "uvx",
"args": ["terminal-mcp"]
}
}
}点击上方的一键安装徽章,或添加到 .vscode/mcp.json:
{
"servers": {
"terminal-mcp": {
"command": "uvx",
"args": ["terminal-mcp"]
}
}
}添加到 ~/.codeium/windsurf/mcp_config.json:
{
"mcpServers": {
"terminal": {
"command": "uvx",
"args": ["terminal-mcp"]
}
}
}3. 验证
session_exec exec="echo hello from terminal-mcp"你能用它做什么?
SSH 连接到远程服务器
session_create command="ssh user@prod-server.com" label="prod"
session_interact session_id="a1b2c3d4" input="df -h" wait_for="\$"
session_interact session_id="a1b2c3d4" input="docker ps" wait_for="\$"
session_close session_id="a1b2c3d4"运行交互式 REPL
session_create command="python3" label="python"
session_interact session_id="e5f6g7h8" input="import pandas as pd" wait_for=">>>"
session_interact session_id="e5f6g7h8" input="df = pd.read_csv('data.csv')" wait_for=">>>"
session_interact session_id="e5f6g7h8" input="df.describe()" wait_for=">>>"
session_close session_id="e5f6g7h8"查询数据库
session_create command="psql -U admin mydb" label="db"
session_interact session_id="x1y2z3w4" input="SELECT count(*) FROM users;" wait_for="row"
session_interact session_id="x1y2z3w4" input="\dt" wait_for="#"
session_close session_id="x1y2z3w4"导航 TUI 应用
session_create command="htop" label="monitor"
session_read session_id="a1b2c3d4"
# Auto-detects TUI, returns screen snapshot
session_send session_id="a1b2c3d4" key="F6"
session_read session_id="a1b2c3d4" mode="diff"
# Returns only changed lines - saves tokens
session_send session_id="a1b2c3d4" key="F10"
session_close session_id="a1b2c3d4"监控长时间运行的构建
session_create command="bash" label="build"
session_send session_id="a1b2c3d4" input="npm run build"
session_wait_for session_id="a1b2c3d4" pattern="Build complete|ERROR" timeout=120运行一次性命令
session_exec exec="git log --oneline -10"
session_exec exec="docker compose ps" timeout=10功能一览
功能 | 作用 |
持久化会话 | 真正的 PTY 会话,可在工具调用之间保持存活 |
一次调用完成发送 + 读取 |
|
基于模式的读取 |
|
自动 TUI 检测 | 检测 htop、vim 等,并自动切换到屏幕快照模式 |
输出差异模式 | 仅返回变化的屏幕行 - 最小化 token 消耗 |
特殊按键 | 方向键、Tab、F1-F12、Home/End、Page Up/Down |
控制字符 | Ctrl-C、Ctrl-D、Ctrl-Z、Ctrl-L、telnet 转义 |
危险命令防护 | 阻止 |
OSC 133 Shell 集成 | 自动检测命令边界和退出码 |
智能截断 | 四种策略防止上下文溢出 |
秘密输入 | 发送密码而不记录日志 |
动态调整大小 | 通过 SIGWINCH 动态调整终端大小 |
空闲清理 | 自动关闭空闲会话 |
跨平台 | 支持 Linux、macOS 和 Windows |
工具参考
terminal-mcp 暴露了 9 个 MCP 工具。完整详情见 docs/tools.md。
工具 | 用途 |
生成一个持久化终端会话 | |
发送文本、按键或控制字符 | |
读取输出(流、快照、自动、差异模式) | |
一次调用完成发送 + 读取 | |
等待输出中出现正则模式 | |
一次性命令执行 | |
优雅关闭会话 | |
调整终端尺寸 | |
列出活动会话 |
架构
flowchart LR
Client[AI Client] -->|MCP JSON-RPC| Server[terminal-mcp]
Server --> SM[Session Manager]
SM --> S1[PTY 1: bash]
SM --> S2[PTY 2: python3]
SM --> S3[PTY 3: ssh user@host]
S1 & S2 & S3 -.->|PTY output| Reader[Reader Thread]
Reader -.->|buffer| Server每个会话都由一个通过 pexpect.spawn(Windows 上为 PopenSpawn)实现的真实 PTY 支持。有关完整架构详情,请参阅 docs/architecture.md。
配置
所有设置均可通过 TERMINAL_MCP_* 环境变量配置。完整参考见 docs/configuration.md。
设置 | 环境变量 | 默认值 |
最大会话数 |
|
|
空闲超时 |
|
|
安全防护 |
|
|
缓冲区上限 |
|
|
截断模式 |
|
|
自定义设置示例:
{
"mcpServers": {
"terminal": {
"command": "uvx",
"args": ["terminal-mcp"],
"env": {
"TERMINAL_MCP_MAX_SESSIONS": "20",
"TERMINAL_MCP_IDLE_TIMEOUT": "3600",
"TERMINAL_MCP_TRUNCATION_MODE": "head_tail"
}
}
}
}文档
文档 | 描述 |
所有 9 个 MCP 工具的完整 API | |
terminal-mcp 的工作原理 | |
所有设置和环境变量 | |
危险命令检测和安全防护 | |
实际应用场景和模式 | |
版本历史和发布说明 | |
如何贡献 |
支持的客户端
客户端 | 状态 | 安装 |
Claude Code(CLI) | 支持 |
|
Claude Desktop | 支持 | |
VS Code(Copilot Chat) | 支持 | 一键安装 或 |
Cursor | 支持 | 一键安装 或设置 |
Windsurf | 支持 |
|
运行测试
pip install -e ".[dev]"
pytest tests/ -v贡献
欢迎贡献!请参阅 docs/contributing.md 了解指南。
许可证
Available Tools
9 toolssession_closeA
Terminate a session gracefully. Sends EOF, then SIGHUP, then SIGKILL.
Inputs: session_id (required).
session_createA
Spawn a persistent PTY terminal session. Returns a session_id used by all other session_* tools. Supports interactive shells, SSH, REPLs, and database CLIs. Snapshot mode is always available.
Inputs: command (required), cols, rows, label, +3 more.
session_execB
Execute a command in a temporary session and return the output. The session is automatically cleaned up after execution.
Inputs: exec (required), cols, rows, command, +2 more.
session_interactA
Send input and read output in a single call. Combines session_send + session_read to halve round trips. Optionally waits for a regex pattern in the output.
Inputs: session_id (required), key, input, timeout, +8 more.
session_listA
List all active terminal sessions with their status and idle time.
Inputs: no named parameters.
session_readA
Read output from a session. Mode 'auto' (default) auto-detects TUI applications and switches between stream and snapshot. 'diff' returns only changed screen lines.
Inputs: session_id (required), mode, timeout, scrollback, +2 more.
session_resizeA
Resize the terminal window of an active session. Sends SIGWINCH to the process.
Inputs: cols (required), rows (required), session_id (required).
session_sendA
Send input text, a control character, or a special key to an active session. Use control_char for signals (e.g. 'c' for Ctrl-C). Use key for special keys (e.g. 'up', 'tab', 'f1').
Inputs: session_id (required), key, input, password, +3 more.
session_wait_forA
Read output from a session until a regex pattern matches or timeout expires. Use this instead of session_read when you know what output to expect.
Inputs: pattern (required), session_id (required), timeout, strip_ansi, +1 more.
Tool Schema Changelog
Recent tool additions, removals, and schema changes observed during successful MCP inspections.
Loading tool schema changelog…
TDQS
Scored across 9 tools
Each tool has a clear, distinct purpose: create/list/resize/send/read/wait/interact/exec/close. Overlaps like session_read vs session_wait_for are well-differentiated by their descriptions (reading vs waiting for a pattern). No two tools could be easily confused.
All tools follow a consistent 'session_verb' pattern using snake_case. Verbs are descriptive (create, list, resize, send, read, wait_for, interact, exec, close). No mixing of styles or vague names.
Nine tools is an ideal size for a terminal session manager. Each tool serves a necessary operation without being too few or too many, covering creation, manipulation, reading, and destruction of sessions.
The tool set provides a full lifecycle for interactive terminal sessions: creation (session_create), querying (session_list), resizing, input sending (session_send), output reading (session_read, session_wait_for), combined interaction (session_interact), one-shot execution (session_exec), and cleanup (session_close). No obvious gaps.
Maintenance
Related MCP Connectors
Persistent cloud workspaces for AI agents: run commands, edit files, use git and a browser.
Persistent Linux computers for AI agents: desktop, signed-in browser, vault, apps and shell.
Cloud desktops for AI agents: create a Linux computer, see its screen, click, type and run tasks.
Operate Linux, macOS and Windows from your LLM. Every action runs through an auditable allowlist.
Related MCP Servers
- FlicenseNot gradedqualityDmaintenanceProvides stateful, interactive terminal access for LLMs to spawn and maintain persistent processes like SSH sessions, debuggers, and REPLs with continuous input/output interaction across commands.8-
- AlicenseNot gradedqualityDmaintenanceProvides AI agents with fully interactive terminal sessions, including TUI support, keyboard control, and screen capture across Windows, Linux, and Mac.MIT
- AlicenseNot gradedqualityDmaintenanceEnables AI agents to have persistent, fully interactive SSH sessions into remote hosts, behaving like a local terminal.9 npm2MIT
- AlicenseNot gradedqualityCmaintenanceEnables AI agents to securely execute terminal commands with persistent sessions, async jobs, and mission control, while providing a live dashboard for human oversight.4Apache 2.0