内容
<p align="center">
<h1 align="center">tui-mcp</h1>
<p align="center">
通过AI代理控制TUI应用程序的MCP服务器
<br />
<a href="https://www.npmjs.com/package/@blue-hope/tui-mcp"><strong>npm</strong></a>
·
<a href="https://github.com/blue-hope/tui-mcp/issues"><strong>问题</strong></a>
</p>
</p>
<p align="center">
<a href="https://www.npmjs.com/package/@blue-hope/tui-mcp"><img src="https://img.shields.io/npm/v/@blue-hope/tui-mcp.svg" alt="npm版本" /></a>
<a href="https://www.npmjs.com/package/@blue-hope/tui-mcp"><img src="https://img.shields.io/npm/dm/@blue-hope/tui-mcp.svg" alt="npm下载量" /></a>
<a href="https://github.com/blue-hope/tui-mcp/blob/main/LICENSE"><img src="https://img.shields.io/github/license/blue-hope/tui-mcp.svg" alt="许可证" /></a>
<a href="https://github.com/blue-hope/tui-mcp"><img src="https://img.shields.io/github/stars/blue-hope/tui-mcp.svg?style=social" alt="GitHub星标" /></a>
</p>
---
在沙箱PTY会话中启动TUI应用程序(vim、htop、lazygit等),并让AI代理观察和与它们交互。
类似于 [chrome-devtools MCP](https://github.com/anthropics/chrome-devtools-mcp) 用于浏览器和 [mobile-mcp](https://github.com/mobile-next/mobile-mcp) 用于移动设备 — 但适用于终端UI。
```
代理:launch({ command: "vim", args: ["main.py"] })
代理:type_text({ sessionId: "a3f8k2x1", text: "iprint('hello')" })
代理:press_key({ sessionId: "a3f8k2x1", key: "Escape" })
代理:type_text({ sessionId: "a3f8k2x1", text: ":wq" })
代理:press_key({ sessionId: "a3f8k2x1", key: "Enter" })
```
## 快速开始
将以下内容添加到您的MCP客户端配置中:
<details>
<summary><strong>Claude Desktop</strong></summary>
编辑 `~/Library/Application Support/Claude/claude_desktop_config.json`:
```json
{
"mcpServers": {
"tui": {
"command": "npx",
"args": ["-y", "@blue-hope/tui-mcp"]
}
}
}
```
</details>
<details>
<summary><strong>Claude Code</strong></summary>
将以下内容添加到项目根目录的 `.mcp.json` 中:
```json
{
"mcpServers": {
"tui": {
"command": "npx",
"args": ["-y", "@blue-hope/tui-mcp"]
}
}
}
```
</details>
<details>
<summary><strong>其他MCP客户端</strong></summary>
任何支持stdio传输的MCP客户端:
```json
{
"command": "npx",
"args": ["-y", "@blue-hope/tui-mcp"]
}
```
</details>
## 工具
### 会话管理
| 工具 | 描述 |
|------|-------------|
| `launch` | 在新的PTY会话中启动TUI应用程序 |
| `list_sessions` | 列出所有活动会话 |
| `kill_session` | 终止会话 |
| `send_signal` | 发送POSIX信号(SIGINT,SIGTSTP,SIGCONT等) |
| `wait_for_exit` | 等待进程退出,返回退出代码 |
### 观察
| 工具 | 描述 |
|------|-------------|
| `take_snapshot` | 捕获屏幕内容,光标位置和尺寸 |
| `get_cursor_position` | 获取当前光标X/Y |
| `get_screen_size` | 获取终端列/行 |
| `find_text` | 在屏幕上查找文本位置(行,列) |
### 输入
| 工具 | 描述 |
|------|-------------|
| `type_text` | 向TUI发送文本输入 |
| `press_key` | 发送按键(Enter,Ctrl+C,Alt+X,F5等) |
### 控制
| 工具 | 描述 |
|------|-------------|
| `resize` | 调整终端大小 |
| `wait_for_text` | 等待特定文本出现在屏幕上 |
| `wait_for_stable` | 等待屏幕停止变化 |
## 示例
### 使用vim编辑文件
```
launch({ command: "vim", args: ["file.txt"] })
→ { sessionId: "a3f8k2x1", pid: 12345, cols: 80, rows: 24 }
type_text({ sessionId: "a3f8k2x1", text: "ihello world" })
press_key({ sessionId: "a3f8k2x1", key: "Escape" })
type_text({ sessionId: "a3f8k2x1", text: ":wq" })
press_key({ sessionId: "a3f8k2x1", key: "Enter" })
```
### 在仓库中使用lazygit
```
launch({ command: "lazygit", args: [], cwd: "/path/to/repo" })
take_snapshot({ sessionId: "..." })
→ { lines: [...], cursor: { x: 0, y: 0 }, rows: 24, cols: 80 }
```
### 运行命令并等待输出
```
launch({ command: "npm test" })
wait_for_text({ sessionId: "...", text: "Tests passed", timeout: 30000 })
```
### 自定义环境和尺寸
```
launch({
command: "htop",
args: [],
cols: 120,
rows: 40,
env: { "HTOPRC": "/path/to/config" }
})
```
## `launch` 模式
| 模式 | 何时 | 示例 |
|------|------|---------|
| **直接** | 提供 `args` | `launch({ command: "vim", args: ["file.txt"] })` |
| **Shell** | 省略 `args` | `launch({ command: "ls -la \| head -20" })` |
直接模式直接调用二进制文件。Shell模式通过 `$SHELL -c "..."` 运行,支持管道,通配符和环境变量扩展。
## 键支持
支持任意键组合:
| 类别 | 示例 |
|----------|---------|
| 命名键 | `Enter`, `Tab`, `Escape`, `Backspace`, `Space`, `Delete`, `Home`, `End`, `PageUp`, `PageDown` |
| 箭头键 | `ArrowUp`, `ArrowDown`, `ArrowLeft`, `ArrowRight` (或 `Up`, `Down`, `Left`, `Right`) |
| 功能键 | `F1` 至 `F12` |
| Ctrl组合键 | `Ctrl+C`, `Ctrl+Z`, `Ctrl+A`, `Ctrl+D`, ... |
| Alt组合键 | `Alt+X`, `Alt+Enter`, ... |
| 单个字符 | `a`, `1`, `/`, ... |
## 配置
| 环境变量 | 默认值 | 描述 |
|---------------------|---------|-------------|
| `TUI_MCP_IDLE_TIMEOUT` | `1800000` (30分钟) | 空闲会话超时毫秒 |
## 工作原理
```
AI代理(Claude,Cursor等)
│ stdio(JSON-RPC 2.0)
▼
┌──────────────────────────────────┐
│ tui-mcp服务器 │
│ │
│ ┌────────────┐ ┌────────────┐ │
│ │ 会话A │ │ 会话B │ │
│ │ ┌────────┐ │ │ ┌────────┐ │ │
│ │ │node-pty│ │ │ │node-pty│ │ │
│ │ └───┬────┘ │ │ └───┬────┘ │ │
│ │ │ PTY │ │ │ PTY │ │
│ │ ┌───▼────┐ │ │ ┌───▼────┐ │ │
│ │ │ xterm │ │ │ │ xterm │ │ │
│ │ │无头│ │ │ │无头│ │ │
│ │ └────────┘ │ │ └────────┘ │ │
│ └────────────┘ └────────────┘ │
└──────────────────────────────────┘
│ │
▼ ▼
vim htop
```
每个会话都将 [node-pty](https://github.com/microsoft/node-pty) 进程与 [@xterm/headless](https://www.npmjs.com/package/@xterm/headless) 终端模拟器配对。PTY启动TUI应用程序;xterm将其ANSI输出解析为可读的屏幕缓冲区。会话内的工具调用通过每个会话的互斥锁序列化。
## 开发
```bash
git clone https://github.com/blue-hope/tui-mcp.git
cd tui-mcp
npm install
npm run dev # 使用tsx运行(热重载)
npm run build # 编译TypeScript
npm test # 运行测试
```
### 项目结构
```
src/
├── index.ts # 入口点,MCP服务器引导
├── session.ts # 会话:PTY + 终端对
├── session-manager.ts # 会话生命周期,空闲超时
├── tools.ts # 11个MCP工具注册
├── keys.ts # 键名 → ANSI序列映射器
├── mutex.ts # 基于Promise的FIFO互斥锁
├── tool-wrapper.ts # 处理器包装器(日志记录,错误)
└── __tests__/ # vitest测试套件
```
## 需求
- Node.js >= 18
- macOS 或 Linux (node-pty需要POSIX)
- node-pty原生模块的C++构建工具 (`node-gyp`)
## 许可证
[MIT](LICENSE)
MCP 配置
以下是此 MCP Server 的配置信息,可直接复制到 Cursor 或其他 MCP 客户端中使用。
mcp.json
连接信息
猜你喜欢
rea
用于编码代理的逆向工程:一个 CLI 和 MCP 服务器,用于检查任何应用程序,了解其功能,并按照您的方式构建它们。
everything-claude-code
完整的 Claude Code 配置集合 - agents、skills、hooks、commands、rules、MCPs。来自 Anthropic...
Firecrawl
Firecrawl MCP Server 实现了网页抓取、爬虫和内容提取功能。
MarkItDown MCP
MarkItDown-MCP是一个轻量级服务器,用于将URI转换为Markdown。
cc-switch
跨平台的Claude Code、Codex和Gemini CLI一体化助手。
Agent-Reach
赋予你的AI Agent观察整个互联网的眼睛。读取并搜索...