ghosthand-mcp
Provides SSH-based tools to execute commands and search, read, and write files on a remote Linux host, scoped to a configured project root.
This server does not currently have a deployable release on Glama. Follow the repository's setup instructions to install and configure it for your MCP client, including any required credentials and local dependencies.
ghosthand-mcp
轻量级、基于 stdio 传输的 AsyncSSH MCP Server,专为 Windows 本地 AI Agent 开发与运维 Linux 远端主机设计。
远端零 Agent 安装:通过标准
sshd的exec通道和 SFTP 子系统访问 Linux 主机。单连接多路复用:本地仅建立一条加密长连接并维护常驻 SFTP 通道,所有命令通过独立非交互
exec通道并发执行,绕过 WindowsWin32-OpenSSH不支持ControlMaster的痛点。并发与断线恢复:使用
asyncio.Semaphore(默认 4)限制并发exec通道数,使用asyncio.Lock保护建连与断线关闭,默认开启 30 秒 SSH keepalive;只读工具在传输断线时自动重连一次。安全基线:严格主机公钥校验(
SSH_KNOWN_HOSTS,默认~/.ssh/known_hosts)、显式禁用agent_forwarding与x11_forwarding、服务端realpath校验防止路径与符号链接越界,写入时使用lstat拒绝覆盖符号链接。
架构概览
Windows Local AI Client (Codex / Claude / Cursor / Antigravity)
│ stdio (JSON-RPC)
▼
server.py (FastMCP + SSHProject)
│ Single AsyncSSH Encrypted Connection (keepalive=30s)
├──► Persistent SFTP Channel ──► ssh_read_file / ssh_write_file (SHA-256 + atomic rename)
└──► Semaphore(4) Exec Pool ──► ssh_exec / ssh_search (rg -> git grep)
▼
Remote Linux sshd (scoped to REMOTE_PROJECT_ROOT)Related MCP server: mcp-ssh-terminal
安装与环境准备
远端非交互 SSH 环境需要支持 POSIX shell 命令和 util-linux 的 setsid --wait。超时清理使用 ps -eo pgid=,stat=、awk 和支持小数秒的 sleep。搜索需要 rg 或 Git,其中 rg 搜索子路径还需支持 NUL 分隔符的 awk 和 xargs -0 -r。
前置要求:Windows 本地 Python 3.10+。在项目根目录用 PowerShell 执行:
py -m venv .venv
.\.venv\Scripts\python.exe -m pip install -e .
# 如需运行本地测试,改用:
# .\.venv\Scripts\python.exe -m pip install -e ".[dev]"环境变量说明
变量名 | 必填 | 默认值 | 说明 |
| 是 | - | 远端 Linux 主机名或 IP |
| 是 | - | 远端 SSH 登录用户名 |
| 是 | - | 远端项目根目录(必须为 POSIX 绝对路径,如 |
| 否 |
| SSH 端口(1-65535) |
| 否 |
| OpenSSH 格式已知主机公钥文件(严格校验,自定义端口使用 |
| 否 | - | 本机私钥路径;省略时使用 AsyncSSH 默认密钥发现与本机 SSH agent |
| 否 | - | 私钥口令 |
| 否 | - | 密码认证(仅在不方便使用私钥时使用) |
| 否 |
| SSH keepalive 心跳间隔(秒) |
| 否 |
| 最大并发 |
| 否 |
|
|
| 否 |
| 回显在响应与错误详情中的目标别名 |
| 否 |
| 设为 |
| 否 |
| 服务日志写入 |
首次连接时,可在 Windows PowerShell 5.1 中扫描主机公钥并查看指纹(主机和端口请按实际配置替换):
$hostKey = ssh-keyscan -H -p 22 -t ed25519 dev.example.com
$hostKey | ssh-keygen -lf -通过可信渠道与管理员核对指纹。一致后,在同一个 PowerShell 会话中追加刚核验的公钥。已有的 known_hosts 应为 ASCII 或无 BOM 的 UTF-8 文件。
New-Item -ItemType Directory -Force "$HOME/.ssh" | Out-Null
$hostKey | Out-File -FilePath "$HOME/.ssh/known_hosts" -Append -Encoding ascii按项目动态配置(.ghosthand.env / .env)
在 Orca、Codex 等支持以当前项目目录作为工作目录(cwd)拉起 MCP 服务的客户端中,只需全局配置一次 ghosthand-mcp,即可按不同本地项目目录动态读取配置:
延迟校验:MCP 服务启动时不立即校验 SSH 配置,普通本地项目目录下也能正常完成
initialize与list_tools;仅在首次实际调用任意ssh_*工具时才读取并校验配置,成功后缓存在当前服务进程中。配置优先级:非空进程环境变量 >
.ghosthand.env>.env(.ghosthand.env中显式赋值为空的键会覆盖.env中的同名键;文件值按字面量读取,不做变量插值)。
推荐在目标项目根目录创建 .ghosthand.env(避免与项目自身的 .env 冲突),也可直接复用 .env:
SSH_HOST=dev.example.com
SSH_USER=developer
REMOTE_PROJECT_ROOT=/srv/project
SSH_KNOWN_HOSTS=C:/Users/Developer/.ssh/known_hosts安全提示:
.ghosthand.env与.env可能包含主机地址、用户名、私钥路径或口令等凭据,请务必将它们加入目标项目的.gitignore(本仓库已忽略这两个文件名)。
暴露的 4 个 MCP 工具
ssh_exec(command, cwd=".", timeout=30)在
REMOTE_PROJECT_ROOT下的相对路径cwd中以独立进程组(setsid)执行非交互命令。超时后经同一 SSH 长连接向整组发送TERM,必要时发送KILL,并检查组内是否仍有活动进程。MCP 客户端取消调用时,服务通过同一 SSH 连接尝试终止已启动的进程组。连接中断或终止通道不可用时,远端进程可能继续运行。
返回
exit_code、stdout、stderr、stdout_truncated、stderr_truncated、duration_ms、timed_out和process_state(exited/terminated/still_running/unknown)。超时响应附带error_code: "TIMEOUT"及已捕获的stdout/stderr。stdout和stderr分别限制字符数(默认每流 20,000),超长时保留开头约 40% 与结尾约 60%,中间提示省略字符数。
ssh_search(pattern, sub_path=".", max_results=50, include_hidden=False)按字面量搜索。优先使用
rg,缺少时使用git grep。默认跳过隐藏文件和目录,并遵循.gitignore规则;include_hidden=True允许搜索隐藏文件与目录(仍遵循忽略规则)。无匹配时返回exit_code=1和空的matches。matches为结构化对象列表[{"path", "line", "text"}],其中path为相对于REMOTE_PROJECT_ROOT的 POSIX 路径(文件名含冒号也能准确解析),可直接传给ssh_read_file。
ssh_read_file(rel_path, start_line=1, end_line=200)校验路径后,通过 SFTP 分块扫描全文件,返回原始字节的
sha256、全文件行数total_lines和从 1 开始的闭区间内的正文content。保留 LF、CRLF、CR,非法 UTF-8 以替换字符显示。正文超过
SSH_MAX_OUTPUT_CHARS时仅返回解码后的前缀,并设content_truncated=true。换行符计入上限,因此正文可能在行中或 CRLF 之间结束。恰好达到上限或所选范围为空时,该字段为false。
ssh_write_file(rel_path, content, expected_sha256=None, create_only=False)写入前可选比对
expected_sha256防止并发覆盖(服务内串行化写入校验)。create_only=True仅在目标不存在时创建(与expected_sha256互斥),优先通过 SFTP 硬链接原子发布(不支持时回退rename),并发创建同一路径仅一个请求成功。自动按需创建项目内缺失的父目录,先写入同目录
0o600临时文件并尽量保留原文件权限,再原子替换目标文件。
四个工具均回显 request_id、target 以及解析后的 cwd / path;默认在 structuredContent 返回完整结构化结果、在 content 返回简短摘要(设 SSH_MCP_COMPAT_FULL_CONTENT=1 可在 content 保留完整 JSON)。
错误返回结构化字段 error_code、message 和 details:
错误码 | 含义 |
| 参数格式、取值或组合无效 |
| 路径超出项目根目录 |
| 所需文件或目录不存在 |
| SSH 配置缺失或无效 |
| SSH 连接、认证或主机密钥校验失败 |
| 文件已变化或不存在, |
|
|
| SSH 操作超时 |
| 服务端不支持所需的原子写入操作 |
| 无法启动隔离的远端进程组 |
| 远端找不到 |
| 远端文件访问被拒绝或发生 I/O 错误 |
客户端即用配置
将示例中的项目路径、SSH 主机、用户和 REMOTE_PROJECT_ROOT 替换为实际值:
1. 标准 MCP 客户端(mcpServers JSON)
{
"mcpServers": {
"ghosthand-mcp": {
"command": "D:/ghosthand-mcp/.venv/Scripts/python.exe",
"args": ["D:/ghosthand-mcp/server.py"],
"env": {
"SSH_HOST": "dev.example.com",
"SSH_USER": "developer",
"SSH_KNOWN_HOSTS": "C:/Users/Developer/.ssh/known_hosts",
"REMOTE_PROJECT_ROOT": "/srv/project"
}
}
}
}2. Codex CLI(~/.codex/config.toml)
[mcp_servers.ghosthand_mcp]
command = "D:/ghosthand-mcp/.venv/Scripts/python.exe"
args = ["D:/ghosthand-mcp/server.py"]
[mcp_servers.ghosthand_mcp.env]
SSH_HOST = "dev.example.com"
SSH_USER = "developer"
SSH_KNOWN_HOSTS = "C:/Users/Developer/.ssh/known_hosts"
REMOTE_PROJECT_ROOT = "/srv/project"本地校验与测试
.\.venv\Scripts\python.exe -m compileall -q server.py tests
.\.venv\Scripts\python.exe -c "import asyncssh, server; from mcp.server.fastmcp import FastMCP"
.\.venv\Scripts\python.exe -m pytest -q许可证
Tool Schema Changelog
Recent tool additions, removals, and schema changes observed during successful MCP inspections.
Loading tool schema changelog…
This server cannot be deployed
Maintenance
Related MCP Connectors
Remote MCP access to user-authorized Linux, Windows and Intel Mac devices for AI assistants.
Connect AI assistants to your Mac and Linux computers with scoped access to local files and tools.
- DNS-DATA RemoteDeprecatedcom.dns-data
Connect AI assistants to authorized Windows and macOS devices for files, commands, and management.
Use your own Windows, Mac or Linux computer from ChatGPT, Claude, Cursor or Gemini CLI: run commands, read, write and search files, run long processes. Your computer only makes an outgoing connection (no open ports or VPN). Read-only mode, allowed folders, blocked commands and an action log keep you in control. Free plan available.
Related MCP Servers
- AlicenseAqualityDmaintenanceEnables AI assistants to securely execute remote SSH commands, perform file transfers, and monitor system status through a standardized interface. It features robust security controls including command whitelisting, blacklisting, and credential isolation to prevent unauthorized operations.1020 npmMIT
- AlicenseNot gradedqualityDmaintenanceEnables AI agents to have persistent, fully interactive SSH sessions into remote hosts, behaving like a local terminal.19 npm2MIT
- AlicenseNot gradedqualityBmaintenanceEnables AI agents to securely execute commands on remote hosts via SSH and SFTP, with persistent shells, file transfers, screenshots, and an audit log.5MIT
- FlicenseNot gradedqualityBmaintenanceEnables AI-controlled administration of remote Linux hosts and containers, including bounded shell execution, persistent PTY sessions, durable jobs/tasks, hardened filesystem access, process/service control, and resumable transfers.-