Serial Web Terminal MCP

Serial Web Terminal MCP

Enables AI assistants to interact with serial port devices by sending commands, capturing output, and managing connections, with real-time browser-based terminal monitoring.

Category
访问服务器

README

Serial Web Terminal MCP

Python 3.11+ License: MIT MCP Compatible

A Model Context Protocol server that gives AI coding assistants (Claude Code, Cursor, Windsurf, etc.) the ability to interact with serial port devices.

AI agents can connect to serial devices, send commands, and capture output — while users watch the entire process in real-time through a browser-based terminal.

✨ Features

  • 🔌 Serial Connection — Connect to COM ports, /dev/ttyUSB*, /dev/ttyS*, etc. with automatic login
  • 🖥️ Web Terminal — xterm.js browser terminal showing real-time serial I/O (like Xshell)
  • 🤖 MCP Server — Native tool integration; AI agents call directly via MCP protocol
  • 📝 Timestamped Logs — Every line logged with timestamps, daily rotation, matches terminal display exactly
  • ⌨️ Bidirectional — AI sends commands + user can type manually in the browser terminal
  • 🌐 Multi-language Login — Auto-detects login/password prompts in English, Chinese, and Japanese
  • ⏱️ Wait-and-Send — Wait for specific output then immediately send data (e.g., uboot password windows)
  • 🛡️ Timeout Recovery — Automatic Ctrl+C on timeout, no hung sessions

📦 Installation

pip install mcp pyserial aiohttp

Or from requirements:

pip install -r requirements.txt

🚀 Quick Start

1. Configure your AI client

Claude Code (.mcp.json in project root or ~/.claude/claude_config.json):

{
  "mcpServers": {
    "serial-terminal": {
      "command": "python",
      "args": ["/path/to/serial_mcp_server.py"]
    }
  }
}

Cursor (Settings → MCP → Add Server):

{
  "mcpServers": {
    "serial-terminal": {
      "command": "python",
      "args": ["/path/to/serial_mcp_server.py"]
    }
  }
}

See examples/ for ready-to-use configuration files.

2. Talk to your AI assistant

> List available serial ports
AI: [calls serial_list_ports] → Found COM3, COM4...

> Connect to COM3, username admin, password ****
AI: [calls serial_connect(port="COM3", login_user="admin", login_pass="****")]
    → Serial connected, Web terminal: http://localhost:8080

> Run uname -a
AI: [calls serial_send(command="uname -a")]
    → Linux device 4.19.246 aarch64 GNU/Linux

Open http://localhost:8080 in your browser to watch AI's serial operations in real-time.

🔧 MCP Tools

Tool Description
serial_list_ports List all available serial port devices
serial_connect Connect to a serial port and start the web terminal (supports auto-login)
serial_send Send a shell command and return device output
serial_raw Send raw data (e.g., Ctrl+C = \x03)
serial_wait_send Wait for specific output, then immediately send data (for time-critical operations)
serial_status Check current connection status
serial_log Get timestamped operation logs
serial_disconnect Disconnect and stop the web terminal

serial_connect

Connect to a serial device with optional auto-login.

Parameter Type Default Description
port str (required) Serial device name (e.g., COM3, /dev/ttyUSB0)
baudrate int 115200 Baud rate
login_user str "" Auto-login username (skip if empty)
login_pass str "" Auto-login password
init_cmd str unset TMOUT Command to run after login (prevent session timeout)
web_port int 8080 Web terminal port

serial_send

Send a shell command and capture output.

Parameter Type Default Description
command str (required) Shell command to execute
timeout int 8 Response timeout in seconds

serial_wait_send

Wait for a specific string in serial output, then immediately send data. Ideal for:

  • Entering uboot during reboot (3-second password window)
  • Responding to login prompts
  • Any "wait for X, then send Y" automation
Parameter Type Default Description
wait_for str (required) Target string to wait for
send_data str (required) Data to send when target is found
timeout int 60 Max wait time in seconds
trigger str "" Optional data to send before waiting (e.g., \r\n to re-trigger a static prompt)

🖥️ Standalone Usage (without MCP)

serial_web.py can run independently via HTTP API:

# Start with auto-login
python serial_web.py --port COM3 --baud 115200 \
  --login-user admin --login-pass secret \
  --init-cmd "unset TMOUT"

# List available ports
python serial_web.py --list

HTTP API

# Send a command
curl -s -X POST http://localhost:8080/api/send \
     -H "Content-Type: application/json" \
     -d '{"command":"ls /","timeout":5}'

# Send raw data (Ctrl+C)
curl -s -X POST http://localhost:8080/api/raw \
     -H "Content-Type: application/json" \
     -d '{"data":"\x03"}'

# Wait-and-send
curl -s -X POST http://localhost:8080/api/wait-send \
     -H "Content-Type: application/json" \
     -d '{"wait_for":"login:","send_data":"admin","timeout":30}'

# Check status
curl -s http://localhost:8080/api/status

# Get logs
curl -s "http://localhost:8080/api/log?lines=50"

CLI Arguments

Argument Default Description
--port (required) Serial device name (COM3, /dev/ttyUSB0)
--baud 115200 Baud rate
--web-port 8080 Web server port
--login-user (none) Auto-login username
--login-pass (none) Auto-login password
--init-cmd unset TMOUT Post-login command (use ; for multiple)
--prompt-regex (auto) Custom prompt detection regex
--list List available serial ports

📝 Log Format

Logs are saved to logs/serial_YYYYMMDD.log (daily rotation):

2026-08-06 15:32:22  device # uname -a
2026-08-06 15:32:22  Linux device 4.19.246 aarch64 GNU/Linux
2026-08-06 15:32:23  device # cat /proc/cpuinfo | head -5
2026-08-06 15:32:23  processor	: 0
2026-08-06 15:32:23  >>> 自动登录流程完成
  • Terminal output: timestamp content (extracted from xterm.js buffer — matches browser display exactly)
  • System events: timestamp >>> message (login, startup, etc.)

Log line fidelity:

  • No wrap splitting — lines soft-wrapped by the terminal (80-column wrap) are merged back into a single logical line
  • Progress-bar aware\r overwrite sequences (10%\r20%\r30%) are collapsed to the final visible state (30%)
  • Backspace-aware — manual edits with backspace are recorded as the final edited line
  • Every line always carries a timestamp prefix

🏗️ Architecture

AI Agent (Claude Code / Cursor / ...)
  └─ MCP Protocol (stdio)
      └─ serial_mcp_server.py
          └─ HTTP API
              └─ serial_web.py (aiohttp)
                  ├─ Serial Port (pyserial)
                  ├─ Web Terminal (xterm.js + WebSocket)
                  └─ Log Recording

Browser
  └─ http://localhost:8080
      ├─ xterm.js terminal (real-time serial data)
      └─ Log panel (timestamped logs)

📁 Project Structure

serial-web-terminal/
├── serial_web.py              # Core: Web terminal + HTTP API
├── serial_mcp_server.py       # MCP Server (wraps HTTP API)
├── tests/
│   └── test_regression.py     # Regression test suite (68 tests)
├── examples/
│   ├── claude-code.json       # Claude Code MCP config
│   └── cursor.json            # Cursor MCP config
├── requirements.txt
├── LICENSE
└── README.md

🧪 Testing

Run the regression test suite (no physical serial device required):

python tests/test_regression.py -v

Tests cover:

  • Output cleaning (ANSI stripping, echo removal, prompt removal)
  • Prompt detection (shell prompts, known prompts)
  • Log line buffering (backspace handling, partial lines, ANSI cleaning)
  • Auto-login keyword detection (English, Chinese, Japanese)
  • Command send/receive (mock serial, timeout, Ctrl+C recovery)
  • Wait-and-send (immediate match, dynamic match, timeout, trigger)
  • HTML page structure (no duplicate IDs, required elements)
  • MCP server tool registration
  • HTTP API endpoints (status, send, raw, log — error handling)
  • Security (no hardcoded credentials, .gitignore coverage)

🌐 Auto-Login

The auto-login flow supports multi-language prompts:

Language Login Prompts Password Prompts
English login: Password:
Chinese 登录: 用户名: 口令: 密码:
Japanese パスワード:

Login flow:

  1. Send Enter to wake the terminal
  2. Detect login: prompt → send username
  3. Detect Password: prompt → send password
  4. Wait for shell prompt
  5. Execute stty cols 200 (wide terminal, prevents 80-column wrapping)
  6. Execute --init-cmd (default: unset TMOUT)

If already logged in (no login prompt detected), skips to step 5.

📄 License

MIT

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选