使用说明
SSH MCP服务器
MCP(模型上下文协议)服务器,允许Claude通过SSH在远程服务器上执行命令。支持 ~/.ssh/config、私钥、密码认证和SSH代理转发。
工具
| 工具 | 说明 |
|---|---|
ssh_connect |
打开与远程主机的SSH连接 |
ssh_execute |
在连接的主机上运行命令 |
ssh_disconnect |
关闭连接 |
ssh_list_connections |
显示所有连接及其状态 |
设置
git clone <repo-url> && cd ssh-mcp-server
npm install
npm run build
克劳德代码
claude mcp add ssh node /absolute/path/to/ssh-mcp-server/build/index.js
克劳德桌面
增添 claude_desktop_config.json:
{
"mcpServers": {
"ssh": {
"command": "node",
"args": ["/absolute/path/to/ssh-mcp-server/build/index.js"]
}
}
}
认证
认证按以下顺序解决——第一场比赛获胜:
- 显式密码 —
password参数 - 显式私钥 —
privateKeyPathparam(支持passphrase) - SSH配置标识文件 --阅读
~/.ssh/config,按顺序尝试每个键 - SSH代理 --用途
SSH_AUTH_SOCK(未指定其他内容时默认)
使用 ~/.ssh/config
服务器读取 ~/.ssh/config 在每次connect呼叫中,编辑都会被拾取而无需重新启动。支持所有标准指令:
Host myserver
HostName 10.0.1.50
User deploy
Port 2222
IdentityFile ~/.ssh/id_ed25519
然后只需连接 host: "myserver" --别名、主机名、用户、端口和密钥都会自动解析。
工具参考
ssh-connect
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
host |
string | yes | 主机名、IP或SSH配置别名 |
username |
string | no | 覆盖SSH配置用户 |
port |
number | no | 覆盖SSH配置端口(默认22) |
password |
string | no | 密码验证 |
privateKeyPath |
string | no | 私钥路径(例如。 ~/.ssh/id_ed25519) |
passphrase |
string | no | 加密私钥的密码 |
useAgent |
boolean | no | 使用SSH代理(默认值:如果没有其他身份验证,则为true) |
connectTimeout |
number | no | TCP+握手超时(毫秒)(默认20000) |
connectionId |
string | no | 此连接的自定义ID(默认值:主机值) |
返回其他工具使用的连接ID。
ssh执行
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
connectionId |
string | yes | 来自的连接ID ssh_connect |
command |
string | yes | 要运行的Shell命令 |
cwd |
string | no | 工作目录(包装为 cd <cwd> && <command>) |
timeout |
number | no | 命令超时(毫秒)(默认30000) |
errOnNonZero |
boolean | no | 将非零退出标记为MCP错误(默认为false) |
返回stdout、stderr和退出代码。每个流的输出上限为1MB。
errOnNonZero 默认为 false 因为许多命令像 grep (不匹配=退出1)和 diff (差异=出口1)对正常结果使用非零出口。
ssh断开连接
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
connectionId |
string | yes | 要关闭的连接ID |
ssh_list_连接
没有参数。返回所有连接,包括主机、端口、用户和状态(已连接/已断开)。
破碎管道处理
- 保活数据包 每10秒发送一次,以便及早检测死连接
- 如果连接断开,
ssh_execute返回一个明确的错误,告诉您重新连接 - 一 外部超时 封装整个TCP+SSH握手,因此与防火墙/黑洞IP的连接不会永远挂起
- 所有连接都已清理干净
SIGTERM/SIGINT
示例会话
1. ssh_connect({ host: "myserver" })
→ Connected to 10.0.1.50:2222 as deploy. Connection ID: "myserver"
2. ssh_execute({ connectionId: "myserver", command: "uname -a" })
→ Linux prod-01 5.15.0 #1 SMP x86_64 GNU/Linux
→ Exit code: 0
3. ssh_execute({ connectionId: "myserver", command: "ls", cwd: "/var/log" })
→ syslog auth.log kern.log ...
→ Exit code: 0
4. ssh_list_connections()
→ - myserver: 10.0.1.50:2222 as deploy [connected]
5. ssh_disconnect({ connectionId: "myserver" })
→ Disconnected "myserver".
安全说明
- 主机密钥验证 未强制执行(TOFU——首次使用时的信任)。服务器接受任何主机密钥。
- 密码和短语作为工具参数传递,不会持久化,但可能会出现在MCP客户端日志中,具体取决于您的客户端。
- 服务器在启动它的用户的权限下运行。
许可证
麻省理工学院
