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 了解指南。
许可证
Maintenance
Resources
Unclaimed servers have limited discoverability.
Looking for Admin?
If you are the server author, to access and configure the admin panel.
Related MCP Servers
- Flicense-qualityDmaintenanceProvides 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.7
- Alicense-qualityCmaintenanceProvides AI agents with fully interactive terminal sessions, including TUI support, keyboard control, and screen capture across Windows, Linux, and Mac.MIT
- Alicense-qualityCmaintenanceEnables AI agents to have persistent, fully interactive SSH sessions into remote hosts, behaving like a local terminal.231MIT
- Alicense-qualityDmaintenanceEnables AI agents to spawn and interact with real terminal sessions, capturing screenshots of rendered TUI output and sharing live sessions for debugging.01MIT
Related MCP Connectors
Operate Linux, macOS and Windows from your LLM. Every action runs through an auditable allowlist.
Let AI operate servers without SSH. Choose actions, approve risky changes, and audit every step.
Run AI customer support from your terminal: conversations, knowledge base, and chat widget.
Latest Blog Posts
- Who's Calling? MCP Hosts Are an Identity Blind Spot (And the Spec Knows It)By Om-Shree-0709 on .mcpAgent IdentityOAuth 2.1
- Your AI Chatbot Just Exposed Your CEO's Salary to an InternBy Om-Shree-0709 on .Agent IdentityMCP SecurityOAuth Delegation
- Why MCP Servers Need Execution Sandboxing (And Why Your Current Stack Isn't Enough)By Om-Shree-0709 on .Agentic AiPrompt InjectionWebAssembly
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/mkpvishnu/terminal-mcp'
If you have feedback or need assistance with the MCP directory API, please join our Discord server