mcp-ssh

mcp-ssh

Enables AI assistants to run commands on remote SSH-accessible devices via persistent sessions, supporting both POSIX and CLI shells with safety filters.

Category
访问服务器

README

mcp-ssh

MCP server for running commands over persistent SSH sessions. Connects AI assistants (Claude, Cursor, etc.) to remote hosts — Linux servers, routers, switches, and any SSH-accessible device.

Built with the help of Claude AI (Anthropic).


Features

  • Persistent SSH sessions — connections are pooled and reused; no reconnect overhead per command
  • Two execution modes — stateless exec for POSIX hosts, stateful PTY shell for interactive CLIs (Cisco, MikroTik, etc.)
  • Safety layer — configurable regex deny-list blocks destructive commands (rm -rf /, mkfs, reboot, etc.)
  • Append-only audit log — every command, decision, and exit code recorded to JSONL
  • Two transportsstdio for local use, SSE with Bearer token auth for remote/Docker deployment
  • No secrets in config — passwords and passphrases are referenced by environment variable name, not stored

MCP Tools

Tool Description
ssh_list_hosts List configured hosts (no secrets exposed)
ssh_connect Open or reuse a persistent session
ssh_run Run a stateless command via exec (POSIX)
ssh_shell Run a command in the interactive PTY shell (stateful, required for CLI devices)
ssh_list_sessions Show active sessions and idle times
ssh_disconnect Close a session

All tools accept confirm_dangerous=true to override a safety block when the command is intentional.

Quick Start

Install

git clone https://github.com/your-username/mcp-ssh
cd mcp-ssh
poetry config virtualenvs.in-project true --local
poetry install

Configure hosts

Use the interactive manager to add, edit, or remove hosts without touching YAML by hand:

python manage_hosts.py
# or with an explicit config path:
MCP_SSH_CONFIG=/path/to/hosts.yaml python manage_hosts.py

The script provides a menu-driven terminal UI: list hosts, add, edit (current values shown as defaults), and delete with confirmation. Validates IP/hostname, port range, regex syntax, and warns if a referenced env var is not exported.

Alternatively, copy hosts.example.yaml and edit manually:

hosts:
  home-server:
    host: 192.168.1.10
    user: admin
    auth:
      method: key
      key_path: ~/.ssh/id_ed25519
    shell: posix

  cisco-switch:
    host: 192.168.1.1
    user: cisco
    auth:
      method: password
      password_env: CISCO_PASS      # name of env var, not the password itself
    shell: cli
    prompt_regex: '[\w.-]+[>#]\s*$'

settings:
  idle_timeout: 600
  command_timeout: 60
  audit_log: ~/.mcp_ssh/audit.log

Export secrets before starting:

export MCP_SSH_CONFIG=/path/to/hosts.yaml
export CISCO_PASS='my-password'

Run (stdio — local use)

poetry run mcp-ssh

Register poetry run mcp-ssh (with env vars set) as a stdio MCP server in your AI assistant.

Run (SSE — remote / Docker)

export MCP_TRANSPORT=sse
export MCP_AUTH_TOKEN=$(python -c "import secrets; print(secrets.token_hex(32))")
export MCP_PORT=8000
poetry run mcp-ssh

MCP_HOST defaults to 127.0.0.1. Set MCP_HOST=0.0.0.0 only when a reverse proxy handles TLS and auth in front.

Shell Types

Device shell Reason
Linux / Raspberry Pi posix Standard POSIX shell, has echo $?
OpenWRT / DD-WRT posix BusyBox ash — POSIX compatible
Cisco IOS / NX-OS cli Proprietary CLI, no $?
MikroTik RouterOS cli Proprietary CLI
Managed switches cli Proprietary CLI

cli hosts require prompt_regex — a regex matching the device prompt (e.g. [\w.-]+[>#]\s*$).

Safety

Default deny patterns block:

rm -rf /      mkfs      reboot      shutdown
dd of=/dev/*  write erase   erase startup-config

Extend or override in hosts.yaml:

settings:
  deny_patterns:
    - "rm\\s+-rf\\s+/"
    - "your-custom-pattern"

Pass confirm_dangerous=true to any tool to override a block when you know it is intentional.

Docker / Portainer

See docs/portainer-deployment.md for a full guide including nginx reverse proxy setup.

docker compose up -d

Key environment variables for the container:

Variable Required Default Description
MCP_SSH_CONFIG yes Path to hosts.yaml inside the container
MCP_TRANSPORT no stdio Set to sse for HTTP transport
MCP_AUTH_TOKEN yes (SSE) Bearer token; required in SSE mode
MCP_HOST no 127.0.0.1 Bind address
MCP_PORT no 8000 Bind port

Tests

# Unit tests (no Docker required)
poetry run pytest -m "not integration"

# Full suite (requires docker-compose SSH server)
docker compose up -d
poetry run pytest
docker compose down

Architecture

AI Agent (Claude / Cursor)
    │  stdio or HTTP SSE + Bearer token
    ▼
mcp-ssh server
    ├── safety.classify_command()   ← regex deny-list
    ├── audit.log()                 ← append-only JSONL
    └── SessionManager.get()        ← pooled SSH connections
            ├── SSHConnection.run_exec()      ← POSIX stateless
            └── SSHConnection.run_in_shell()  ← PTY stateful (ShellSession)

Key modules: mcp_ssh/server.py · mcp_ssh/session_manager.py · mcp_ssh/connection.py · mcp_ssh/safety.py · mcp_ssh/audit.py

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 模型以安全和受控的方式获取实时的网络信息。

官方
精选