Skip to main content
Glama
terminal-mcp

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 -rf、DROP TABLE、curl|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

Available Tools

9 tools
session_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

    A4.1/5.0

    Scored across 9 tools

    Disambiguation5/5

    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.

    Naming Consistency5/5

    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.

    Tool Count5/5

    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.

    Completeness5/5

    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

    ActivityNo data
    ResponsivenessLimited data

    Related MCP Connectors

    Related MCP Servers

    • A
      license
      Not graded
      quality
      D
      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
      Not graded
      quality
      D
      maintenance
      Enables AI agents to have persistent, fully interactive SSH sessions into remote hosts, behaving like a local terminal.
      9 npm
      2
      MIT
    • A
      license
      Not graded
      quality
      C
      maintenance
      Enables AI agents to securely execute terminal commands with persistent sessions, async jobs, and mission control, while providing a live dashboard for human oversight.
      4
      Apache 2.0