关于 vscode-terminal-mcp
vscode-terminal-mcp 是一个 MCP 服务,可在可见的 VSCode 终端标签页中运行命令并捕获输出。它面向在 VS Code 中工作的 AI Agent,支持复用会话、增量读取和交互式输入。
主要功能
- 01在可见的 VSCode 终端中执行命令
- 02复用空闲终端会话
- 03无需等待即可运行并增量读取输出
- 04向提示和交互式程序发送输入
- 05通过 agentId 隔离并行任务
适用场景
- 01在 VSCode 中运行测试并查看输出
- 02非阻塞地监控长时间运行的构建
- 03响应交互式命令中的提示
- 04隔离并行 Agent 的终端输出
MCP 相关
MCP 服务提供创建终端会话、执行命令、读取输出、发送输入、列出会话和关闭会话的工具。MCP 客户端通过 stdio 与入口程序通信。
配置
本地服务的配置在各客户端中通用,复制后粘贴到客户端的 MCP 配置文件即可。
工具
本地服务需要在你的电脑上运行后才能读取工具,维护者暂未提供工具列表。
详细介绍
vscode-terminal-mcp
可在可见的 VSCode 终端标签页中执行命令并完整捕获输出的 MCP 服务。与内联执行不同,每条命令都会在真实终端中运行,你可以查看、滚动浏览并与之交互。
主要功能
- 可见终端:命令在真实的 VSCode 终端标签页中运行,而非隐藏进程。你可以实时查看所有内容。
- 会话复用:
run工具会自动复用空闲会话,仅在需要时创建新终端。 - 支持长时间运行的任务:使用
waitForCompletion: false即可启动后不等待,然后通过read增量轮询输出。 - 子 Agent 隔离:使用
agentId标记会话,以隔离并行 Agent 的工作负载。
要求
- VS Code 1.93+(需要 Shell Integration API)
- Node.js 20+
快速开始
Claude Code
claude mcp add BashTerm -- npx vscode-terminal-mcp@latest
VS Code / Copilot
将以下内容添加到 .vscode/mcp.json:
{
"servers": {
"BashTerm": {
"type": "stdio",
"command": "npx",
"args": ["vscode-terminal-mcp@latest"]
}
}
}
Cursor
将以下内容添加到 .cursor/mcp.json:
{
"mcpServers": {
"BashTerm": {
"command": "npx",
"args": ["-y", "vscode-terminal-mcp@latest"]
}
}
}
Claude Desktop
将以下内容添加到 claude_desktop_config.json:
{
"mcpServers": {
"BashTerm": {
"command": "npx",
"args": ["-y", "vscode-terminal-mcp@latest"]
}
}
}
你的第一个提示词
安装后,可以尝试询问:
在终端中运行
ls -la
你应该会看到 VSCode 中打开一个新的终端标签页,并显示命令输出。
截图
使用 run 运行命令

exec 的权限对话框

输出干净的 Exec 结果

工具
快速执行
| 工具 | 描述 |
|---|---|
run | 创建(或复用)终端,并一步执行命令。返回干净的输出和退出代码。 |
会话管理
| 工具 | 描述 |
|---|---|
create | 创建新的可见终端会话。返回 sessionId。 |
exec | 在现有会话中执行命令并捕获输出。 |
read | 分页读取会话输出。支持增量读取和尾部模式(offset: -N)。 |
input | 向交互式终端发送文本(提示、REPL、确认)。 |
list | 列出活动会话。可按 agentId 筛选。 |
close | 关闭终端会话及其 VSCode 标签页。 |
使用模式
简单命令
run 工具会处理所有操作——按需创建终端、执行命令并返回干净的输出:
> Run npm test
$ npm test
PASS src/utils.test.ts (3 tests)
PASS src/index.test.ts (5 tests)
[exit: 0 | 1243ms | session-abc123]
长时间运行的进程
适用于构建、部署或任何需要较长时间的命令:
> Start `npm run build` without waiting, then check progress
Agent 将:
- 使用
waitForCompletion: false调用run——立即返回 - 使用
offset: -10调用read,检查最后 10 行 - 重复以上步骤,直到进程完成
交互式命令
适用于需要用户输入的命令:
> Run npm init and answer the prompts
Agent 将:
- 使用
npm init调用run - 调用
read查看提示 - 调用
input发送答案
并行 Agent
子 Agent 可以使用 agentId 在相互隔离的终端中工作:
> Have one agent run tests while another runs the linter
每个子 Agent 都会获得一个标记了其 agentId 的独立终端,避免输出混杂。
配置
此扩展会从 VSCode 设置中读取 terminalMcp.* 下的配置:
| 设置 | 类型 | 默认值 | 描述 |
|---|---|---|---|
terminalMcp.maxSessions | number | 10 | 并发终端会话的最大数量 |
terminalMcp.commandTimeout | number | 30000 | 命令的默认超时时间,单位为 ms |
terminalMcp.maxOutputLines | number | 5000 | 每个会话的输出缓冲区最多保留的行数 |
terminalMcp.idleTimeout | number | 1800000 | 空闲会话在此时间后关闭,单位为 ms(0 = 禁用) |
terminalMcp.blockedCommands | string[] | ["rm -rf /"] | 将被拒绝的命令 |
推荐:设为首选工具
Claude Code 等 LLM Agent 内置了 Bash 工具,可在聊天中直接执行命令。命令输出会嵌入对话中,难以阅读,尤其是输出内容较多的命令。我们建议告知 Agent 优先使用此 MCP,而不是内置的 Bash 工具。
将以下内容添加到项目的 CLAUDE.md(或同等的指令文件)中:
## Terminal Execution
Prefer the BashTerm MCP tools (`run`, `exec`, `read`, etc.) over the built-in Bash tool for executing commands.
BashTerm runs commands in visible VSCode terminal tabs where the user can see output in real time.
Only fall back to the built-in Bash tool for simple, non-interactive operations like reading environment variables.
For commands that may take longer than 30 seconds or produce large amounts of output (builds, test suites,
deployments, installs), use the pull mode pattern:
1. Call `run` with `waitForCompletion: false` to launch the command without blocking.
2. Call `read` with `offset: -10` to check the last 10 lines of output.
3. Repeat step 2 until you see the command has finished (look for exit messages, prompts, or "Done").
4. Report the final result to the user.
This prevents conversation timeouts and lets the user watch progress in the terminal in real time.
这样做的原因:
| 内置 Bash | BashTerm MCP | |
|---|---|---|
| 输出可见性 | 嵌入聊天中,难以滚动查看 | 显示在 VSCode 终端选项卡中 |
| 实时反馈 | 命令完成前,用户看不到任何内容 | 用户可实时查看输出 |
| 长时间运行的命令 | 超时前会阻塞对话 | 即发即忘 + 轮询 |
| 会话状态 | 每条命令相互隔离 | 会话持久化,并保留历史记录 |
| 交互式命令 | 不支持 | 可向提示词/REPL 发送输入 |
开发:更新扩展
VSCode 会积极地在内存中缓存扩展。在本地开发时,code --install-extension 甚至“Developer: Reload Window”都可能无法重新加载更改。请使用以下工作流:
快速更新(无需重启)
修改源文件后,进行构建并直接复制到已安装的扩展目录中:
cd /path/to/vscode-terminal-mcp
npm run build
cp dist/extension.js ~/.vscode/extensions/sirlordt.vscode-terminal-mcp-<version>/dist/extension.js
然后运行 “Developer: Reload Window”(Ctrl+Shift+P)。
完整重新安装(快速更新不起作用时)
如果 VSCode 仍在使用旧代码:
# 1. Uninstall and remove all copies
code --uninstall-extension sirlordt.vscode-terminal-mcp
rm -rf ~/.vscode/extensions/sirlordt.vscode-terminal-mcp-*
# 2. Check for ghost entries with old publisher names
# Look in ~/.vscode/extensions/extensions.json for stale entries
# Remove any entries with old publisher IDs (e.g., "terminal-mcp.vscode-terminal-mcp")
# 3. Close VSCode completely (not just reload)
# 4. Rebuild and install
npm run build
npx vsce package --allow-missing-repository
code --install-extension vscode-terminal-mcp-<version>.vsix --force
# 5. Open VSCode
确认已加载正确版本
# Check which extension directories exist
ls ~/.vscode/extensions/ | grep terminal
# Verify your changes are in the installed extension
grep "YOUR_UNIQUE_STRING" ~/.vscode/extensions/sirlordt.vscode-terminal-mcp-*/dist/extension.js
# Compare checksums
md5sum dist/extension.js ~/.vscode/extensions/sirlordt.vscode-terminal-mcp-*/dist/extension.js
处理大量输出
当 read 返回的输出超过 MCP 客户端的令牌限制时,系统会自动将完整输出保存到临时 JSON 文件,并在错误消息中返回文件路径。
如需提取相关内容:
# Get the last 50 lines (most relevant for status)
tail -50 /path/to/saved/file.txt
# Or parse the JSON to extract the text content
python3 -c "import json; data=json.load(open('/path/to/file.txt')); print(data[0]['text'][-2000:])"
文件格式为 JSON:[{"type": "text", "text": "..."}]
这通常发生在生成大量 TUI 输出(进度条、ANSI 转义码)的命令中。使用较小的 offset 值(例如,使用 offset: -20 而不是 offset: -100)可减少捕获的输出量。
工作原理
- VSCode 扩展激活,并在 Unix 套接字上启动 IPC 服务
- MCP 入口点(
mcp-entry.js)由 MCP 客户端启动,在 JSON-RPC 标准输入/输出与 IPC 套接字之间进行桥接 - 命令通过 Shell Integration API 在真实的 VSCode 终端中执行,以可靠地捕获输出并检测退出代码
- 输出存储在循环缓冲区中,并支持分页,以便高效读取
最新更改(0.1.6)
- 添加了用于市场页面的 README 截图
- 所有工具的输出格式更简洁——不再显示原始 JSON
- 修复了
waitForCompletion: false不起作用的问题 - 禁用了空闲回收器——由用户手动关闭会话
- 每个工作区使用唯一的 IPC 套接字(支持多实例)
- 自定义终端选项卡名称,采用日期格式
- 添加了大量输出处理文档
完整历史记录请参阅 CHANGELOG.md。
许可证
MIT
评论
一个开放协议,规定了 AI 应用如何以统一方式连接外部工具与数据源。客户端负责与模型对话,MCP 服务负责提供工具、资源和提示模板,两者通过标准消息通信,同一个服务可以被不同客户端复用。