使用说明
mcp交互式终端
MCP服务器,为AI代理(Claude Code、Cursor、Windsurf等)提供真正的交互式终端会话。运行REPL、SSH、数据库客户端和任何交互式CLI——具有干净的文本输出、智能完成检测和7层安全性。
为什么存在
AI编码代理无法处理交互式命令。没有PTY,没有stdin流媒体。你不能跑 rails console, python, psql, ssh,或通过它们进行任何REPL。此MCP服务器修复了此问题。
AI Agent (Claude Code, Cursor, etc.)
↕ MCP (JSON-RPC over stdio)
mcp-interactive-terminal
↕ node-pty + xterm-headless
Interactive Process (rails console, python, psql, ssh, bash...)
↕
Clean text output (exactly what a human would see)
安装
克劳德代码
claude mcp add terminal -- npx -y mcp-interactive-terminal
就是这样。服务器现在可用。让Claude“打开python REPL并计算2**100”。
光标
首选 设置>MCP服务器,单击 添加服务器,然后输入:
{
"mcpServers": {
"terminal": {
"command": "npx",
"args": ["-y", "mcp-interactive-terminal"]
}
}
}
帆板运动
添加到MCP配置中:
{
"mcpServers": {
"terminal": {
"command": "npx",
"args": ["-y", "mcp-interactive-terminal"]
}
}
}
VS代码(GitHub副本)
添加到您的 .vscode/mcp.json:
{
"servers": {
"terminal": {
"command": "npx",
"args": ["-y", "mcp-interactive-terminal"]
}
}
}
任何MCP客户端
服务器通过stdio使用 模型上下文协议任何兼容MCP的客户端都可以使用它 npx -y mcp-interactive-terminal 命令。
真实世界的例子
Rails控制台
You: "Open rails console for staging and check the user count"
Agent creates session → bash
Agent sends: cd /path/to/app && rails console -e staging
Agent sends: User.count
Agent returns: 1,847,293
Python 交互式解释器
You: "Open python and test my sorting algorithm"
Agent creates session → python3
Agent sends: def quicksort(arr): ...
Agent sends: quicksort([3, 1, 4, 1, 5, 9])
Agent returns: [1, 1, 3, 4, 5, 9]
数据库客户端
You: "Connect to postgres and show me the largest tables"
Agent creates session → psql -U myuser mydb
Agent sends: SELECT tablename, pg_size_pretty(pg_total_relation_size(tablename::text)) ...
Agent returns: formatted table of results
安全外壳协议
You: "SSH into the staging server and check disk usage"
Agent creates session → ssh user@staging.example.com
Agent sends: df -h
Agent returns: disk usage table
码头工人
You: "Open a shell in my running container and check the logs"
Agent creates session → docker exec -it my-container bash
Agent sends: tail -100 /var/log/app.log
Agent returns: last 100 log lines
Node.js REPL
You: "Open node and test the date parsing logic"
Agent creates session → node
Agent sends: new Date('2024-02-29').toISOString()
Agent returns: 2024-02-29T00:00:00.000Z
工具
服务器公开了7个MCP工具:
create_session --生成一个互动过程
{ "command": "python3", "name": "my-python", "cwd": "/project" }
→ { "session_id": "a1b2c3d4", "name": "my-python", "pid": 12345 }
| 参数 | 必填 | 默认 | 说明 |
|---|---|---|---|
command |
是 | -- | 要运行的命令(bash、python3、psql、ssh等) |
args |
没有 | [] |
命令参数 |
name |
否 | 自动 | 人类可读的会话名称 |
cwd |
否 | 服务器cwd | 工作目录 |
env |
没有 | {} |
其他环境变量 |
cols |
没有 | 120 |
端子柱 |
rows |
没有 | 40 |
端子排 |
send_command --发送输入并获取输出
{ "session_id": "a1b2c3d4", "input": "1 + 1" }
→ { "output": "2", "is_complete": true, "is_alive": true }
| 参数 | 必填 | 默认 | 说明 |
|---|---|---|---|
session_id |
是 | -- | 目标会话 |
input |
是 | -- | 要发送的命令/输入(自动附加换行符) |
timeout_ms |
没有 | 5000 |
输出的最大等待时间 |
max_output_chars |
没有 | 20000 |
截断超出此范围的输出 |
危险指令(rm -rf, DROP TABLE, curl|bash等)被阻止——代理必须使用 confirm_dangerous_command 第一。
read_output --读取终端屏幕(只读)
{ "session_id": "a1b2c3d4" }
→ { "output": ">>> ", "is_alive": true }
自动批准是安全的——这只会读取,从不发送输入。
list_sessions --列出活动会话(只读)
→ [{ "session_id": "a1b2c3d4", "name": "my-python", "command": "python3", "pid": 12345, "is_alive": true }]
自动批准是安全的。
close_session --终止会话
{ "session_id": "a1b2c3d4" }
→ { "success": true }
send_control --发送控制字符
{ "session_id": "a1b2c3d4", "control": "ctrl+c" }
→ { "output": "^C\n>>>" }
支持: ctrl+c, ctrl+d, ctrl+z, ctrl+l, ctrl+r, tab, escape, up, down, left, right, enter, backspace, delete, home, end以及更多。
confirm_dangerous_command --两步安全确认
{ "session_id": "a1b2c3d4", "input": "rm -rf /tmp/old", "justification": "Cleaning up stale temp files from failed build" }
→ { "output": "...", "is_complete": true, "is_alive": true }
需要时 send_command 检测到危险模式。代理必须解释为什么需要该命令。这是一个 独立工具 --即使 send_command 是自动批准的,这需要它自己的许可。
运作原理
两种终端模式
PTY模式 (默认)--使用 node-pty + @xterm/headless (与VS Code相同的终端仿真器):
- 干净的输出——人工智能能准确地看到人类在屏幕上看到的东西
- 光标定位、进度条、,
\r正确覆盖所有渲染 - 全键盘:箭头键、制表符补全、ctrl+c/d/z、主页/结束
- 终端大小调整,TUI应用程序(vim、htop、top),256色,1000行滚动
管道模式 (自动回退)——当节点pty无法加载时激活(例如,在沙盒环境中):
- 交互式会话仍然可以通过
child_process.spawn带有自动注入标志(python -u -i,bash -i等等) - ANSI代码被删除,控制键仍然有效
- 没有终端模拟,但涵盖了基础知识
模式是自动选择的——首先尝试PTY,如果失败,管道模式将启动。
AI看到了什么:PTY vs Pipe
| 场景 | PTY模式 | 管道模式 |
|---|---|---|
printf "\rProgress: 3/3" |
Progress: 3/3 |
Progress: 1/3Progress: 2/3Progress: 3/3 |
| ANSI颜色 | 干净地剥离 | 通过正则表达式剥离 |
| vim,htop,top | 可读屏幕 | 乱码 |
| 箭头键,选项卡完成 | 工程 | 工程 |
| 终端大小调整 | 有效 | 无操作 |
智能“命令完成”检测
服务器使用分层策略,而不是盲目等待固定时间:
- 进程退出 --如果进程终止,则命令完成
- 快速检测 --启动时自动检测会话的提示(bash
$python>>>,psql#等),等待它再次出现 - 输出稳定 --300ms内没有新的输出=可能已经完成
- 超时 --总是在之后返回
timeout_ms随着is_complete: false
安全
七层纵深防御:
| 图层 | 功能 | 默认值 |
|---|---|---|
| MCP工具注释 | readOnlyHint/destructiveHint 在每个工具上 |
始终打开 |
| 确认流程 | 危险模式需要 confirm_dangerous_command |
始终开启 |
| 输入模式检测 | 检测rm-rf、DROP TABLE、curl | bash等。 |
| 命令阻止列表/允许列表 | 阻止/允许特定命令 | 可配置 |
| 操作系统级沙盒 | 内核级进程沙盒 @anthropic-ai/sandbox-runtime |
关闭(选择加入) |
| 秘密重设 | 在输出中重设AWS密钥、令牌、私钥 | 关闭(选择加入) |
| 资源限制 | 最大会话数、输出上限、空闲超时、审核日志记录 | 始终打开 |
推荐权限
仅自动批准只读工具:
{
"permissions": {
"allow": [
"mcp__terminal__list_sessions",
"mcp__terminal__read_output"
]
}
}
这边 send_command, create_session,尤其是 confirm_dangerous_command 总是需要人类的认可。
配置
所有设置均通过环境变量进行。在MCP配置中传递它们:
{
"mcpServers": {
"terminal": {
"command": "npx",
"args": ["-y", "mcp-interactive-terminal"],
"env": {
"MCP_TERMINAL_ALLOWED_COMMANDS": "bash,python3,node,psql",
"MCP_TERMINAL_REDACT_SECRETS": "true",
"MCP_TERMINAL_IDLE_TIMEOUT": "300000"
}
}
}
}
| 变量 | 默认值 | 描述 |
|---|---|---|
MCP_TERMINAL_MAX_SESSIONS |
10 |
最大并发会话数 |
MCP_TERMINAL_MAX_OUTPUT |
20000 |
每次读取的最大输出字符数 |
MCP_TERMINAL_DEFAULT_TIMEOUT |
5000 |
默认等待超时(ms) |
MCP_TERMINAL_BLOCKED_COMMANDS |
-- | 逗号分隔的块列表 |
MCP_TERMINAL_ALLOWED_COMMANDS |
-- | 逗号分隔的列表(如果设置,则只允许使用这些列表) |
MCP_TERMINAL_ALLOWED_PATHS |
-- | 会话可以访问的逗号分隔路径 |
MCP_TERMINAL_REDACT_SECRETS |
false |
在输出中修改AWS密钥、令牌和私钥 |
MCP_TERMINAL_LOG_INPUTS |
false |
将所有输入记录到stderr(用于调试) |
MCP_TERMINAL_IDLE_TIMEOUT |
1800000 |
自动关闭空闲会话(毫秒,默认30分钟,0=禁用) |
MCP_TERMINAL_DANGER_DETECTION |
true |
启用危险命令确认流程 |
MCP_TERMINAL_AUDIT_LOG |
-- | JSON审核日志文件的路径 |
MCP_TERMINAL_SANDBOX |
false |
启用操作系统级内核沙盒 |
MCP_TERMINAL_SANDBOX_ALLOW_WRITE |
/tmp |
沙盒模式下的可写路径 |
MCP_TERMINAL_SANDBOX_ALLOW_NETWORK |
* |
沙盒中允许的网络域 |
故障排除
“工具未显示”/服务器静默失败
无法启动的MCP服务器通常不会在客户端中显示错误。检查:
# Test the server directly:
npx -y mcp-interactive-terminal
# You should see "[mcp-terminal] Starting MCP Interactive Terminal Server" on stderr.
# If you see an error, that's what's failing.
Node.js版本太旧
服务器要求Node.js>=18。如果您看到有关不支持的语法或缺少API的错误:
node --version # Must be >= 18
# If using nvm:
nvm install 18 && nvm use 18
# If using volta:
volta install node@18
适用于nvm/volta/fnm用户: npx 可能使用与shell不同的Node版本。使用绝对路径:
{
"mcpServers": {
"terminal": {
"command": "/Users/you/.nvm/versions/node/v22.0.0/bin/npx",
"args": ["-y", "mcp-interactive-terminal"]
}
}
}
通过以下方式找到你的路: which npx
节点pty编译错误
node-pty 是一个需要构建工具的原生模块。如果编译失败,服务器会自动回退到 管道模式 --交互式会话仍然可以工作,只是没有终端模拟。
如果您需要完全的PTY支持:
# macOS:
xcode-select --install
# Ubuntu/Debian:
sudo apt-get install -y make python3 build-essential
# RHEL/Fedora:
sudo yum install -y make python3 gcc gcc-c++
会话立即终止
有些命令需要在shell中运行,而不是直接运行:
# Instead of: create_session({ command: "rails console -e staging" })
# Do this: create_session({ command: "bash" })
# send_command({ input: "rails console -e staging" })
这是因为 create_session 直接运行命令(如 exec)不是通过外壳。产卵 bash 首先给你一个完整的shell环境。
输出看起来混乱
如果输出包含转义码或看起来有误,您可能会 管道模式 (节点pty加载失败)。检查服务器日志 "falling back to pipe mode".安装构建工具(见上文)以启用PTY模式。
对于长时间运行的命令,超时时间太短
增加每个命令的超时时间:
{ "session_id": "...", "input": "bundle install", "timeout_ms": 60000 }
或者全局通过环境变量:
{ "env": { "MCP_TERMINAL_DEFAULT_TIMEOUT": "30000" } }
与备选方案的比较
| 功能 | mcp交互式终端 | 特定于应用程序的终端服务器 | 通用外壳mcp服务器 |
|---|---|---|---|
| 跨平台 | 是 | 通常只有单个应用程序 | 各不相同 |
| 干净输出(xterm无头) | 是 | 否(屏幕刮擦) | 否(原始PTY转储) |
| 智能完成检测 | 4层算法 | 否 | 基本超时 |
| 安全层 | 7(确认流、沙盒、编校等) | 无 | 基本 |
| 危险命令确认 | 是(单独工具) | 否 | 否 |
| MCP工具注释 | 是 | 否 | 否 |
| 后台会话 | 是 | 否(使用活动选项卡) | 是 |
| 聚焦API | 7个工具 | 2-3个工具 | 15-20+个工具(范围蔓延) |
| 安装 | npx -y (零配置) |
需要特定的应用程序 | 各不相同 |
发展
git clone https://github.com/amol21p/mcp-interactive-terminal.git
cd mcp-interactive-terminal
npm install
npm run build
npm test
MCP检验员测试:
npx @modelcontextprotocol/inspector dist/index.js
许可证
麻省理工学院