Skip to main content
Glama
mkpvishnu

terminal-mcp

by mkpvishnu

问题所在

每个 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-mcp

2. 连接到你的 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 会话,可在工具调用之间保持存活

一次调用完成发送 + 读取

session_interact 将 LLM 往返次数减半

基于模式的读取

wait_for 会阻塞直到正则匹配 - 无需猜测超时时间

自动 TUI 检测

检测 htop、vim 等,并自动切换到屏幕快照模式

输出差异模式

仅返回变化的屏幕行 - 最小化 token 消耗

特殊按键

方向键、Tab、F1-F12、Home/End、Page Up/Down

控制字符

Ctrl-C、Ctrl-D、Ctrl-Z、Ctrl-L、telnet 转义

危险命令防护

阻止 rm -rfDROP TABLEcurl|sh - 需要确认

OSC 133 Shell 集成

自动检测命令边界和退出码

智能截断

四种策略防止上下文溢出

秘密输入

发送密码而不记录日志

动态调整大小

通过 SIGWINCH 动态调整终端大小

空闲清理

自动关闭空闲会话

跨平台

支持 Linux、macOS 和 Windows


工具参考

terminal-mcp 暴露了 9 个 MCP 工具。完整详情见 docs/tools.md

工具

用途

session_create

生成一个持久化终端会话

session_send

发送文本、按键或控制字符

session_read

读取输出(流、快照、自动、差异模式)

session_interact

一次调用完成发送 + 读取

session_wait_for

等待输出中出现正则模式

session_exec

一次性命令执行

session_close

优雅关闭会话

session_resize

调整终端尺寸

session_list

列出活动会话


架构

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

设置

环境变量

默认值

最大会话数

TERMINAL_MCP_MAX_SESSIONS

10

空闲超时

TERMINAL_MCP_IDLE_TIMEOUT

1800(30 分钟)

安全防护

TERMINAL_MCP_SAFETY_GATE

on

缓冲区上限

TERMINAL_MCP_MAX_BUFFER_BYTES

1000000(1MB)

截断模式

TERMINAL_MCP_TRUNCATION_MODE

tail

自定义设置示例:

{
  "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.json.mcp.json

Claude Desktop

支持

一键安装

VS Code(Copilot Chat)

支持

一键安装.vscode/mcp.json

Cursor

支持

一键安装 或设置

Windsurf

支持

~/.codeium/windsurf/mcp_config.json


运行测试

pip install -e ".[dev]"
pytest tests/ -v

贡献

欢迎贡献!请参阅 docs/contributing.md 了解指南。

许可证

MIT

Install Server
A
license - permissive license
A
quality
A
maintenance

Maintenance

Maintainers
62dResponse time
6wRelease cycle
5Releases (12mo)
Commit activity
Issues opened vs closed

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

  • A
    license
    -
    quality
    C
    maintenance
    Provides AI agents with fully interactive terminal sessions, including TUI support, keyboard control, and screen capture across Windows, Linux, and Mac.
    MIT
  • A
    license
    -
    quality
    C
    maintenance
    Enables AI agents to have persistent, fully interactive SSH sessions into remote hosts, behaving like a local terminal.
    23
    1
    MIT

View all related MCP servers

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.

View all MCP Connectors

Latest Blog Posts

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