Repository files navigation

ssh-mcp-server logo

ssh-mcp-server

NPM VersionGitHub forksGitHub Repo starsGitHub Issues or Pull RequestsGitHub Issues or Pull RequestsGitHub Issues or Pull RequestsGitHub Issues or Pull Requests

基于 SSH 的 MCP (Model Context Protocol) 服务器,允许通过 MCP 协议远程执行 SSH 命令。

English Document | 中文文档

📝 项目介绍

ssh-mcp-server 是一个桥接工具,可以让 AI 助手等支持 MCP 协议的应用通过标准化接口执行远程 SSH 命令。这使得 AI 助手能够安全地操作远程服务器,执行命令并获取结果,而无需直接暴露 SSH 凭据给 AI 模型。

💬 如有任何问题,欢迎加入微信群交流:

wechat

✨ 功能亮点

  • 🔒 安全连接:支持多种安全的 SSH 连接方式,包括密码认证和私钥认证(支持带密码的私钥)
  • 🛡️ 命令安全控制:通过灵活的黑白名单机制,精确控制允许执行的命令范围,防止危险操作
  • 🔄 标准化接口:符合 MCP 协议规范,与支持该协议的 AI 助手无缝集成
  • 🚇 双传输模式:同时支持 execshell 两种 transport,兼容直连主机与堡垒机或跳板机场景
  • 📂 文件传输:支持双向文件传输功能,可上传本地文件到服务器或从服务器下载文件
  • 🔑 凭据隔离:SSH 凭据完全在本地管理,不会暴露给 AI 模型,增强安全性
  • 🚀 即用即走:使用 NPX 可直接运行,无需全局安装,方便快捷

📦 开源仓库

GitHub:https://github.com/classfang/ssh-mcp-server

NPM: https://www.npmjs.com/package/@fangjunjie/ssh-mcp-server

🛠️ 工具列表

工具名称描述
execute-command命令执行工具在远程服务器上执行 SSH 命令并获取执行结果
upload文件上传工具将本地文件上传到远程服务器指定位置
download文件下载工具从远程服务器下载文件到本地指定位置
list-servers服务器列表工具列出所有可用SSH服务器配置

📚 使用方法

0. 🤖 通过 AI Skill 快速配置(推荐)

如果你使用支持 skill 的 AI 编程助手(如 Claude Code),可以直接使用内置的 ssh-mcp-helper skill 通过交互式问答完成安装和配置,无需手动编辑 JSON 文件。

使用方式:

  1. 从本仓库 skills/ 目录安装该 skill
  2. 告诉你的 AI 助手:"帮我配置 ssh-mcp-server" 或 "给 Cursor 加一个 SSH MCP 连接"
  3. skill 会逐步引导你:检查 Node.js 环境 → 选择 MCP 客户端 → 选择认证方式 → 收集连接参数 → 生成并写入配置

该 skill 支持下文所有场景(账号密码、私钥、SSH config 复用、SOCKS 代理、堡垒机、多连接、2FA、命令限制等),并自动生成格式正确的配置。


下面的章节按从简单到复杂的顺序排列,最简单的入门方式就是用账号密码连接服务器。直接复制对应场景下的 mcp.json 配置到你的 MCP 客户端即可使用。

⚠️ 重要提示:在 MCP 配置文件中,每个命令行参数和其值必须是 args 数组中的独立元素。不要用空格将它们连接在一起。例如,使用 "--host", "192.168.1.1" 而不是 "--host 192.168.1.1"

1. 🔑 账号密码(最简单)

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "192.168.1.1",
"--port", "22",
"--username", "root",
"--password", "pwd123456"
]
}
}
}

2. 🔐 账号 + 私钥

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "192.168.1.1",
"--port", "22",
"--username", "root",
"--privateKey", "~/.ssh/id_rsa"
]
}
}
}

3. 🔏 带密码的私钥

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "192.168.1.1",
"--port", "22",
"--username", "root",
"--privateKey", "~/.ssh/id_rsa",
"--passphrase", "pwd123456"
]
}
}
}

4. 📋 复用 ~/.ssh/config

如果你已经在 ~/.ssh/config 配置了主机别名,服务器会自动从中读取连接参数,mcp.json 里就不用再写一遍。

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "myserver"
]
}
}
}

假设你的 ~/.ssh/config 包含:

Host myserver
HostName 192.168.1.1
Port 22
User root
IdentityFile ~/.ssh/id_rsa

你也可以指定自定义的 SSH 配置文件路径:

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "myserver",
"--ssh-config-file", "/path/to/custom/ssh_config"
]
}
}
}

注意:命令行参数优先级高于 SSH 配置值。例如,如果你指定了 --port 2222,它会覆盖 SSH 配置中的端口。

5. 🌐 通过代理连接

当目标主机只能通过代理访问时,可使用 --proxy 配置 SOCKS5、HTTP 或 HTTPS 代理。

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "192.168.1.1",
"--port", "22",
"--username", "root",
"--password", "pwd123456",
"--proxy", "http://username:password@proxy-host:proxy-port"
]
}
}
}

支持的 URL 格式:

socks://username:password@proxy-host:1080
socks5://username:password@proxy-host:1080
http://username:password@proxy-host:8080
https://username:password@proxy-host:8443

HTTP 和 HTTPS 代理通过 CONNECT 方法建立到 SSH 服务的隧道,用户名和密码使用 Basic 代理认证。HTTP、HTTPS 未填写端口时分别默认使用 80443;SOCKS5 必须填写端口。HTTPS 代理证书使用 Node.js 默认信任链进行验证。

原有 socksProxy 配置和 --socksProxy 参数继续兼容,但只接受 socks://socks5://。不要同时配置 proxysocksProxy

6. 📝 使用命令白名单 / 黑名单

通过 --whitelist--blacklist 限制服务器允许执行的命令范围。多个模式之间用逗号分隔,每个模式都是一个正则表达式。生产环境强烈建议配置

白名单示例(仅允许只读型查看命令):

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "192.168.1.1",
"--port", "22",
"--username", "root",
"--password", "pwd123456",
"--whitelist", "^ls( .*)?,^cat .*,^df.*"
]
}
}
}

黑名单示例(屏蔽危险命令):

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "192.168.1.1",
"--port", "22",
"--username", "root",
"--password", "pwd123456",
"--blacklist", "^rm .*,^shutdown.*,^reboot.*"
]
}
}
}

注意:如果同时指定了白名单和黑名单,系统会先检查命令是否在白名单中,再检查是否在黑名单中,命令必须同时通过两项检查才能被执行。

7. 🧩 使用命令模板包裹命令

commandTemplate 会把每条执行的命令套进一个模板里,适合切换用户(su)、放进容器、或经过跳板机的场景。当命令会作为 shell 参数传入时使用 <quotedCommand>,需要原样插入时使用 <command>;模板会在目录 cd 拼接之后应用,因此整个 cd ... && <实际命令> 都会被包裹起来。

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "10.0.0.1",
"--port", "22",
"--username", "deploy",
"--password", "xxx",
"--command-template", "su root -c <quotedCommand>"
]
}
}
}

当指定目录为 /data 执行 ls /app 时,实际发送的命令是:

su root -c 'cd -- '\''/data'\'' && ls /app'

其他常见模板:

sudo bash -c <quotedCommand>
docker exec -i mycontainer sh -c <quotedCommand>
ssh jumphost <quotedCommand>

8. 🚇 堡垒机 / 跳板机(transportMode: shell

transportMode 默认是 exec。出现下面这些情况时,应该切换到 shell

  • SSH 登录成功,但 exec 执行命令失败
  • 远端必须等登录 banner、profile、环境初始化完成后才能正常执行命令
  • 连接目标本质上是堡垒机或只暴露交互式 shell 的设备

两者差异:

  • exec:支持 execute-commanduploaddownload
  • shell:命令通过持久 shell 会话串行执行,内部带命令队列;但不支持upload / download,因为该模式下禁用了 SFTP
{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "bastion.example.com",
"--port", "22",
"--username", "ops",
"--password", "pwd123456",
"--transport-mode", "shell",
"--shell-ready-timeout", "15000"
]
}
}
}

JSON 配置文件中还可以通过 shellCommandTimeoutMs 覆盖 shell 模式下单条命令的默认超时。

9. 🔐 多因素认证(2FA / MFA)

当 SSH 服务器要求多因素认证(密码 + 私钥 + 2FA 验证码)时启用 tryKeyboard。密码和私钥会自动提供;对于非密码提示,请在连接前通过服务端环境变量 SSH_MCP_2FA_CODE 提供验证码。

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "example.com",
"--port", "22",
"--username", "user",
"--password", "your_password",
"--privateKey", "/path/to/key",
"--try-keyboard"
]
}
}
}

认证流程:

  1. 私钥认证(如果提供)
  2. 密码认证(如果提供)
  3. 键盘交互式认证通过 SSH_MCP_2FA_CODE 提供 2FA 验证码

10. 🧩 多 SSH 连接配置

需要在同一个 MCP server 里同时管理多个 SSH 目标时,给每个连接命名,调用时通过 connectionName 选择。共有三种配置方式:

📄 方式一:使用配置文件(推荐)

创建 JSON 配置文件(例如 ssh-config.json):

数组格式:

[
{
"name": "dev",
"host": "1.2.3.4",
"port": 22,
"username": "alice",
"password": "{abc=P100s0}",
"socksProxy": "socks://127.0.0.1:10808",
"commandTimeoutMs": 120000,
"maxOutputBytes": 10485760
},
{
"name": "bastion",
"host": "9.9.9.9",
"port": 22,
"username": "ops",
"password": "pwd123456",
"transportMode": "shell",
"shellReadyTimeoutMs": 15000,
"shellCommandTimeoutMs": 45000,
"connectionTimeoutMs": 30000,
"keepaliveIntervalMs": 10000,
"keepaliveCountMax": 3
},
{
"name": "prod",
"host": "5.6.7.8",
"port": 22,
"username": "bob",
"password": "yyy",
"socksProxy": "socks://127.0.0.1:10808"
},
{
"name": "secure-server",
"host": "secure.example.com",
"port": 22,
"username": "admin",
"password": "your_password",
"privateKey": "/path/to/private/key",
"tryKeyboard": true
}
]

对象格式:

{
"dev": {
"host": "1.2.3.4",
"port": 22,
"username": "alice",
"password": "{abc=P100s0}",
"socksProxy": "socks://127.0.0.1:10808",
"commandTimeoutMs": 120000,
"maxOutputBytes": 10485760
},
"bastion": {
"host": "9.9.9.9",
"port": 22,
"username": "ops",
"password": "pwd123456",
"transportMode": "shell",
"shellReadyTimeoutMs": 15000,
"shellCommandTimeoutMs": 45000
},
"prod": {
"host": "5.6.7.8",
"port": 22,
"username": "bob",
"password": "yyy",
"socksProxy": "socks://127.0.0.1:10808"
}
}

然后使用 --config-file 参数:

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--config-file", "ssh-config.json"
]
}
}
}

🔧 方式二:使用 JSON 格式的 --ssh 参数

可以直接传递 JSON 格式的配置字符串:

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--ssh", "{\"name\":\"dev\",\"host\":\"1.2.3.4\",\"port\":22,\"username\":\"alice\",\"password\":\"{abc=P100s0}\",\"socksProxy\":\"socks://127.0.0.1:10808\"}",
"--ssh", "{\"name\":\"bastion\",\"host\":\"9.9.9.9\",\"port\":22,\"username\":\"ops\",\"password\":\"pwd123456\",\"transportMode\":\"shell\",\"shellReadyTimeoutMs\":15000}",
"--ssh", "{\"name\":\"prod\",\"host\":\"5.6.7.8\",\"port\":22,\"username\":\"bob\",\"password\":\"yyy\",\"socksProxy\":\"socks://127.0.0.1:10808\"}"
]
}
}
}

📝 方式三:旧格式逗号分隔(向后兼容)

对于密码中不包含特殊字符的简单情况,仍可使用旧格式:

npx @fangjunjie/ssh-mcp-server \
--ssh "name=dev,host=1.2.3.4,port=22,user=alice,password=xxx" \
--ssh "name=prod,host=5.6.7.8,port=22,user=bob,password=yyy"

⚠️ 注意:旧格式在处理包含特殊字符(如 =,{})的密码时可能会有问题。如果密码包含特殊字符,请使用方式一或方式二。

在MCP工具调用时,通过 connectionName 参数指定目标连接名称,未指定时使用默认连接。

示例(在prod连接上执行命令):

{
"tool": "execute-command",
"params": {
"cmdString": "ls -al",
"connectionName": "prod"
}
}

示例(带超时选项的命令执行):

{
"tool": "execute-command",
"params": {
"cmdString": "ping -c 10 127.0.0.1",
"connectionName": "prod",
"timeout": 5000
}
}

⏱️ 命令执行超时

execute-command 工具支持超时选项,防止命令无限期挂起:

  • timeout: 单次调用的命令执行超时时间(毫秒,可选);传入时会覆盖连接配置,未传入时使用对应连接配置或其 30000ms 默认值
  • 在 JSON 配置文件里为单个连接设置 commandTimeoutMs,可以改掉这个默认值,避免每次调用都手动传 timeoutexec 模式)
  • shell 模式对应的配置项是 shellCommandTimeoutMs
  • 调用参数里的 timeout 始终优先于上面两个配置项
  • 连接默认启用 SSH keepalive(keepaliveIntervalMs: 10000,keepaliveCountMax: 3),并使用 connectionTimeoutMs 限制连接建立时间
  • SFTP 打开和传输操作使用 sftpTimeoutMs 控制超时(默认 300000ms)
  • 错误响应现在包含稳定的 codemessageretriable 字段,便于上层 Agent 处理

这对于像 pingtail -f 或其他可能阻塞执行的长时间运行进程特别有用。

📦 命令输出限制

会限制单条命令捕获的 stdoutstderr 总量,避免大文件或无限输出耗尽 MCP server 内存:

  • 在 JSON 连接配置中使用 maxOutputBytes 设置上限,默认值为 10485760(10 MiB)
  • maxOutputBytes 必须是非负整数;设置为 0 可禁用限制,但不建议对不受信任的命令禁用
  • 输出超过限制时,远端命令会被中止,工具返回 OUTPUT_LIMIT_EXCEEDED 错误和已经捕获的截断输出,不会把中止的命令误报为成功
  • ptyfalse 时,成功命令写入 stderr 的警告或进度信息会保留在 [stderr] 区段中
  • execshell 两种模式都会应用该限制。区别在于 exec 模式只关闭该命令的通道,而 shell 模式的通道由该连接上的所有命令共用、远端在中止后仍会继续写入,因此会断开连接(与 shell 模式命令超时的处理一致)

🗂️ 列出所有SSH服务器

可以通过MCP工具 list-servers 获取所有可用的SSH服务器配置:

调用示例:

{
"tool": "list-servers",
"params": {}
}

返回示例:

[
{ "name": "dev", "host": "1.2.3.4", "port": 22, "username": "alice" },
{ "name": "prod", "host": "5.6.7.8", "port": 22, "username": "bob" }
]

⚙️ 命令行选项参考

选项:
--config-file JSON 配置文件路径(推荐用于多服务器配置)
--ssh-config-file SSH 配置文件路径(默认: ~/.ssh/config)
--ssh SSH 连接配置(可以是 JSON 字符串或旧格式)
-h, --host SSH 服务器主机地址或 SSH 配置中的别名
-p, --port SSH 服务器端口
-u, --username SSH 用户名
-w, --password SSH 密码
-k, --privateKey SSH 私钥文件路径
-P, --passphrase 私钥密码(如果有的话)
-a, --agent SSH agent socket 路径
--try-keyboard 启用键盘交互式认证以支持 2FA/MFA(默认: false)
-W, --whitelist 命令白名单,以逗号分隔的正则表达式
-B, --blacklist 命令黑名单,以逗号分隔的正则表达式
--proxy 代理地址,支持 SOCKS5、HTTP 和 HTTPS
-s, --socksProxy 旧版 SOCKS5 代理地址(兼容参数)
--allowed-local-paths upload/download 允许访问的额外本地路径,逗号分隔
--allowed-remote-paths SFTP upload/download 允许访问的远端路径(POSIX 绝对路径),逗号分隔
--transport-mode SSH transport 模式: exec 或 shell(默认: exec)
--shell-ready-timeout shell 就绪探测超时,单位毫秒(默认: 10000)
--command-template 命令模板;shell 参数用 <quotedCommand>,原样插入用 <command>
--pty 为命令执行分配伪终端(默认: true)
--pre-connect 启动时预连接所有配置的 SSH 服务器
--version, -v 打印包版本
--help 打印帮助信息

🛡️ 安全注意事项

该服务器提供了在远程服务器上执行命令和传输文件的强大功能。为确保安全使用,请注意以下几点:

  • 命令白名单强烈建议 使用 --whitelist 选项来限制可执行的命令集合。如果没有白名单,任何命令都可以在远程服务器上执行,这可能带来重大的安全风险。
  • 私钥安全:服务器会将 SSH 私钥读入内存。请确保运行 ssh-mcp-server 的机器是安全的。不要将服务器暴露给不受信任的网络。
  • 拒绝服务攻击 (DoS):服务器没有内置的速率限制。攻击者可能通过向服务器发送大量连接请求或大文件传输来发起 DoS 攻击。建议在具有速率限制功能的防火墙或反向代理后面运行服务器。
  • 路径遍历:服务器内置了对本地文件系统路径遍历攻击的保护。但是,仍然需要注意在 uploaddownload 命令中使用的路径。
  • 本地传输范围:默认仅允许访问当前工作目录。只有在明确可信时,才建议通过 --allowed-local-paths 或配置文件中的 allowedLocalPaths 放宽范围。
  • 远端传输范围:SFTP upload/download 仅接受绝对 POSIX 路径。未配置 allowedRemotePaths(或 --allowed-remote-paths)时,任意远端路径都允许,但启动时会打印警告。强烈建议显式配置 allowedRemotePaths 白名单,避免模型被 prompt 注入后读写 ~/.ssh/authorized_keys/etc/sshd_config 之类敏感文件。

🌟 Star 历史

Star History

Star History Chart

About

基于 SSH 的 MCP 服务 🧙‍♀️。已被MCP官方收录 🎉。 SSH MCP Server 🧙‍♀️. It has been included in the community MCP repository 🎉.

Resources

Stars

822 stars

Watchers

3 watching

Forks

Releases

Packages

Used by

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all
 blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks");
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content

Repository files navigation

ssh-mcp-server logo

ssh-mcp-server

NPM VersionGitHub forksGitHub Repo starsGitHub Issues or Pull RequestsGitHub Issues or Pull RequestsGitHub Issues or Pull RequestsGitHub Issues or Pull Requests

基于 SSH 的 MCP (Model Context Protocol) 服务器,允许通过 MCP 协议远程执行 SSH 命令。

English Document | 中文文档

📝 项目介绍

ssh-mcp-server 是一个桥接工具,可以让 AI 助手等支持 MCP 协议的应用通过标准化接口执行远程 SSH 命令。这使得 AI 助手能够安全地操作远程服务器,执行命令并获取结果,而无需直接暴露 SSH 凭据给 AI 模型。

💬 如有任何问题,欢迎加入微信群交流:

wechat

✨ 功能亮点

  • 🔒 安全连接:支持多种安全的 SSH 连接方式,包括密码认证和私钥认证(支持带密码的私钥)
  • 🛡️ 命令安全控制:通过灵活的黑白名单机制,精确控制允许执行的命令范围,防止危险操作
  • 🔄 标准化接口:符合 MCP 协议规范,与支持该协议的 AI 助手无缝集成
  • 🚇 双传输模式:同时支持 execshell 两种 transport,兼容直连主机与堡垒机或跳板机场景
  • 📂 文件传输:支持双向文件传输功能,可上传本地文件到服务器或从服务器下载文件
  • 🔑 凭据隔离:SSH 凭据完全在本地管理,不会暴露给 AI 模型,增强安全性
  • 🚀 即用即走:使用 NPX 可直接运行,无需全局安装,方便快捷

📦 开源仓库

GitHub:https://github.com/classfang/ssh-mcp-server

NPM: https://www.npmjs.com/package/@fangjunjie/ssh-mcp-server

🛠️ 工具列表

工具名称描述
execute-command命令执行工具在远程服务器上执行 SSH 命令并获取执行结果
upload文件上传工具将本地文件上传到远程服务器指定位置
download文件下载工具从远程服务器下载文件到本地指定位置
list-servers服务器列表工具列出所有可用SSH服务器配置

📚 使用方法

0. 🤖 通过 AI Skill 快速配置(推荐)

如果你使用支持 skill 的 AI 编程助手(如 Claude Code),可以直接使用内置的 ssh-mcp-helper skill 通过交互式问答完成安装和配置,无需手动编辑 JSON 文件。

使用方式:

  1. 从本仓库 skills/ 目录安装该 skill
  2. 告诉你的 AI 助手:"帮我配置 ssh-mcp-server" 或 "给 Cursor 加一个 SSH MCP 连接"
  3. skill 会逐步引导你:检查 Node.js 环境 → 选择 MCP 客户端 → 选择认证方式 → 收集连接参数 → 生成并写入配置

该 skill 支持下文所有场景(账号密码、私钥、SSH config 复用、SOCKS 代理、堡垒机、多连接、2FA、命令限制等),并自动生成格式正确的配置。


下面的章节按从简单到复杂的顺序排列,最简单的入门方式就是用账号密码连接服务器。直接复制对应场景下的 mcp.json 配置到你的 MCP 客户端即可使用。

⚠️ 重要提示:在 MCP 配置文件中,每个命令行参数和其值必须是 args 数组中的独立元素。不要用空格将它们连接在一起。例如,使用 "--host", "192.168.1.1" 而不是 "--host 192.168.1.1"

1. 🔑 账号密码(最简单)

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "192.168.1.1",
"--port", "22",
"--username", "root",
"--password", "pwd123456"
]
}
}
}

2. 🔐 账号 + 私钥

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "192.168.1.1",
"--port", "22",
"--username", "root",
"--privateKey", "~/.ssh/id_rsa"
]
}
}
}

3. 🔏 带密码的私钥

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "192.168.1.1",
"--port", "22",
"--username", "root",
"--privateKey", "~/.ssh/id_rsa",
"--passphrase", "pwd123456"
]
}
}
}

4. 📋 复用 ~/.ssh/config

如果你已经在 ~/.ssh/config 配置了主机别名,服务器会自动从中读取连接参数,mcp.json 里就不用再写一遍。

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "myserver"
]
}
}
}

假设你的 ~/.ssh/config 包含:

Host myserver
HostName 192.168.1.1
Port 22
User root
IdentityFile ~/.ssh/id_rsa

你也可以指定自定义的 SSH 配置文件路径:

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "myserver",
"--ssh-config-file", "/path/to/custom/ssh_config"
]
}
}
}

注意:命令行参数优先级高于 SSH 配置值。例如,如果你指定了 --port 2222,它会覆盖 SSH 配置中的端口。

5. 🌐 通过代理连接

当目标主机只能通过代理访问时,可使用 --proxy 配置 SOCKS5、HTTP 或 HTTPS 代理。

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "192.168.1.1",
"--port", "22",
"--username", "root",
"--password", "pwd123456",
"--proxy", "http://username:password@proxy-host:proxy-port"
]
}
}
}

支持的 URL 格式:

socks://username:password@proxy-host:1080
socks5://username:password@proxy-host:1080
http://username:password@proxy-host:8080
https://username:password@proxy-host:8443

HTTP 和 HTTPS 代理通过 CONNECT 方法建立到 SSH 服务的隧道,用户名和密码使用 Basic 代理认证。HTTP、HTTPS 未填写端口时分别默认使用 80443;SOCKS5 必须填写端口。HTTPS 代理证书使用 Node.js 默认信任链进行验证。

原有 socksProxy 配置和 --socksProxy 参数继续兼容,但只接受 socks://socks5://。不要同时配置 proxysocksProxy

6. 📝 使用命令白名单 / 黑名单

通过 --whitelist--blacklist 限制服务器允许执行的命令范围。多个模式之间用逗号分隔,每个模式都是一个正则表达式。生产环境强烈建议配置

白名单示例(仅允许只读型查看命令):

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "192.168.1.1",
"--port", "22",
"--username", "root",
"--password", "pwd123456",
"--whitelist", "^ls( .*)?,^cat .*,^df.*"
]
}
}
}

黑名单示例(屏蔽危险命令):

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "192.168.1.1",
"--port", "22",
"--username", "root",
"--password", "pwd123456",
"--blacklist", "^rm .*,^shutdown.*,^reboot.*"
]
}
}
}

注意:如果同时指定了白名单和黑名单,系统会先检查命令是否在白名单中,再检查是否在黑名单中,命令必须同时通过两项检查才能被执行。

7. 🧩 使用命令模板包裹命令

commandTemplate 会把每条执行的命令套进一个模板里,适合切换用户(su)、放进容器、或经过跳板机的场景。当命令会作为 shell 参数传入时使用 <quotedCommand>,需要原样插入时使用 <command>;模板会在目录 cd 拼接之后应用,因此整个 cd ... && <实际命令> 都会被包裹起来。

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "10.0.0.1",
"--port", "22",
"--username", "deploy",
"--password", "xxx",
"--command-template", "su root -c <quotedCommand>"
]
}
}
}

当指定目录为 /data 执行 ls /app 时,实际发送的命令是:

su root -c 'cd -- '\''/data'\'' && ls /app'

其他常见模板:

sudo bash -c <quotedCommand>
docker exec -i mycontainer sh -c <quotedCommand>
ssh jumphost <quotedCommand>

8. 🚇 堡垒机 / 跳板机(transportMode: shell

transportMode 默认是 exec。出现下面这些情况时,应该切换到 shell

  • SSH 登录成功,但 exec 执行命令失败
  • 远端必须等登录 banner、profile、环境初始化完成后才能正常执行命令
  • 连接目标本质上是堡垒机或只暴露交互式 shell 的设备

两者差异:

  • exec:支持 execute-commanduploaddownload
  • shell:命令通过持久 shell 会话串行执行,内部带命令队列;但不支持upload / download,因为该模式下禁用了 SFTP
{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "bastion.example.com",
"--port", "22",
"--username", "ops",
"--password", "pwd123456",
"--transport-mode", "shell",
"--shell-ready-timeout", "15000"
]
}
}
}

JSON 配置文件中还可以通过 shellCommandTimeoutMs 覆盖 shell 模式下单条命令的默认超时。

9. 🔐 多因素认证(2FA / MFA)

当 SSH 服务器要求多因素认证(密码 + 私钥 + 2FA 验证码)时启用 tryKeyboard。密码和私钥会自动提供;对于非密码提示,请在连接前通过服务端环境变量 SSH_MCP_2FA_CODE 提供验证码。

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "example.com",
"--port", "22",
"--username", "user",
"--password", "your_password",
"--privateKey", "/path/to/key",
"--try-keyboard"
]
}
}
}

认证流程:

  1. 私钥认证(如果提供)
  2. 密码认证(如果提供)
  3. 键盘交互式认证通过 SSH_MCP_2FA_CODE 提供 2FA 验证码

10. 🧩 多 SSH 连接配置

需要在同一个 MCP server 里同时管理多个 SSH 目标时,给每个连接命名,调用时通过 connectionName 选择。共有三种配置方式:

📄 方式一:使用配置文件(推荐)

创建 JSON 配置文件(例如 ssh-config.json):

数组格式:

[
{
"name": "dev",
"host": "1.2.3.4",
"port": 22,
"username": "alice",
"password": "{abc=P100s0}",
"socksProxy": "socks://127.0.0.1:10808",
"commandTimeoutMs": 120000,
"maxOutputBytes": 10485760
},
{
"name": "bastion",
"host": "9.9.9.9",
"port": 22,
"username": "ops",
"password": "pwd123456",
"transportMode": "shell",
"shellReadyTimeoutMs": 15000,
"shellCommandTimeoutMs": 45000,
"connectionTimeoutMs": 30000,
"keepaliveIntervalMs": 10000,
"keepaliveCountMax": 3
},
{
"name": "prod",
"host": "5.6.7.8",
"port": 22,
"username": "bob",
"password": "yyy",
"socksProxy": "socks://127.0.0.1:10808"
},
{
"name": "secure-server",
"host": "secure.example.com",
"port": 22,
"username": "admin",
"password": "your_password",
"privateKey": "/path/to/private/key",
"tryKeyboard": true
}
]

对象格式:

{
"dev": {
"host": "1.2.3.4",
"port": 22,
"username": "alice",
"password": "{abc=P100s0}",
"socksProxy": "socks://127.0.0.1:10808",
"commandTimeoutMs": 120000,
"maxOutputBytes": 10485760
},
"bastion": {
"host": "9.9.9.9",
"port": 22,
"username": "ops",
"password": "pwd123456",
"transportMode": "shell",
"shellReadyTimeoutMs": 15000,
"shellCommandTimeoutMs": 45000
},
"prod": {
"host": "5.6.7.8",
"port": 22,
"username": "bob",
"password": "yyy",
"socksProxy": "socks://127.0.0.1:10808"
}
}

然后使用 --config-file 参数:

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--config-file", "ssh-config.json"
]
}
}
}

🔧 方式二:使用 JSON 格式的 --ssh 参数

可以直接传递 JSON 格式的配置字符串:

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--ssh", "{\"name\":\"dev\",\"host\":\"1.2.3.4\",\"port\":22,\"username\":\"alice\",\"password\":\"{abc=P100s0}\",\"socksProxy\":\"socks://127.0.0.1:10808\"}",
"--ssh", "{\"name\":\"bastion\",\"host\":\"9.9.9.9\",\"port\":22,\"username\":\"ops\",\"password\":\"pwd123456\",\"transportMode\":\"shell\",\"shellReadyTimeoutMs\":15000}",
"--ssh", "{\"name\":\"prod\",\"host\":\"5.6.7.8\",\"port\":22,\"username\":\"bob\",\"password\":\"yyy\",\"socksProxy\":\"socks://127.0.0.1:10808\"}"
]
}
}
}

📝 方式三:旧格式逗号分隔(向后兼容)

对于密码中不包含特殊字符的简单情况,仍可使用旧格式:

npx @fangjunjie/ssh-mcp-server \
--ssh "name=dev,host=1.2.3.4,port=22,user=alice,password=xxx" \
--ssh "name=prod,host=5.6.7.8,port=22,user=bob,password=yyy"

⚠️ 注意:旧格式在处理包含特殊字符(如 =,{})的密码时可能会有问题。如果密码包含特殊字符,请使用方式一或方式二。

在MCP工具调用时,通过 connectionName 参数指定目标连接名称,未指定时使用默认连接。

示例(在prod连接上执行命令):

{
"tool": "execute-command",
"params": {
"cmdString": "ls -al",
"connectionName": "prod"
}
}

示例(带超时选项的命令执行):

{
"tool": "execute-command",
"params": {
"cmdString": "ping -c 10 127.0.0.1",
"connectionName": "prod",
"timeout": 5000
}
}

⏱️ 命令执行超时

execute-command 工具支持超时选项,防止命令无限期挂起:

  • timeout: 单次调用的命令执行超时时间(毫秒,可选);传入时会覆盖连接配置,未传入时使用对应连接配置或其 30000ms 默认值
  • 在 JSON 配置文件里为单个连接设置 commandTimeoutMs,可以改掉这个默认值,避免每次调用都手动传 timeoutexec 模式)
  • shell 模式对应的配置项是 shellCommandTimeoutMs
  • 调用参数里的 timeout 始终优先于上面两个配置项
  • 连接默认启用 SSH keepalive(keepaliveIntervalMs: 10000,keepaliveCountMax: 3),并使用 connectionTimeoutMs 限制连接建立时间
  • SFTP 打开和传输操作使用 sftpTimeoutMs 控制超时(默认 300000ms)
  • 错误响应现在包含稳定的 codemessageretriable 字段,便于上层 Agent 处理

这对于像 pingtail -f 或其他可能阻塞执行的长时间运行进程特别有用。

📦 命令输出限制

会限制单条命令捕获的 stdoutstderr 总量,避免大文件或无限输出耗尽 MCP server 内存:

  • 在 JSON 连接配置中使用 maxOutputBytes 设置上限,默认值为 10485760(10 MiB)
  • maxOutputBytes 必须是非负整数;设置为 0 可禁用限制,但不建议对不受信任的命令禁用
  • 输出超过限制时,远端命令会被中止,工具返回 OUTPUT_LIMIT_EXCEEDED 错误和已经捕获的截断输出,不会把中止的命令误报为成功
  • ptyfalse 时,成功命令写入 stderr 的警告或进度信息会保留在 [stderr] 区段中
  • execshell 两种模式都会应用该限制。区别在于 exec 模式只关闭该命令的通道,而 shell 模式的通道由该连接上的所有命令共用、远端在中止后仍会继续写入,因此会断开连接(与 shell 模式命令超时的处理一致)

🗂️ 列出所有SSH服务器

可以通过MCP工具 list-servers 获取所有可用的SSH服务器配置:

调用示例:

{
"tool": "list-servers",
"params": {}
}

返回示例:

[
{ "name": "dev", "host": "1.2.3.4", "port": 22, "username": "alice" },
{ "name": "prod", "host": "5.6.7.8", "port": 22, "username": "bob" }
]

⚙️ 命令行选项参考

选项:
--config-file JSON 配置文件路径(推荐用于多服务器配置)
--ssh-config-file SSH 配置文件路径(默认: ~/.ssh/config)
--ssh SSH 连接配置(可以是 JSON 字符串或旧格式)
-h, --host SSH 服务器主机地址或 SSH 配置中的别名
-p, --port SSH 服务器端口
-u, --username SSH 用户名
-w, --password SSH 密码
-k, --privateKey SSH 私钥文件路径
-P, --passphrase 私钥密码(如果有的话)
-a, --agent SSH agent socket 路径
--try-keyboard 启用键盘交互式认证以支持 2FA/MFA(默认: false)
-W, --whitelist 命令白名单,以逗号分隔的正则表达式
-B, --blacklist 命令黑名单,以逗号分隔的正则表达式
--proxy 代理地址,支持 SOCKS5、HTTP 和 HTTPS
-s, --socksProxy 旧版 SOCKS5 代理地址(兼容参数)
--allowed-local-paths upload/download 允许访问的额外本地路径,逗号分隔
--allowed-remote-paths SFTP upload/download 允许访问的远端路径(POSIX 绝对路径),逗号分隔
--transport-mode SSH transport 模式: exec 或 shell(默认: exec)
--shell-ready-timeout shell 就绪探测超时,单位毫秒(默认: 10000)
--command-template 命令模板;shell 参数用 <quotedCommand>,原样插入用 <command>
--pty 为命令执行分配伪终端(默认: true)
--pre-connect 启动时预连接所有配置的 SSH 服务器
--version, -v 打印包版本
--help 打印帮助信息

🛡️ 安全注意事项

该服务器提供了在远程服务器上执行命令和传输文件的强大功能。为确保安全使用,请注意以下几点:

  • 命令白名单强烈建议 使用 --whitelist 选项来限制可执行的命令集合。如果没有白名单,任何命令都可以在远程服务器上执行,这可能带来重大的安全风险。
  • 私钥安全:服务器会将 SSH 私钥读入内存。请确保运行 ssh-mcp-server 的机器是安全的。不要将服务器暴露给不受信任的网络。
  • 拒绝服务攻击 (DoS):服务器没有内置的速率限制。攻击者可能通过向服务器发送大量连接请求或大文件传输来发起 DoS 攻击。建议在具有速率限制功能的防火墙或反向代理后面运行服务器。
  • 路径遍历:服务器内置了对本地文件系统路径遍历攻击的保护。但是,仍然需要注意在 uploaddownload 命令中使用的路径。
  • 本地传输范围:默认仅允许访问当前工作目录。只有在明确可信时,才建议通过 --allowed-local-paths 或配置文件中的 allowedLocalPaths 放宽范围。
  • 远端传输范围:SFTP upload/download 仅接受绝对 POSIX 路径。未配置 allowedRemotePaths(或 --allowed-remote-paths)时,任意远端路径都允许,但启动时会打印警告。强烈建议显式配置 allowedRemotePaths 白名单,避免模型被 prompt 注入后读写 ~/.ssh/authorized_keys/etc/sshd_config 之类敏感文件。

🌟 Star 历史

Star History

Star History Chart

About

基于 SSH 的 MCP 服务 🧙‍♀️。已被MCP官方收录 🎉。 SSH MCP Server 🧙‍♀️. It has been included in the community MCP repository 🎉.

Resources

Stars

822 stars

Watchers

3 watching

Forks

Releases

Packages

Used by

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Repository files navigation

ssh-mcp-server logo

ssh-mcp-server

NPM VersionGitHub forksGitHub Repo starsGitHub Issues or Pull RequestsGitHub Issues or Pull RequestsGitHub Issues or Pull RequestsGitHub Issues or Pull Requests

基于 SSH 的 MCP (Model Context Protocol) 服务器,允许通过 MCP 协议远程执行 SSH 命令。

English Document | 中文文档

📝 项目介绍

ssh-mcp-server 是一个桥接工具,可以让 AI 助手等支持 MCP 协议的应用通过标准化接口执行远程 SSH 命令。这使得 AI 助手能够安全地操作远程服务器,执行命令并获取结果,而无需直接暴露 SSH 凭据给 AI 模型。

💬 如有任何问题,欢迎加入微信群交流:

wechat

✨ 功能亮点

  • 🔒 安全连接:支持多种安全的 SSH 连接方式,包括密码认证和私钥认证(支持带密码的私钥)
  • 🛡️ 命令安全控制:通过灵活的黑白名单机制,精确控制允许执行的命令范围,防止危险操作
  • 🔄 标准化接口:符合 MCP 协议规范,与支持该协议的 AI 助手无缝集成
  • 🚇 双传输模式:同时支持 execshell 两种 transport,兼容直连主机与堡垒机或跳板机场景
  • 📂 文件传输:支持双向文件传输功能,可上传本地文件到服务器或从服务器下载文件
  • 🔑 凭据隔离:SSH 凭据完全在本地管理,不会暴露给 AI 模型,增强安全性
  • 🚀 即用即走:使用 NPX 可直接运行,无需全局安装,方便快捷

📦 开源仓库

GitHub:https://github.com/classfang/ssh-mcp-server

NPM: https://www.npmjs.com/package/@fangjunjie/ssh-mcp-server

🛠️ 工具列表

工具名称描述
execute-command命令执行工具在远程服务器上执行 SSH 命令并获取执行结果
upload文件上传工具将本地文件上传到远程服务器指定位置
download文件下载工具从远程服务器下载文件到本地指定位置
list-servers服务器列表工具列出所有可用SSH服务器配置

📚 使用方法

0. 🤖 通过 AI Skill 快速配置(推荐)

如果你使用支持 skill 的 AI 编程助手(如 Claude Code),可以直接使用内置的 ssh-mcp-helper skill 通过交互式问答完成安装和配置,无需手动编辑 JSON 文件。

使用方式:

  1. 从本仓库 skills/ 目录安装该 skill
  2. 告诉你的 AI 助手:"帮我配置 ssh-mcp-server" 或 "给 Cursor 加一个 SSH MCP 连接"
  3. skill 会逐步引导你:检查 Node.js 环境 → 选择 MCP 客户端 → 选择认证方式 → 收集连接参数 → 生成并写入配置

该 skill 支持下文所有场景(账号密码、私钥、SSH config 复用、SOCKS 代理、堡垒机、多连接、2FA、命令限制等),并自动生成格式正确的配置。


下面的章节按从简单到复杂的顺序排列,最简单的入门方式就是用账号密码连接服务器。直接复制对应场景下的 mcp.json 配置到你的 MCP 客户端即可使用。

⚠️ 重要提示:在 MCP 配置文件中,每个命令行参数和其值必须是 args 数组中的独立元素。不要用空格将它们连接在一起。例如,使用 "--host", "192.168.1.1" 而不是 "--host 192.168.1.1"

1. 🔑 账号密码(最简单)

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "192.168.1.1",
"--port", "22",
"--username", "root",
"--password", "pwd123456"
]
}
}
}

2. 🔐 账号 + 私钥

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "192.168.1.1",
"--port", "22",
"--username", "root",
"--privateKey", "~/.ssh/id_rsa"
]
}
}
}

3. 🔏 带密码的私钥

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "192.168.1.1",
"--port", "22",
"--username", "root",
"--privateKey", "~/.ssh/id_rsa",
"--passphrase", "pwd123456"
]
}
}
}

4. 📋 复用 ~/.ssh/config

如果你已经在 ~/.ssh/config 配置了主机别名,服务器会自动从中读取连接参数,mcp.json 里就不用再写一遍。

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "myserver"
]
}
}
}

假设你的 ~/.ssh/config 包含:

Host myserver
HostName 192.168.1.1
Port 22
User root
IdentityFile ~/.ssh/id_rsa

你也可以指定自定义的 SSH 配置文件路径:

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "myserver",
"--ssh-config-file", "/path/to/custom/ssh_config"
]
}
}
}

注意:命令行参数优先级高于 SSH 配置值。例如,如果你指定了 --port 2222,它会覆盖 SSH 配置中的端口。

5. 🌐 通过代理连接

当目标主机只能通过代理访问时,可使用 --proxy 配置 SOCKS5、HTTP 或 HTTPS 代理。

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "192.168.1.1",
"--port", "22",
"--username", "root",
"--password", "pwd123456",
"--proxy", "http://username:password@proxy-host:proxy-port"
]
}
}
}

支持的 URL 格式:

socks://username:password@proxy-host:1080
socks5://username:password@proxy-host:1080
http://username:password@proxy-host:8080
https://username:password@proxy-host:8443

HTTP 和 HTTPS 代理通过 CONNECT 方法建立到 SSH 服务的隧道,用户名和密码使用 Basic 代理认证。HTTP、HTTPS 未填写端口时分别默认使用 80443;SOCKS5 必须填写端口。HTTPS 代理证书使用 Node.js 默认信任链进行验证。

原有 socksProxy 配置和 --socksProxy 参数继续兼容,但只接受 socks://socks5://。不要同时配置 proxysocksProxy

6. 📝 使用命令白名单 / 黑名单

通过 --whitelist--blacklist 限制服务器允许执行的命令范围。多个模式之间用逗号分隔,每个模式都是一个正则表达式。生产环境强烈建议配置

白名单示例(仅允许只读型查看命令):

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "192.168.1.1",
"--port", "22",
"--username", "root",
"--password", "pwd123456",
"--whitelist", "^ls( .*)?,^cat .*,^df.*"
]
}
}
}

黑名单示例(屏蔽危险命令):

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "192.168.1.1",
"--port", "22",
"--username", "root",
"--password", "pwd123456",
"--blacklist", "^rm .*,^shutdown.*,^reboot.*"
]
}
}
}

注意:如果同时指定了白名单和黑名单,系统会先检查命令是否在白名单中,再检查是否在黑名单中,命令必须同时通过两项检查才能被执行。

7. 🧩 使用命令模板包裹命令

commandTemplate 会把每条执行的命令套进一个模板里,适合切换用户(su)、放进容器、或经过跳板机的场景。当命令会作为 shell 参数传入时使用 <quotedCommand>,需要原样插入时使用 <command>;模板会在目录 cd 拼接之后应用,因此整个 cd ... && <实际命令> 都会被包裹起来。

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "10.0.0.1",
"--port", "22",
"--username", "deploy",
"--password", "xxx",
"--command-template", "su root -c <quotedCommand>"
]
}
}
}

当指定目录为 /data 执行 ls /app 时,实际发送的命令是:

su root -c 'cd -- '\''/data'\'' && ls /app'

其他常见模板:

sudo bash -c <quotedCommand>
docker exec -i mycontainer sh -c <quotedCommand>
ssh jumphost <quotedCommand>

8. 🚇 堡垒机 / 跳板机(transportMode: shell

transportMode 默认是 exec。出现下面这些情况时,应该切换到 shell

  • SSH 登录成功,但 exec 执行命令失败
  • 远端必须等登录 banner、profile、环境初始化完成后才能正常执行命令
  • 连接目标本质上是堡垒机或只暴露交互式 shell 的设备

两者差异:

  • exec:支持 execute-commanduploaddownload
  • shell:命令通过持久 shell 会话串行执行,内部带命令队列;但不支持upload / download,因为该模式下禁用了 SFTP
{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "bastion.example.com",
"--port", "22",
"--username", "ops",
"--password", "pwd123456",
"--transport-mode", "shell",
"--shell-ready-timeout", "15000"
]
}
}
}

JSON 配置文件中还可以通过 shellCommandTimeoutMs 覆盖 shell 模式下单条命令的默认超时。

9. 🔐 多因素认证(2FA / MFA)

当 SSH 服务器要求多因素认证(密码 + 私钥 + 2FA 验证码)时启用 tryKeyboard。密码和私钥会自动提供;对于非密码提示,请在连接前通过服务端环境变量 SSH_MCP_2FA_CODE 提供验证码。

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "example.com",
"--port", "22",
"--username", "user",
"--password", "your_password",
"--privateKey", "/path/to/key",
"--try-keyboard"
]
}
}
}

认证流程:

  1. 私钥认证(如果提供)
  2. 密码认证(如果提供)
  3. 键盘交互式认证通过 SSH_MCP_2FA_CODE 提供 2FA 验证码

10. 🧩 多 SSH 连接配置

需要在同一个 MCP server 里同时管理多个 SSH 目标时,给每个连接命名,调用时通过 connectionName 选择。共有三种配置方式:

📄 方式一:使用配置文件(推荐)

创建 JSON 配置文件(例如 ssh-config.json):

数组格式:

[
{
"name": "dev",
"host": "1.2.3.4",
"port": 22,
"username": "alice",
"password": "{abc=P100s0}",
"socksProxy": "socks://127.0.0.1:10808",
"commandTimeoutMs": 120000,
"maxOutputBytes": 10485760
},
{
"name": "bastion",
"host": "9.9.9.9",
"port": 22,
"username": "ops",
"password": "pwd123456",
"transportMode": "shell",
"shellReadyTimeoutMs": 15000,
"shellCommandTimeoutMs": 45000,
"connectionTimeoutMs": 30000,
"keepaliveIntervalMs": 10000,
"keepaliveCountMax": 3
},
{
"name": "prod",
"host": "5.6.7.8",
"port": 22,
"username": "bob",
"password": "yyy",
"socksProxy": "socks://127.0.0.1:10808"
},
{
"name": "secure-server",
"host": "secure.example.com",
"port": 22,
"username": "admin",
"password": "your_password",
"privateKey": "/path/to/private/key",
"tryKeyboard": true
}
]

对象格式:

{
"dev": {
"host": "1.2.3.4",
"port": 22,
"username": "alice",
"password": "{abc=P100s0}",
"socksProxy": "socks://127.0.0.1:10808",
"commandTimeoutMs": 120000,
"maxOutputBytes": 10485760
},
"bastion": {
"host": "9.9.9.9",
"port": 22,
"username": "ops",
"password": "pwd123456",
"transportMode": "shell",
"shellReadyTimeoutMs": 15000,
"shellCommandTimeoutMs": 45000
},
"prod": {
"host": "5.6.7.8",
"port": 22,
"username": "bob",
"password": "yyy",
"socksProxy": "socks://127.0.0.1:10808"
}
}

然后使用 --config-file 参数:

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--config-file", "ssh-config.json"
]
}
}
}

🔧 方式二:使用 JSON 格式的 --ssh 参数

可以直接传递 JSON 格式的配置字符串:

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--ssh", "{\"name\":\"dev\",\"host\":\"1.2.3.4\",\"port\":22,\"username\":\"alice\",\"password\":\"{abc=P100s0}\",\"socksProxy\":\"socks://127.0.0.1:10808\"}",
"--ssh", "{\"name\":\"bastion\",\"host\":\"9.9.9.9\",\"port\":22,\"username\":\"ops\",\"password\":\"pwd123456\",\"transportMode\":\"shell\",\"shellReadyTimeoutMs\":15000}",
"--ssh", "{\"name\":\"prod\",\"host\":\"5.6.7.8\",\"port\":22,\"username\":\"bob\",\"password\":\"yyy\",\"socksProxy\":\"socks://127.0.0.1:10808\"}"
]
}
}
}

📝 方式三:旧格式逗号分隔(向后兼容)

对于密码中不包含特殊字符的简单情况,仍可使用旧格式:

npx @fangjunjie/ssh-mcp-server \
--ssh "name=dev,host=1.2.3.4,port=22,user=alice,password=xxx" \
--ssh "name=prod,host=5.6.7.8,port=22,user=bob,password=yyy"

⚠️ 注意:旧格式在处理包含特殊字符(如 =,{})的密码时可能会有问题。如果密码包含特殊字符,请使用方式一或方式二。

在MCP工具调用时,通过 connectionName 参数指定目标连接名称,未指定时使用默认连接。

示例(在prod连接上执行命令):

{
"tool": "execute-command",
"params": {
"cmdString": "ls -al",
"connectionName": "prod"
}
}

示例(带超时选项的命令执行):

{
"tool": "execute-command",
"params": {
"cmdString": "ping -c 10 127.0.0.1",
"connectionName": "prod",
"timeout": 5000
}
}

⏱️ 命令执行超时

execute-command 工具支持超时选项,防止命令无限期挂起:

  • timeout: 单次调用的命令执行超时时间(毫秒,可选);传入时会覆盖连接配置,未传入时使用对应连接配置或其 30000ms 默认值
  • 在 JSON 配置文件里为单个连接设置 commandTimeoutMs,可以改掉这个默认值,避免每次调用都手动传 timeoutexec 模式)
  • shell 模式对应的配置项是 shellCommandTimeoutMs
  • 调用参数里的 timeout 始终优先于上面两个配置项
  • 连接默认启用 SSH keepalive(keepaliveIntervalMs: 10000,keepaliveCountMax: 3),并使用 connectionTimeoutMs 限制连接建立时间
  • SFTP 打开和传输操作使用 sftpTimeoutMs 控制超时(默认 300000ms)
  • 错误响应现在包含稳定的 codemessageretriable 字段,便于上层 Agent 处理

这对于像 pingtail -f 或其他可能阻塞执行的长时间运行进程特别有用。

📦 命令输出限制

会限制单条命令捕获的 stdoutstderr 总量,避免大文件或无限输出耗尽 MCP server 内存:

  • 在 JSON 连接配置中使用 maxOutputBytes 设置上限,默认值为 10485760(10 MiB)
  • maxOutputBytes 必须是非负整数;设置为 0 可禁用限制,但不建议对不受信任的命令禁用
  • 输出超过限制时,远端命令会被中止,工具返回 OUTPUT_LIMIT_EXCEEDED 错误和已经捕获的截断输出,不会把中止的命令误报为成功
  • ptyfalse 时,成功命令写入 stderr 的警告或进度信息会保留在 [stderr] 区段中
  • execshell 两种模式都会应用该限制。区别在于 exec 模式只关闭该命令的通道,而 shell 模式的通道由该连接上的所有命令共用、远端在中止后仍会继续写入,因此会断开连接(与 shell 模式命令超时的处理一致)

🗂️ 列出所有SSH服务器

可以通过MCP工具 list-servers 获取所有可用的SSH服务器配置:

调用示例:

{
"tool": "list-servers",
"params": {}
}

返回示例:

[
{ "name": "dev", "host": "1.2.3.4", "port": 22, "username": "alice" },
{ "name": "prod", "host": "5.6.7.8", "port": 22, "username": "bob" }
]

⚙️ 命令行选项参考

选项:
--config-file JSON 配置文件路径(推荐用于多服务器配置)
--ssh-config-file SSH 配置文件路径(默认: ~/.ssh/config)
--ssh SSH 连接配置(可以是 JSON 字符串或旧格式)
-h, --host SSH 服务器主机地址或 SSH 配置中的别名
-p, --port SSH 服务器端口
-u, --username SSH 用户名
-w, --password SSH 密码
-k, --privateKey SSH 私钥文件路径
-P, --passphrase 私钥密码(如果有的话)
-a, --agent SSH agent socket 路径
--try-keyboard 启用键盘交互式认证以支持 2FA/MFA(默认: false)
-W, --whitelist 命令白名单,以逗号分隔的正则表达式
-B, --blacklist 命令黑名单,以逗号分隔的正则表达式
--proxy 代理地址,支持 SOCKS5、HTTP 和 HTTPS
-s, --socksProxy 旧版 SOCKS5 代理地址(兼容参数)
--allowed-local-paths upload/download 允许访问的额外本地路径,逗号分隔
--allowed-remote-paths SFTP upload/download 允许访问的远端路径(POSIX 绝对路径),逗号分隔
--transport-mode SSH transport 模式: exec 或 shell(默认: exec)
--shell-ready-timeout shell 就绪探测超时,单位毫秒(默认: 10000)
--command-template 命令模板;shell 参数用 <quotedCommand>,原样插入用 <command>
--pty 为命令执行分配伪终端(默认: true)
--pre-connect 启动时预连接所有配置的 SSH 服务器
--version, -v 打印包版本
--help 打印帮助信息

🛡️ 安全注意事项

该服务器提供了在远程服务器上执行命令和传输文件的强大功能。为确保安全使用,请注意以下几点:

  • 命令白名单强烈建议 使用 --whitelist 选项来限制可执行的命令集合。如果没有白名单,任何命令都可以在远程服务器上执行,这可能带来重大的安全风险。
  • 私钥安全:服务器会将 SSH 私钥读入内存。请确保运行 ssh-mcp-server 的机器是安全的。不要将服务器暴露给不受信任的网络。
  • 拒绝服务攻击 (DoS):服务器没有内置的速率限制。攻击者可能通过向服务器发送大量连接请求或大文件传输来发起 DoS 攻击。建议在具有速率限制功能的防火墙或反向代理后面运行服务器。
  • 路径遍历:服务器内置了对本地文件系统路径遍历攻击的保护。但是,仍然需要注意在 uploaddownload 命令中使用的路径。
  • 本地传输范围:默认仅允许访问当前工作目录。只有在明确可信时,才建议通过 --allowed-local-paths 或配置文件中的 allowedLocalPaths 放宽范围。
  • 远端传输范围:SFTP upload/download 仅接受绝对 POSIX 路径。未配置 allowedRemotePaths(或 --allowed-remote-paths)时,任意远端路径都允许,但启动时会打印警告。强烈建议显式配置 allowedRemotePaths 白名单,避免模型被 prompt 注入后读写 ~/.ssh/authorized_keys/etc/sshd_config 之类敏感文件。

🌟 Star 历史

Star History

Star History Chart

About

基于 SSH 的 MCP 服务 🧙‍♀️。已被MCP官方收录 🎉。 SSH MCP Server 🧙‍♀️. It has been included in the community MCP repository 🎉.

Resources

Stars

822 stars

Watchers

3 watching

Forks

Releases

Packages

Used by

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length > 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Repository files navigation

ssh-mcp-server logo

ssh-mcp-server

NPM VersionGitHub forksGitHub Repo starsGitHub Issues or Pull RequestsGitHub Issues or Pull RequestsGitHub Issues or Pull RequestsGitHub Issues or Pull Requests

基于 SSH 的 MCP (Model Context Protocol) 服务器,允许通过 MCP 协议远程执行 SSH 命令。

English Document | 中文文档

📝 项目介绍

ssh-mcp-server 是一个桥接工具,可以让 AI 助手等支持 MCP 协议的应用通过标准化接口执行远程 SSH 命令。这使得 AI 助手能够安全地操作远程服务器,执行命令并获取结果,而无需直接暴露 SSH 凭据给 AI 模型。

💬 如有任何问题,欢迎加入微信群交流:

wechat

✨ 功能亮点

  • 🔒 安全连接:支持多种安全的 SSH 连接方式,包括密码认证和私钥认证(支持带密码的私钥)
  • 🛡️ 命令安全控制:通过灵活的黑白名单机制,精确控制允许执行的命令范围,防止危险操作
  • 🔄 标准化接口:符合 MCP 协议规范,与支持该协议的 AI 助手无缝集成
  • 🚇 双传输模式:同时支持 execshell 两种 transport,兼容直连主机与堡垒机或跳板机场景
  • 📂 文件传输:支持双向文件传输功能,可上传本地文件到服务器或从服务器下载文件
  • 🔑 凭据隔离:SSH 凭据完全在本地管理,不会暴露给 AI 模型,增强安全性
  • 🚀 即用即走:使用 NPX 可直接运行,无需全局安装,方便快捷

📦 开源仓库

GitHub:https://github.com/classfang/ssh-mcp-server

NPM: https://www.npmjs.com/package/@fangjunjie/ssh-mcp-server

🛠️ 工具列表

工具名称描述
execute-command命令执行工具在远程服务器上执行 SSH 命令并获取执行结果
upload文件上传工具将本地文件上传到远程服务器指定位置
download文件下载工具从远程服务器下载文件到本地指定位置
list-servers服务器列表工具列出所有可用SSH服务器配置

📚 使用方法

0. 🤖 通过 AI Skill 快速配置(推荐)

如果你使用支持 skill 的 AI 编程助手(如 Claude Code),可以直接使用内置的 ssh-mcp-helper skill 通过交互式问答完成安装和配置,无需手动编辑 JSON 文件。

使用方式:

  1. 从本仓库 skills/ 目录安装该 skill
  2. 告诉你的 AI 助手:"帮我配置 ssh-mcp-server" 或 "给 Cursor 加一个 SSH MCP 连接"
  3. skill 会逐步引导你:检查 Node.js 环境 → 选择 MCP 客户端 → 选择认证方式 → 收集连接参数 → 生成并写入配置

该 skill 支持下文所有场景(账号密码、私钥、SSH config 复用、SOCKS 代理、堡垒机、多连接、2FA、命令限制等),并自动生成格式正确的配置。


下面的章节按从简单到复杂的顺序排列,最简单的入门方式就是用账号密码连接服务器。直接复制对应场景下的 mcp.json 配置到你的 MCP 客户端即可使用。

⚠️ 重要提示:在 MCP 配置文件中,每个命令行参数和其值必须是 args 数组中的独立元素。不要用空格将它们连接在一起。例如,使用 "--host", "192.168.1.1" 而不是 "--host 192.168.1.1"

1. 🔑 账号密码(最简单)

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "192.168.1.1",
"--port", "22",
"--username", "root",
"--password", "pwd123456"
]
}
}
}

2. 🔐 账号 + 私钥

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "192.168.1.1",
"--port", "22",
"--username", "root",
"--privateKey", "~/.ssh/id_rsa"
]
}
}
}

3. 🔏 带密码的私钥

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "192.168.1.1",
"--port", "22",
"--username", "root",
"--privateKey", "~/.ssh/id_rsa",
"--passphrase", "pwd123456"
]
}
}
}

4. 📋 复用 ~/.ssh/config

如果你已经在 ~/.ssh/config 配置了主机别名,服务器会自动从中读取连接参数,mcp.json 里就不用再写一遍。

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "myserver"
]
}
}
}

假设你的 ~/.ssh/config 包含:

Host myserver
HostName 192.168.1.1
Port 22
User root
IdentityFile ~/.ssh/id_rsa

你也可以指定自定义的 SSH 配置文件路径:

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "myserver",
"--ssh-config-file", "/path/to/custom/ssh_config"
]
}
}
}

注意:命令行参数优先级高于 SSH 配置值。例如,如果你指定了 --port 2222,它会覆盖 SSH 配置中的端口。

5. 🌐 通过代理连接

当目标主机只能通过代理访问时,可使用 --proxy 配置 SOCKS5、HTTP 或 HTTPS 代理。

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "192.168.1.1",
"--port", "22",
"--username", "root",
"--password", "pwd123456",
"--proxy", "http://username:password@proxy-host:proxy-port"
]
}
}
}

支持的 URL 格式:

socks://username:password@proxy-host:1080
socks5://username:password@proxy-host:1080
http://username:password@proxy-host:8080
https://username:password@proxy-host:8443

HTTP 和 HTTPS 代理通过 CONNECT 方法建立到 SSH 服务的隧道,用户名和密码使用 Basic 代理认证。HTTP、HTTPS 未填写端口时分别默认使用 80443;SOCKS5 必须填写端口。HTTPS 代理证书使用 Node.js 默认信任链进行验证。

原有 socksProxy 配置和 --socksProxy 参数继续兼容,但只接受 socks://socks5://。不要同时配置 proxysocksProxy

6. 📝 使用命令白名单 / 黑名单

通过 --whitelist--blacklist 限制服务器允许执行的命令范围。多个模式之间用逗号分隔,每个模式都是一个正则表达式。生产环境强烈建议配置

白名单示例(仅允许只读型查看命令):

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "192.168.1.1",
"--port", "22",
"--username", "root",
"--password", "pwd123456",
"--whitelist", "^ls( .*)?,^cat .*,^df.*"
]
}
}
}

黑名单示例(屏蔽危险命令):

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "192.168.1.1",
"--port", "22",
"--username", "root",
"--password", "pwd123456",
"--blacklist", "^rm .*,^shutdown.*,^reboot.*"
]
}
}
}

注意:如果同时指定了白名单和黑名单,系统会先检查命令是否在白名单中,再检查是否在黑名单中,命令必须同时通过两项检查才能被执行。

7. 🧩 使用命令模板包裹命令

commandTemplate 会把每条执行的命令套进一个模板里,适合切换用户(su)、放进容器、或经过跳板机的场景。当命令会作为 shell 参数传入时使用 <quotedCommand>,需要原样插入时使用 <command>;模板会在目录 cd 拼接之后应用,因此整个 cd ... && <实际命令> 都会被包裹起来。

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "10.0.0.1",
"--port", "22",
"--username", "deploy",
"--password", "xxx",
"--command-template", "su root -c <quotedCommand>"
]
}
}
}

当指定目录为 /data 执行 ls /app 时,实际发送的命令是:

su root -c 'cd -- '\''/data'\'' && ls /app'

其他常见模板:

sudo bash -c <quotedCommand>
docker exec -i mycontainer sh -c <quotedCommand>
ssh jumphost <quotedCommand>

8. 🚇 堡垒机 / 跳板机(transportMode: shell

transportMode 默认是 exec。出现下面这些情况时,应该切换到 shell

  • SSH 登录成功,但 exec 执行命令失败
  • 远端必须等登录 banner、profile、环境初始化完成后才能正常执行命令
  • 连接目标本质上是堡垒机或只暴露交互式 shell 的设备

两者差异:

  • exec:支持 execute-commanduploaddownload
  • shell:命令通过持久 shell 会话串行执行,内部带命令队列;但不支持upload / download,因为该模式下禁用了 SFTP
{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "bastion.example.com",
"--port", "22",
"--username", "ops",
"--password", "pwd123456",
"--transport-mode", "shell",
"--shell-ready-timeout", "15000"
]
}
}
}

JSON 配置文件中还可以通过 shellCommandTimeoutMs 覆盖 shell 模式下单条命令的默认超时。

9. 🔐 多因素认证(2FA / MFA)

当 SSH 服务器要求多因素认证(密码 + 私钥 + 2FA 验证码)时启用 tryKeyboard。密码和私钥会自动提供;对于非密码提示,请在连接前通过服务端环境变量 SSH_MCP_2FA_CODE 提供验证码。

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "example.com",
"--port", "22",
"--username", "user",
"--password", "your_password",
"--privateKey", "/path/to/key",
"--try-keyboard"
]
}
}
}

认证流程:

  1. 私钥认证(如果提供)
  2. 密码认证(如果提供)
  3. 键盘交互式认证通过 SSH_MCP_2FA_CODE 提供 2FA 验证码

10. 🧩 多 SSH 连接配置

需要在同一个 MCP server 里同时管理多个 SSH 目标时,给每个连接命名,调用时通过 connectionName 选择。共有三种配置方式:

📄 方式一:使用配置文件(推荐)

创建 JSON 配置文件(例如 ssh-config.json):

数组格式:

[
{
"name": "dev",
"host": "1.2.3.4",
"port": 22,
"username": "alice",
"password": "{abc=P100s0}",
"socksProxy": "socks://127.0.0.1:10808",
"commandTimeoutMs": 120000,
"maxOutputBytes": 10485760
},
{
"name": "bastion",
"host": "9.9.9.9",
"port": 22,
"username": "ops",
"password": "pwd123456",
"transportMode": "shell",
"shellReadyTimeoutMs": 15000,
"shellCommandTimeoutMs": 45000,
"connectionTimeoutMs": 30000,
"keepaliveIntervalMs": 10000,
"keepaliveCountMax": 3
},
{
"name": "prod",
"host": "5.6.7.8",
"port": 22,
"username": "bob",
"password": "yyy",
"socksProxy": "socks://127.0.0.1:10808"
},
{
"name": "secure-server",
"host": "secure.example.com",
"port": 22,
"username": "admin",
"password": "your_password",
"privateKey": "/path/to/private/key",
"tryKeyboard": true
}
]

对象格式:

{
"dev": {
"host": "1.2.3.4",
"port": 22,
"username": "alice",
"password": "{abc=P100s0}",
"socksProxy": "socks://127.0.0.1:10808",
"commandTimeoutMs": 120000,
"maxOutputBytes": 10485760
},
"bastion": {
"host": "9.9.9.9",
"port": 22,
"username": "ops",
"password": "pwd123456",
"transportMode": "shell",
"shellReadyTimeoutMs": 15000,
"shellCommandTimeoutMs": 45000
},
"prod": {
"host": "5.6.7.8",
"port": 22,
"username": "bob",
"password": "yyy",
"socksProxy": "socks://127.0.0.1:10808"
}
}

然后使用 --config-file 参数:

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--config-file", "ssh-config.json"
]
}
}
}

🔧 方式二:使用 JSON 格式的 --ssh 参数

可以直接传递 JSON 格式的配置字符串:

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--ssh", "{\"name\":\"dev\",\"host\":\"1.2.3.4\",\"port\":22,\"username\":\"alice\",\"password\":\"{abc=P100s0}\",\"socksProxy\":\"socks://127.0.0.1:10808\"}",
"--ssh", "{\"name\":\"bastion\",\"host\":\"9.9.9.9\",\"port\":22,\"username\":\"ops\",\"password\":\"pwd123456\",\"transportMode\":\"shell\",\"shellReadyTimeoutMs\":15000}",
"--ssh", "{\"name\":\"prod\",\"host\":\"5.6.7.8\",\"port\":22,\"username\":\"bob\",\"password\":\"yyy\",\"socksProxy\":\"socks://127.0.0.1:10808\"}"
]
}
}
}

📝 方式三:旧格式逗号分隔(向后兼容)

对于密码中不包含特殊字符的简单情况,仍可使用旧格式:

npx @fangjunjie/ssh-mcp-server \
--ssh "name=dev,host=1.2.3.4,port=22,user=alice,password=xxx" \
--ssh "name=prod,host=5.6.7.8,port=22,user=bob,password=yyy"

⚠️ 注意:旧格式在处理包含特殊字符(如 =,{})的密码时可能会有问题。如果密码包含特殊字符,请使用方式一或方式二。

在MCP工具调用时,通过 connectionName 参数指定目标连接名称,未指定时使用默认连接。

示例(在prod连接上执行命令):

{
"tool": "execute-command",
"params": {
"cmdString": "ls -al",
"connectionName": "prod"
}
}

示例(带超时选项的命令执行):

{
"tool": "execute-command",
"params": {
"cmdString": "ping -c 10 127.0.0.1",
"connectionName": "prod",
"timeout": 5000
}
}

⏱️ 命令执行超时

execute-command 工具支持超时选项,防止命令无限期挂起:

  • timeout: 单次调用的命令执行超时时间(毫秒,可选);传入时会覆盖连接配置,未传入时使用对应连接配置或其 30000ms 默认值
  • 在 JSON 配置文件里为单个连接设置 commandTimeoutMs,可以改掉这个默认值,避免每次调用都手动传 timeoutexec 模式)
  • shell 模式对应的配置项是 shellCommandTimeoutMs
  • 调用参数里的 timeout 始终优先于上面两个配置项
  • 连接默认启用 SSH keepalive(keepaliveIntervalMs: 10000,keepaliveCountMax: 3),并使用 connectionTimeoutMs 限制连接建立时间
  • SFTP 打开和传输操作使用 sftpTimeoutMs 控制超时(默认 300000ms)
  • 错误响应现在包含稳定的 codemessageretriable 字段,便于上层 Agent 处理

这对于像 pingtail -f 或其他可能阻塞执行的长时间运行进程特别有用。

📦 命令输出限制

会限制单条命令捕获的 stdoutstderr 总量,避免大文件或无限输出耗尽 MCP server 内存:

  • 在 JSON 连接配置中使用 maxOutputBytes 设置上限,默认值为 10485760(10 MiB)
  • maxOutputBytes 必须是非负整数;设置为 0 可禁用限制,但不建议对不受信任的命令禁用
  • 输出超过限制时,远端命令会被中止,工具返回 OUTPUT_LIMIT_EXCEEDED 错误和已经捕获的截断输出,不会把中止的命令误报为成功
  • ptyfalse 时,成功命令写入 stderr 的警告或进度信息会保留在 [stderr] 区段中
  • execshell 两种模式都会应用该限制。区别在于 exec 模式只关闭该命令的通道,而 shell 模式的通道由该连接上的所有命令共用、远端在中止后仍会继续写入,因此会断开连接(与 shell 模式命令超时的处理一致)

🗂️ 列出所有SSH服务器

可以通过MCP工具 list-servers 获取所有可用的SSH服务器配置:

调用示例:

{
"tool": "list-servers",
"params": {}
}

返回示例:

[
{ "name": "dev", "host": "1.2.3.4", "port": 22, "username": "alice" },
{ "name": "prod", "host": "5.6.7.8", "port": 22, "username": "bob" }
]

⚙️ 命令行选项参考

选项:
--config-file JSON 配置文件路径(推荐用于多服务器配置)
--ssh-config-file SSH 配置文件路径(默认: ~/.ssh/config)
--ssh SSH 连接配置(可以是 JSON 字符串或旧格式)
-h, --host SSH 服务器主机地址或 SSH 配置中的别名
-p, --port SSH 服务器端口
-u, --username SSH 用户名
-w, --password SSH 密码
-k, --privateKey SSH 私钥文件路径
-P, --passphrase 私钥密码(如果有的话)
-a, --agent SSH agent socket 路径
--try-keyboard 启用键盘交互式认证以支持 2FA/MFA(默认: false)
-W, --whitelist 命令白名单,以逗号分隔的正则表达式
-B, --blacklist 命令黑名单,以逗号分隔的正则表达式
--proxy 代理地址,支持 SOCKS5、HTTP 和 HTTPS
-s, --socksProxy 旧版 SOCKS5 代理地址(兼容参数)
--allowed-local-paths upload/download 允许访问的额外本地路径,逗号分隔
--allowed-remote-paths SFTP upload/download 允许访问的远端路径(POSIX 绝对路径),逗号分隔
--transport-mode SSH transport 模式: exec 或 shell(默认: exec)
--shell-ready-timeout shell 就绪探测超时,单位毫秒(默认: 10000)
--command-template 命令模板;shell 参数用 <quotedCommand>,原样插入用 <command>
--pty 为命令执行分配伪终端(默认: true)
--pre-connect 启动时预连接所有配置的 SSH 服务器
--version, -v 打印包版本
--help 打印帮助信息

🛡️ 安全注意事项

该服务器提供了在远程服务器上执行命令和传输文件的强大功能。为确保安全使用,请注意以下几点:

  • 命令白名单强烈建议 使用 --whitelist 选项来限制可执行的命令集合。如果没有白名单,任何命令都可以在远程服务器上执行,这可能带来重大的安全风险。
  • 私钥安全:服务器会将 SSH 私钥读入内存。请确保运行 ssh-mcp-server 的机器是安全的。不要将服务器暴露给不受信任的网络。
  • 拒绝服务攻击 (DoS):服务器没有内置的速率限制。攻击者可能通过向服务器发送大量连接请求或大文件传输来发起 DoS 攻击。建议在具有速率限制功能的防火墙或反向代理后面运行服务器。
  • 路径遍历:服务器内置了对本地文件系统路径遍历攻击的保护。但是,仍然需要注意在 uploaddownload 命令中使用的路径。
  • 本地传输范围:默认仅允许访问当前工作目录。只有在明确可信时,才建议通过 --allowed-local-paths 或配置文件中的 allowedLocalPaths 放宽范围。
  • 远端传输范围:SFTP upload/download 仅接受绝对 POSIX 路径。未配置 allowedRemotePaths(或 --allowed-remote-paths)时,任意远端路径都允许,但启动时会打印警告。强烈建议显式配置 allowedRemotePaths 白名单,避免模型被 prompt 注入后读写 ~/.ssh/authorized_keys/etc/sshd_config 之类敏感文件。

🌟 Star 历史

Star History

Star History Chart

About

基于 SSH 的 MCP 服务 🧙‍♀️。已被MCP官方收录 🎉。 SSH MCP Server 🧙‍♀️. It has been included in the community MCP repository 🎉.

Resources

Stars

822 stars

Watchers

3 watching

Forks

Releases

Packages

Used by

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content

Repository files navigation

ssh-mcp-server logo

ssh-mcp-server

NPM VersionGitHub forksGitHub Repo starsGitHub Issues or Pull RequestsGitHub Issues or Pull RequestsGitHub Issues or Pull RequestsGitHub Issues or Pull Requests

基于 SSH 的 MCP (Model Context Protocol) 服务器,允许通过 MCP 协议远程执行 SSH 命令。

English Document | 中文文档

📝 项目介绍

ssh-mcp-server 是一个桥接工具,可以让 AI 助手等支持 MCP 协议的应用通过标准化接口执行远程 SSH 命令。这使得 AI 助手能够安全地操作远程服务器,执行命令并获取结果,而无需直接暴露 SSH 凭据给 AI 模型。

💬 如有任何问题,欢迎加入微信群交流:

wechat

✨ 功能亮点

  • 🔒 安全连接:支持多种安全的 SSH 连接方式,包括密码认证和私钥认证(支持带密码的私钥)
  • 🛡️ 命令安全控制:通过灵活的黑白名单机制,精确控制允许执行的命令范围,防止危险操作
  • 🔄 标准化接口:符合 MCP 协议规范,与支持该协议的 AI 助手无缝集成
  • 🚇 双传输模式:同时支持 execshell 两种 transport,兼容直连主机与堡垒机或跳板机场景
  • 📂 文件传输:支持双向文件传输功能,可上传本地文件到服务器或从服务器下载文件
  • 🔑 凭据隔离:SSH 凭据完全在本地管理,不会暴露给 AI 模型,增强安全性
  • 🚀 即用即走:使用 NPX 可直接运行,无需全局安装,方便快捷

📦 开源仓库

GitHub:https://github.com/classfang/ssh-mcp-server

NPM: https://www.npmjs.com/package/@fangjunjie/ssh-mcp-server

🛠️ 工具列表

工具名称描述
execute-command命令执行工具在远程服务器上执行 SSH 命令并获取执行结果
upload文件上传工具将本地文件上传到远程服务器指定位置
download文件下载工具从远程服务器下载文件到本地指定位置
list-servers服务器列表工具列出所有可用SSH服务器配置

📚 使用方法

0. 🤖 通过 AI Skill 快速配置(推荐)

如果你使用支持 skill 的 AI 编程助手(如 Claude Code),可以直接使用内置的 ssh-mcp-helper skill 通过交互式问答完成安装和配置,无需手动编辑 JSON 文件。

使用方式:

  1. 从本仓库 skills/ 目录安装该 skill
  2. 告诉你的 AI 助手:"帮我配置 ssh-mcp-server" 或 "给 Cursor 加一个 SSH MCP 连接"
  3. skill 会逐步引导你:检查 Node.js 环境 → 选择 MCP 客户端 → 选择认证方式 → 收集连接参数 → 生成并写入配置

该 skill 支持下文所有场景(账号密码、私钥、SSH config 复用、SOCKS 代理、堡垒机、多连接、2FA、命令限制等),并自动生成格式正确的配置。


下面的章节按从简单到复杂的顺序排列,最简单的入门方式就是用账号密码连接服务器。直接复制对应场景下的 mcp.json 配置到你的 MCP 客户端即可使用。

⚠️ 重要提示:在 MCP 配置文件中,每个命令行参数和其值必须是 args 数组中的独立元素。不要用空格将它们连接在一起。例如,使用 "--host", "192.168.1.1" 而不是 "--host 192.168.1.1"

1. 🔑 账号密码(最简单)

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "192.168.1.1",
"--port", "22",
"--username", "root",
"--password", "pwd123456"
]
}
}
}

2. 🔐 账号 + 私钥

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "192.168.1.1",
"--port", "22",
"--username", "root",
"--privateKey", "~/.ssh/id_rsa"
]
}
}
}

3. 🔏 带密码的私钥

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "192.168.1.1",
"--port", "22",
"--username", "root",
"--privateKey", "~/.ssh/id_rsa",
"--passphrase", "pwd123456"
]
}
}
}

4. 📋 复用 ~/.ssh/config

如果你已经在 ~/.ssh/config 配置了主机别名,服务器会自动从中读取连接参数,mcp.json 里就不用再写一遍。

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "myserver"
]
}
}
}

假设你的 ~/.ssh/config 包含:

Host myserver
HostName 192.168.1.1
Port 22
User root
IdentityFile ~/.ssh/id_rsa

你也可以指定自定义的 SSH 配置文件路径:

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "myserver",
"--ssh-config-file", "/path/to/custom/ssh_config"
]
}
}
}

注意:命令行参数优先级高于 SSH 配置值。例如,如果你指定了 --port 2222,它会覆盖 SSH 配置中的端口。

5. 🌐 通过代理连接

当目标主机只能通过代理访问时,可使用 --proxy 配置 SOCKS5、HTTP 或 HTTPS 代理。

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "192.168.1.1",
"--port", "22",
"--username", "root",
"--password", "pwd123456",
"--proxy", "http://username:password@proxy-host:proxy-port"
]
}
}
}

支持的 URL 格式:

socks://username:password@proxy-host:1080
socks5://username:password@proxy-host:1080
http://username:password@proxy-host:8080
https://username:password@proxy-host:8443

HTTP 和 HTTPS 代理通过 CONNECT 方法建立到 SSH 服务的隧道,用户名和密码使用 Basic 代理认证。HTTP、HTTPS 未填写端口时分别默认使用 80443;SOCKS5 必须填写端口。HTTPS 代理证书使用 Node.js 默认信任链进行验证。

原有 socksProxy 配置和 --socksProxy 参数继续兼容,但只接受 socks://socks5://。不要同时配置 proxysocksProxy

6. 📝 使用命令白名单 / 黑名单

通过 --whitelist--blacklist 限制服务器允许执行的命令范围。多个模式之间用逗号分隔,每个模式都是一个正则表达式。生产环境强烈建议配置

白名单示例(仅允许只读型查看命令):

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "192.168.1.1",
"--port", "22",
"--username", "root",
"--password", "pwd123456",
"--whitelist", "^ls( .*)?,^cat .*,^df.*"
]
}
}
}

黑名单示例(屏蔽危险命令):

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "192.168.1.1",
"--port", "22",
"--username", "root",
"--password", "pwd123456",
"--blacklist", "^rm .*,^shutdown.*,^reboot.*"
]
}
}
}

注意:如果同时指定了白名单和黑名单,系统会先检查命令是否在白名单中,再检查是否在黑名单中,命令必须同时通过两项检查才能被执行。

7. 🧩 使用命令模板包裹命令

commandTemplate 会把每条执行的命令套进一个模板里,适合切换用户(su)、放进容器、或经过跳板机的场景。当命令会作为 shell 参数传入时使用 <quotedCommand>,需要原样插入时使用 <command>;模板会在目录 cd 拼接之后应用,因此整个 cd ... && <实际命令> 都会被包裹起来。

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "10.0.0.1",
"--port", "22",
"--username", "deploy",
"--password", "xxx",
"--command-template", "su root -c <quotedCommand>"
]
}
}
}

当指定目录为 /data 执行 ls /app 时,实际发送的命令是:

su root -c 'cd -- '\''/data'\'' && ls /app'

其他常见模板:

sudo bash -c <quotedCommand>
docker exec -i mycontainer sh -c <quotedCommand>
ssh jumphost <quotedCommand>

8. 🚇 堡垒机 / 跳板机(transportMode: shell

transportMode 默认是 exec。出现下面这些情况时,应该切换到 shell

  • SSH 登录成功,但 exec 执行命令失败
  • 远端必须等登录 banner、profile、环境初始化完成后才能正常执行命令
  • 连接目标本质上是堡垒机或只暴露交互式 shell 的设备

两者差异:

  • exec:支持 execute-commanduploaddownload
  • shell:命令通过持久 shell 会话串行执行,内部带命令队列;但不支持upload / download,因为该模式下禁用了 SFTP
{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "bastion.example.com",
"--port", "22",
"--username", "ops",
"--password", "pwd123456",
"--transport-mode", "shell",
"--shell-ready-timeout", "15000"
]
}
}
}

JSON 配置文件中还可以通过 shellCommandTimeoutMs 覆盖 shell 模式下单条命令的默认超时。

9. 🔐 多因素认证(2FA / MFA)

当 SSH 服务器要求多因素认证(密码 + 私钥 + 2FA 验证码)时启用 tryKeyboard。密码和私钥会自动提供;对于非密码提示,请在连接前通过服务端环境变量 SSH_MCP_2FA_CODE 提供验证码。

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "example.com",
"--port", "22",
"--username", "user",
"--password", "your_password",
"--privateKey", "/path/to/key",
"--try-keyboard"
]
}
}
}

认证流程:

  1. 私钥认证(如果提供)
  2. 密码认证(如果提供)
  3. 键盘交互式认证通过 SSH_MCP_2FA_CODE 提供 2FA 验证码

10. 🧩 多 SSH 连接配置

需要在同一个 MCP server 里同时管理多个 SSH 目标时,给每个连接命名,调用时通过 connectionName 选择。共有三种配置方式:

📄 方式一:使用配置文件(推荐)

创建 JSON 配置文件(例如 ssh-config.json):

数组格式:

[
{
"name": "dev",
"host": "1.2.3.4",
"port": 22,
"username": "alice",
"password": "{abc=P100s0}",
"socksProxy": "socks://127.0.0.1:10808",
"commandTimeoutMs": 120000,
"maxOutputBytes": 10485760
},
{
"name": "bastion",
"host": "9.9.9.9",
"port": 22,
"username": "ops",
"password": "pwd123456",
"transportMode": "shell",
"shellReadyTimeoutMs": 15000,
"shellCommandTimeoutMs": 45000,
"connectionTimeoutMs": 30000,
"keepaliveIntervalMs": 10000,
"keepaliveCountMax": 3
},
{
"name": "prod",
"host": "5.6.7.8",
"port": 22,
"username": "bob",
"password": "yyy",
"socksProxy": "socks://127.0.0.1:10808"
},
{
"name": "secure-server",
"host": "secure.example.com",
"port": 22,
"username": "admin",
"password": "your_password",
"privateKey": "/path/to/private/key",
"tryKeyboard": true
}
]

对象格式:

{
"dev": {
"host": "1.2.3.4",
"port": 22,
"username": "alice",
"password": "{abc=P100s0}",
"socksProxy": "socks://127.0.0.1:10808",
"commandTimeoutMs": 120000,
"maxOutputBytes": 10485760
},
"bastion": {
"host": "9.9.9.9",
"port": 22,
"username": "ops",
"password": "pwd123456",
"transportMode": "shell",
"shellReadyTimeoutMs": 15000,
"shellCommandTimeoutMs": 45000
},
"prod": {
"host": "5.6.7.8",
"port": 22,
"username": "bob",
"password": "yyy",
"socksProxy": "socks://127.0.0.1:10808"
}
}

然后使用 --config-file 参数:

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--config-file", "ssh-config.json"
]
}
}
}

🔧 方式二:使用 JSON 格式的 --ssh 参数

可以直接传递 JSON 格式的配置字符串:

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--ssh", "{\"name\":\"dev\",\"host\":\"1.2.3.4\",\"port\":22,\"username\":\"alice\",\"password\":\"{abc=P100s0}\",\"socksProxy\":\"socks://127.0.0.1:10808\"}",
"--ssh", "{\"name\":\"bastion\",\"host\":\"9.9.9.9\",\"port\":22,\"username\":\"ops\",\"password\":\"pwd123456\",\"transportMode\":\"shell\",\"shellReadyTimeoutMs\":15000}",
"--ssh", "{\"name\":\"prod\",\"host\":\"5.6.7.8\",\"port\":22,\"username\":\"bob\",\"password\":\"yyy\",\"socksProxy\":\"socks://127.0.0.1:10808\"}"
]
}
}
}

📝 方式三:旧格式逗号分隔(向后兼容)

对于密码中不包含特殊字符的简单情况,仍可使用旧格式:

npx @fangjunjie/ssh-mcp-server \
--ssh "name=dev,host=1.2.3.4,port=22,user=alice,password=xxx" \
--ssh "name=prod,host=5.6.7.8,port=22,user=bob,password=yyy"

⚠️ 注意:旧格式在处理包含特殊字符(如 =,{})的密码时可能会有问题。如果密码包含特殊字符,请使用方式一或方式二。

在MCP工具调用时,通过 connectionName 参数指定目标连接名称,未指定时使用默认连接。

示例(在prod连接上执行命令):

{
"tool": "execute-command",
"params": {
"cmdString": "ls -al",
"connectionName": "prod"
}
}

示例(带超时选项的命令执行):

{
"tool": "execute-command",
"params": {
"cmdString": "ping -c 10 127.0.0.1",
"connectionName": "prod",
"timeout": 5000
}
}

⏱️ 命令执行超时

execute-command 工具支持超时选项,防止命令无限期挂起:

  • timeout: 单次调用的命令执行超时时间(毫秒,可选);传入时会覆盖连接配置,未传入时使用对应连接配置或其 30000ms 默认值
  • 在 JSON 配置文件里为单个连接设置 commandTimeoutMs,可以改掉这个默认值,避免每次调用都手动传 timeoutexec 模式)
  • shell 模式对应的配置项是 shellCommandTimeoutMs
  • 调用参数里的 timeout 始终优先于上面两个配置项
  • 连接默认启用 SSH keepalive(keepaliveIntervalMs: 10000,keepaliveCountMax: 3),并使用 connectionTimeoutMs 限制连接建立时间
  • SFTP 打开和传输操作使用 sftpTimeoutMs 控制超时(默认 300000ms)
  • 错误响应现在包含稳定的 codemessageretriable 字段,便于上层 Agent 处理

这对于像 pingtail -f 或其他可能阻塞执行的长时间运行进程特别有用。

📦 命令输出限制

会限制单条命令捕获的 stdoutstderr 总量,避免大文件或无限输出耗尽 MCP server 内存:

  • 在 JSON 连接配置中使用 maxOutputBytes 设置上限,默认值为 10485760(10 MiB)
  • maxOutputBytes 必须是非负整数;设置为 0 可禁用限制,但不建议对不受信任的命令禁用
  • 输出超过限制时,远端命令会被中止,工具返回 OUTPUT_LIMIT_EXCEEDED 错误和已经捕获的截断输出,不会把中止的命令误报为成功
  • ptyfalse 时,成功命令写入 stderr 的警告或进度信息会保留在 [stderr] 区段中
  • execshell 两种模式都会应用该限制。区别在于 exec 模式只关闭该命令的通道,而 shell 模式的通道由该连接上的所有命令共用、远端在中止后仍会继续写入,因此会断开连接(与 shell 模式命令超时的处理一致)

🗂️ 列出所有SSH服务器

可以通过MCP工具 list-servers 获取所有可用的SSH服务器配置:

调用示例:

{
"tool": "list-servers",
"params": {}
}

返回示例:

[
{ "name": "dev", "host": "1.2.3.4", "port": 22, "username": "alice" },
{ "name": "prod", "host": "5.6.7.8", "port": 22, "username": "bob" }
]

⚙️ 命令行选项参考

选项:
--config-file JSON 配置文件路径(推荐用于多服务器配置)
--ssh-config-file SSH 配置文件路径(默认: ~/.ssh/config)
--ssh SSH 连接配置(可以是 JSON 字符串或旧格式)
-h, --host SSH 服务器主机地址或 SSH 配置中的别名
-p, --port SSH 服务器端口
-u, --username SSH 用户名
-w, --password SSH 密码
-k, --privateKey SSH 私钥文件路径
-P, --passphrase 私钥密码(如果有的话)
-a, --agent SSH agent socket 路径
--try-keyboard 启用键盘交互式认证以支持 2FA/MFA(默认: false)
-W, --whitelist 命令白名单,以逗号分隔的正则表达式
-B, --blacklist 命令黑名单,以逗号分隔的正则表达式
--proxy 代理地址,支持 SOCKS5、HTTP 和 HTTPS
-s, --socksProxy 旧版 SOCKS5 代理地址(兼容参数)
--allowed-local-paths upload/download 允许访问的额外本地路径,逗号分隔
--allowed-remote-paths SFTP upload/download 允许访问的远端路径(POSIX 绝对路径),逗号分隔
--transport-mode SSH transport 模式: exec 或 shell(默认: exec)
--shell-ready-timeout shell 就绪探测超时,单位毫秒(默认: 10000)
--command-template 命令模板;shell 参数用 <quotedCommand>,原样插入用 <command>
--pty 为命令执行分配伪终端(默认: true)
--pre-connect 启动时预连接所有配置的 SSH 服务器
--version, -v 打印包版本
--help 打印帮助信息

🛡️ 安全注意事项

该服务器提供了在远程服务器上执行命令和传输文件的强大功能。为确保安全使用,请注意以下几点:

  • 命令白名单强烈建议 使用 --whitelist 选项来限制可执行的命令集合。如果没有白名单,任何命令都可以在远程服务器上执行,这可能带来重大的安全风险。
  • 私钥安全:服务器会将 SSH 私钥读入内存。请确保运行 ssh-mcp-server 的机器是安全的。不要将服务器暴露给不受信任的网络。
  • 拒绝服务攻击 (DoS):服务器没有内置的速率限制。攻击者可能通过向服务器发送大量连接请求或大文件传输来发起 DoS 攻击。建议在具有速率限制功能的防火墙或反向代理后面运行服务器。
  • 路径遍历:服务器内置了对本地文件系统路径遍历攻击的保护。但是,仍然需要注意在 uploaddownload 命令中使用的路径。
  • 本地传输范围:默认仅允许访问当前工作目录。只有在明确可信时,才建议通过 --allowed-local-paths 或配置文件中的 allowedLocalPaths 放宽范围。
  • 远端传输范围:SFTP upload/download 仅接受绝对 POSIX 路径。未配置 allowedRemotePaths(或 --allowed-remote-paths)时,任意远端路径都允许,但启动时会打印警告。强烈建议显式配置 allowedRemotePaths 白名单,避免模型被 prompt 注入后读写 ~/.ssh/authorized_keys/etc/sshd_config 之类敏感文件。

🌟 Star 历史

Star History

Star History Chart

About

基于 SSH 的 MCP 服务 🧙‍♀️。已被MCP官方收录 🎉。 SSH MCP Server 🧙‍♀️. It has been included in the community MCP repository 🎉.

Resources

Stars

822 stars

Watchers

3 watching

Forks

Releases

Packages

Used by

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Repository files navigation

ssh-mcp-server logo

ssh-mcp-server

NPM VersionGitHub forksGitHub Repo starsGitHub Issues or Pull RequestsGitHub Issues or Pull RequestsGitHub Issues or Pull RequestsGitHub Issues or Pull Requests

基于 SSH 的 MCP (Model Context Protocol) 服务器,允许通过 MCP 协议远程执行 SSH 命令。

English Document | 中文文档

📝 项目介绍

ssh-mcp-server 是一个桥接工具,可以让 AI 助手等支持 MCP 协议的应用通过标准化接口执行远程 SSH 命令。这使得 AI 助手能够安全地操作远程服务器,执行命令并获取结果,而无需直接暴露 SSH 凭据给 AI 模型。

💬 如有任何问题,欢迎加入微信群交流:

wechat

✨ 功能亮点

  • 🔒 安全连接:支持多种安全的 SSH 连接方式,包括密码认证和私钥认证(支持带密码的私钥)
  • 🛡️ 命令安全控制:通过灵活的黑白名单机制,精确控制允许执行的命令范围,防止危险操作
  • 🔄 标准化接口:符合 MCP 协议规范,与支持该协议的 AI 助手无缝集成
  • 🚇 双传输模式:同时支持 execshell 两种 transport,兼容直连主机与堡垒机或跳板机场景
  • 📂 文件传输:支持双向文件传输功能,可上传本地文件到服务器或从服务器下载文件
  • 🔑 凭据隔离:SSH 凭据完全在本地管理,不会暴露给 AI 模型,增强安全性
  • 🚀 即用即走:使用 NPX 可直接运行,无需全局安装,方便快捷

📦 开源仓库

GitHub:https://github.com/classfang/ssh-mcp-server

NPM: https://www.npmjs.com/package/@fangjunjie/ssh-mcp-server

🛠️ 工具列表

工具名称描述
execute-command命令执行工具在远程服务器上执行 SSH 命令并获取执行结果
upload文件上传工具将本地文件上传到远程服务器指定位置
download文件下载工具从远程服务器下载文件到本地指定位置
list-servers服务器列表工具列出所有可用SSH服务器配置

📚 使用方法

0. 🤖 通过 AI Skill 快速配置(推荐)

如果你使用支持 skill 的 AI 编程助手(如 Claude Code),可以直接使用内置的 ssh-mcp-helper skill 通过交互式问答完成安装和配置,无需手动编辑 JSON 文件。

使用方式:

  1. 从本仓库 skills/ 目录安装该 skill
  2. 告诉你的 AI 助手:"帮我配置 ssh-mcp-server" 或 "给 Cursor 加一个 SSH MCP 连接"
  3. skill 会逐步引导你:检查 Node.js 环境 → 选择 MCP 客户端 → 选择认证方式 → 收集连接参数 → 生成并写入配置

该 skill 支持下文所有场景(账号密码、私钥、SSH config 复用、SOCKS 代理、堡垒机、多连接、2FA、命令限制等),并自动生成格式正确的配置。


下面的章节按从简单到复杂的顺序排列,最简单的入门方式就是用账号密码连接服务器。直接复制对应场景下的 mcp.json 配置到你的 MCP 客户端即可使用。

⚠️ 重要提示:在 MCP 配置文件中,每个命令行参数和其值必须是 args 数组中的独立元素。不要用空格将它们连接在一起。例如,使用 "--host", "192.168.1.1" 而不是 "--host 192.168.1.1"

1. 🔑 账号密码(最简单)

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "192.168.1.1",
"--port", "22",
"--username", "root",
"--password", "pwd123456"
]
}
}
}

2. 🔐 账号 + 私钥

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "192.168.1.1",
"--port", "22",
"--username", "root",
"--privateKey", "~/.ssh/id_rsa"
]
}
}
}

3. 🔏 带密码的私钥

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "192.168.1.1",
"--port", "22",
"--username", "root",
"--privateKey", "~/.ssh/id_rsa",
"--passphrase", "pwd123456"
]
}
}
}

4. 📋 复用 ~/.ssh/config

如果你已经在 ~/.ssh/config 配置了主机别名,服务器会自动从中读取连接参数,mcp.json 里就不用再写一遍。

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "myserver"
]
}
}
}

假设你的 ~/.ssh/config 包含:

Host myserver
HostName 192.168.1.1
Port 22
User root
IdentityFile ~/.ssh/id_rsa

你也可以指定自定义的 SSH 配置文件路径:

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "myserver",
"--ssh-config-file", "/path/to/custom/ssh_config"
]
}
}
}

注意:命令行参数优先级高于 SSH 配置值。例如,如果你指定了 --port 2222,它会覆盖 SSH 配置中的端口。

5. 🌐 通过代理连接

当目标主机只能通过代理访问时,可使用 --proxy 配置 SOCKS5、HTTP 或 HTTPS 代理。

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "192.168.1.1",
"--port", "22",
"--username", "root",
"--password", "pwd123456",
"--proxy", "http://username:password@proxy-host:proxy-port"
]
}
}
}

支持的 URL 格式:

socks://username:password@proxy-host:1080
socks5://username:password@proxy-host:1080
http://username:password@proxy-host:8080
https://username:password@proxy-host:8443

HTTP 和 HTTPS 代理通过 CONNECT 方法建立到 SSH 服务的隧道,用户名和密码使用 Basic 代理认证。HTTP、HTTPS 未填写端口时分别默认使用 80443;SOCKS5 必须填写端口。HTTPS 代理证书使用 Node.js 默认信任链进行验证。

原有 socksProxy 配置和 --socksProxy 参数继续兼容,但只接受 socks://socks5://。不要同时配置 proxysocksProxy

6. 📝 使用命令白名单 / 黑名单

通过 --whitelist--blacklist 限制服务器允许执行的命令范围。多个模式之间用逗号分隔,每个模式都是一个正则表达式。生产环境强烈建议配置

白名单示例(仅允许只读型查看命令):

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "192.168.1.1",
"--port", "22",
"--username", "root",
"--password", "pwd123456",
"--whitelist", "^ls( .*)?,^cat .*,^df.*"
]
}
}
}

黑名单示例(屏蔽危险命令):

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "192.168.1.1",
"--port", "22",
"--username", "root",
"--password", "pwd123456",
"--blacklist", "^rm .*,^shutdown.*,^reboot.*"
]
}
}
}

注意:如果同时指定了白名单和黑名单,系统会先检查命令是否在白名单中,再检查是否在黑名单中,命令必须同时通过两项检查才能被执行。

7. 🧩 使用命令模板包裹命令

commandTemplate 会把每条执行的命令套进一个模板里,适合切换用户(su)、放进容器、或经过跳板机的场景。当命令会作为 shell 参数传入时使用 <quotedCommand>,需要原样插入时使用 <command>;模板会在目录 cd 拼接之后应用,因此整个 cd ... && <实际命令> 都会被包裹起来。

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "10.0.0.1",
"--port", "22",
"--username", "deploy",
"--password", "xxx",
"--command-template", "su root -c <quotedCommand>"
]
}
}
}

当指定目录为 /data 执行 ls /app 时,实际发送的命令是:

su root -c 'cd -- '\''/data'\'' && ls /app'

其他常见模板:

sudo bash -c <quotedCommand>
docker exec -i mycontainer sh -c <quotedCommand>
ssh jumphost <quotedCommand>

8. 🚇 堡垒机 / 跳板机(transportMode: shell

transportMode 默认是 exec。出现下面这些情况时,应该切换到 shell

  • SSH 登录成功,但 exec 执行命令失败
  • 远端必须等登录 banner、profile、环境初始化完成后才能正常执行命令
  • 连接目标本质上是堡垒机或只暴露交互式 shell 的设备

两者差异:

  • exec:支持 execute-commanduploaddownload
  • shell:命令通过持久 shell 会话串行执行,内部带命令队列;但不支持upload / download,因为该模式下禁用了 SFTP
{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "bastion.example.com",
"--port", "22",
"--username", "ops",
"--password", "pwd123456",
"--transport-mode", "shell",
"--shell-ready-timeout", "15000"
]
}
}
}

JSON 配置文件中还可以通过 shellCommandTimeoutMs 覆盖 shell 模式下单条命令的默认超时。

9. 🔐 多因素认证(2FA / MFA)

当 SSH 服务器要求多因素认证(密码 + 私钥 + 2FA 验证码)时启用 tryKeyboard。密码和私钥会自动提供;对于非密码提示,请在连接前通过服务端环境变量 SSH_MCP_2FA_CODE 提供验证码。

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "example.com",
"--port", "22",
"--username", "user",
"--password", "your_password",
"--privateKey", "/path/to/key",
"--try-keyboard"
]
}
}
}

认证流程:

  1. 私钥认证(如果提供)
  2. 密码认证(如果提供)
  3. 键盘交互式认证通过 SSH_MCP_2FA_CODE 提供 2FA 验证码

10. 🧩 多 SSH 连接配置

需要在同一个 MCP server 里同时管理多个 SSH 目标时,给每个连接命名,调用时通过 connectionName 选择。共有三种配置方式:

📄 方式一:使用配置文件(推荐)

创建 JSON 配置文件(例如 ssh-config.json):

数组格式:

[
{
"name": "dev",
"host": "1.2.3.4",
"port": 22,
"username": "alice",
"password": "{abc=P100s0}",
"socksProxy": "socks://127.0.0.1:10808",
"commandTimeoutMs": 120000,
"maxOutputBytes": 10485760
},
{
"name": "bastion",
"host": "9.9.9.9",
"port": 22,
"username": "ops",
"password": "pwd123456",
"transportMode": "shell",
"shellReadyTimeoutMs": 15000,
"shellCommandTimeoutMs": 45000,
"connectionTimeoutMs": 30000,
"keepaliveIntervalMs": 10000,
"keepaliveCountMax": 3
},
{
"name": "prod",
"host": "5.6.7.8",
"port": 22,
"username": "bob",
"password": "yyy",
"socksProxy": "socks://127.0.0.1:10808"
},
{
"name": "secure-server",
"host": "secure.example.com",
"port": 22,
"username": "admin",
"password": "your_password",
"privateKey": "/path/to/private/key",
"tryKeyboard": true
}
]

对象格式:

{
"dev": {
"host": "1.2.3.4",
"port": 22,
"username": "alice",
"password": "{abc=P100s0}",
"socksProxy": "socks://127.0.0.1:10808",
"commandTimeoutMs": 120000,
"maxOutputBytes": 10485760
},
"bastion": {
"host": "9.9.9.9",
"port": 22,
"username": "ops",
"password": "pwd123456",
"transportMode": "shell",
"shellReadyTimeoutMs": 15000,
"shellCommandTimeoutMs": 45000
},
"prod": {
"host": "5.6.7.8",
"port": 22,
"username": "bob",
"password": "yyy",
"socksProxy": "socks://127.0.0.1:10808"
}
}

然后使用 --config-file 参数:

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--config-file", "ssh-config.json"
]
}
}
}

🔧 方式二:使用 JSON 格式的 --ssh 参数

可以直接传递 JSON 格式的配置字符串:

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--ssh", "{\"name\":\"dev\",\"host\":\"1.2.3.4\",\"port\":22,\"username\":\"alice\",\"password\":\"{abc=P100s0}\",\"socksProxy\":\"socks://127.0.0.1:10808\"}",
"--ssh", "{\"name\":\"bastion\",\"host\":\"9.9.9.9\",\"port\":22,\"username\":\"ops\",\"password\":\"pwd123456\",\"transportMode\":\"shell\",\"shellReadyTimeoutMs\":15000}",
"--ssh", "{\"name\":\"prod\",\"host\":\"5.6.7.8\",\"port\":22,\"username\":\"bob\",\"password\":\"yyy\",\"socksProxy\":\"socks://127.0.0.1:10808\"}"
]
}
}
}

📝 方式三:旧格式逗号分隔(向后兼容)

对于密码中不包含特殊字符的简单情况,仍可使用旧格式:

npx @fangjunjie/ssh-mcp-server \
--ssh "name=dev,host=1.2.3.4,port=22,user=alice,password=xxx" \
--ssh "name=prod,host=5.6.7.8,port=22,user=bob,password=yyy"

⚠️ 注意:旧格式在处理包含特殊字符(如 =,{})的密码时可能会有问题。如果密码包含特殊字符,请使用方式一或方式二。

在MCP工具调用时,通过 connectionName 参数指定目标连接名称,未指定时使用默认连接。

示例(在prod连接上执行命令):

{
"tool": "execute-command",
"params": {
"cmdString": "ls -al",
"connectionName": "prod"
}
}

示例(带超时选项的命令执行):

{
"tool": "execute-command",
"params": {
"cmdString": "ping -c 10 127.0.0.1",
"connectionName": "prod",
"timeout": 5000
}
}

⏱️ 命令执行超时

execute-command 工具支持超时选项,防止命令无限期挂起:

  • timeout: 单次调用的命令执行超时时间(毫秒,可选);传入时会覆盖连接配置,未传入时使用对应连接配置或其 30000ms 默认值
  • 在 JSON 配置文件里为单个连接设置 commandTimeoutMs,可以改掉这个默认值,避免每次调用都手动传 timeoutexec 模式)
  • shell 模式对应的配置项是 shellCommandTimeoutMs
  • 调用参数里的 timeout 始终优先于上面两个配置项
  • 连接默认启用 SSH keepalive(keepaliveIntervalMs: 10000,keepaliveCountMax: 3),并使用 connectionTimeoutMs 限制连接建立时间
  • SFTP 打开和传输操作使用 sftpTimeoutMs 控制超时(默认 300000ms)
  • 错误响应现在包含稳定的 codemessageretriable 字段,便于上层 Agent 处理

这对于像 pingtail -f 或其他可能阻塞执行的长时间运行进程特别有用。

📦 命令输出限制

会限制单条命令捕获的 stdoutstderr 总量,避免大文件或无限输出耗尽 MCP server 内存:

  • 在 JSON 连接配置中使用 maxOutputBytes 设置上限,默认值为 10485760(10 MiB)
  • maxOutputBytes 必须是非负整数;设置为 0 可禁用限制,但不建议对不受信任的命令禁用
  • 输出超过限制时,远端命令会被中止,工具返回 OUTPUT_LIMIT_EXCEEDED 错误和已经捕获的截断输出,不会把中止的命令误报为成功
  • ptyfalse 时,成功命令写入 stderr 的警告或进度信息会保留在 [stderr] 区段中
  • execshell 两种模式都会应用该限制。区别在于 exec 模式只关闭该命令的通道,而 shell 模式的通道由该连接上的所有命令共用、远端在中止后仍会继续写入,因此会断开连接(与 shell 模式命令超时的处理一致)

🗂️ 列出所有SSH服务器

可以通过MCP工具 list-servers 获取所有可用的SSH服务器配置:

调用示例:

{
"tool": "list-servers",
"params": {}
}

返回示例:

[
{ "name": "dev", "host": "1.2.3.4", "port": 22, "username": "alice" },
{ "name": "prod", "host": "5.6.7.8", "port": 22, "username": "bob" }
]

⚙️ 命令行选项参考

选项:
--config-file JSON 配置文件路径(推荐用于多服务器配置)
--ssh-config-file SSH 配置文件路径(默认: ~/.ssh/config)
--ssh SSH 连接配置(可以是 JSON 字符串或旧格式)
-h, --host SSH 服务器主机地址或 SSH 配置中的别名
-p, --port SSH 服务器端口
-u, --username SSH 用户名
-w, --password SSH 密码
-k, --privateKey SSH 私钥文件路径
-P, --passphrase 私钥密码(如果有的话)
-a, --agent SSH agent socket 路径
--try-keyboard 启用键盘交互式认证以支持 2FA/MFA(默认: false)
-W, --whitelist 命令白名单,以逗号分隔的正则表达式
-B, --blacklist 命令黑名单,以逗号分隔的正则表达式
--proxy 代理地址,支持 SOCKS5、HTTP 和 HTTPS
-s, --socksProxy 旧版 SOCKS5 代理地址(兼容参数)
--allowed-local-paths upload/download 允许访问的额外本地路径,逗号分隔
--allowed-remote-paths SFTP upload/download 允许访问的远端路径(POSIX 绝对路径),逗号分隔
--transport-mode SSH transport 模式: exec 或 shell(默认: exec)
--shell-ready-timeout shell 就绪探测超时,单位毫秒(默认: 10000)
--command-template 命令模板;shell 参数用 <quotedCommand>,原样插入用 <command>
--pty 为命令执行分配伪终端(默认: true)
--pre-connect 启动时预连接所有配置的 SSH 服务器
--version, -v 打印包版本
--help 打印帮助信息

🛡️ 安全注意事项

该服务器提供了在远程服务器上执行命令和传输文件的强大功能。为确保安全使用,请注意以下几点:

  • 命令白名单强烈建议 使用 --whitelist 选项来限制可执行的命令集合。如果没有白名单,任何命令都可以在远程服务器上执行,这可能带来重大的安全风险。
  • 私钥安全:服务器会将 SSH 私钥读入内存。请确保运行 ssh-mcp-server 的机器是安全的。不要将服务器暴露给不受信任的网络。
  • 拒绝服务攻击 (DoS):服务器没有内置的速率限制。攻击者可能通过向服务器发送大量连接请求或大文件传输来发起 DoS 攻击。建议在具有速率限制功能的防火墙或反向代理后面运行服务器。
  • 路径遍历:服务器内置了对本地文件系统路径遍历攻击的保护。但是,仍然需要注意在 uploaddownload 命令中使用的路径。
  • 本地传输范围:默认仅允许访问当前工作目录。只有在明确可信时,才建议通过 --allowed-local-paths 或配置文件中的 allowedLocalPaths 放宽范围。
  • 远端传输范围:SFTP upload/download 仅接受绝对 POSIX 路径。未配置 allowedRemotePaths(或 --allowed-remote-paths)时,任意远端路径都允许,但启动时会打印警告。强烈建议显式配置 allowedRemotePaths 白名单,避免模型被 prompt 注入后读写 ~/.ssh/authorized_keys/etc/sshd_config 之类敏感文件。

🌟 Star 历史

Star History

Star History Chart

About

基于 SSH 的 MCP 服务 🧙‍♀️。已被MCP官方收录 🎉。 SSH MCP Server 🧙‍♀️. It has been included in the community MCP repository 🎉.

Resources

Stars

822 stars

Watchers

3 watching

Forks

Releases

Packages

Used by

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Repository files navigation

ssh-mcp-server logo

ssh-mcp-server

NPM VersionGitHub forksGitHub Repo starsGitHub Issues or Pull RequestsGitHub Issues or Pull RequestsGitHub Issues or Pull RequestsGitHub Issues or Pull Requests

基于 SSH 的 MCP (Model Context Protocol) 服务器,允许通过 MCP 协议远程执行 SSH 命令。

English Document | 中文文档

📝 项目介绍

ssh-mcp-server 是一个桥接工具,可以让 AI 助手等支持 MCP 协议的应用通过标准化接口执行远程 SSH 命令。这使得 AI 助手能够安全地操作远程服务器,执行命令并获取结果,而无需直接暴露 SSH 凭据给 AI 模型。

💬 如有任何问题,欢迎加入微信群交流:

wechat

✨ 功能亮点

  • 🔒 安全连接:支持多种安全的 SSH 连接方式,包括密码认证和私钥认证(支持带密码的私钥)
  • 🛡️ 命令安全控制:通过灵活的黑白名单机制,精确控制允许执行的命令范围,防止危险操作
  • 🔄 标准化接口:符合 MCP 协议规范,与支持该协议的 AI 助手无缝集成
  • 🚇 双传输模式:同时支持 execshell 两种 transport,兼容直连主机与堡垒机或跳板机场景
  • 📂 文件传输:支持双向文件传输功能,可上传本地文件到服务器或从服务器下载文件
  • 🔑 凭据隔离:SSH 凭据完全在本地管理,不会暴露给 AI 模型,增强安全性
  • 🚀 即用即走:使用 NPX 可直接运行,无需全局安装,方便快捷

📦 开源仓库

GitHub:https://github.com/classfang/ssh-mcp-server

NPM: https://www.npmjs.com/package/@fangjunjie/ssh-mcp-server

🛠️ 工具列表

工具名称描述
execute-command命令执行工具在远程服务器上执行 SSH 命令并获取执行结果
upload文件上传工具将本地文件上传到远程服务器指定位置
download文件下载工具从远程服务器下载文件到本地指定位置
list-servers服务器列表工具列出所有可用SSH服务器配置

📚 使用方法

0. 🤖 通过 AI Skill 快速配置(推荐)

如果你使用支持 skill 的 AI 编程助手(如 Claude Code),可以直接使用内置的 ssh-mcp-helper skill 通过交互式问答完成安装和配置,无需手动编辑 JSON 文件。

使用方式:

  1. 从本仓库 skills/ 目录安装该 skill
  2. 告诉你的 AI 助手:"帮我配置 ssh-mcp-server" 或 "给 Cursor 加一个 SSH MCP 连接"
  3. skill 会逐步引导你:检查 Node.js 环境 → 选择 MCP 客户端 → 选择认证方式 → 收集连接参数 → 生成并写入配置

该 skill 支持下文所有场景(账号密码、私钥、SSH config 复用、SOCKS 代理、堡垒机、多连接、2FA、命令限制等),并自动生成格式正确的配置。


下面的章节按从简单到复杂的顺序排列,最简单的入门方式就是用账号密码连接服务器。直接复制对应场景下的 mcp.json 配置到你的 MCP 客户端即可使用。

⚠️ 重要提示:在 MCP 配置文件中,每个命令行参数和其值必须是 args 数组中的独立元素。不要用空格将它们连接在一起。例如,使用 "--host", "192.168.1.1" 而不是 "--host 192.168.1.1"

1. 🔑 账号密码(最简单)

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "192.168.1.1",
"--port", "22",
"--username", "root",
"--password", "pwd123456"
]
}
}
}

2. 🔐 账号 + 私钥

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "192.168.1.1",
"--port", "22",
"--username", "root",
"--privateKey", "~/.ssh/id_rsa"
]
}
}
}

3. 🔏 带密码的私钥

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "192.168.1.1",
"--port", "22",
"--username", "root",
"--privateKey", "~/.ssh/id_rsa",
"--passphrase", "pwd123456"
]
}
}
}

4. 📋 复用 ~/.ssh/config

如果你已经在 ~/.ssh/config 配置了主机别名,服务器会自动从中读取连接参数,mcp.json 里就不用再写一遍。

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "myserver"
]
}
}
}

假设你的 ~/.ssh/config 包含:

Host myserver
HostName 192.168.1.1
Port 22
User root
IdentityFile ~/.ssh/id_rsa

你也可以指定自定义的 SSH 配置文件路径:

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "myserver",
"--ssh-config-file", "/path/to/custom/ssh_config"
]
}
}
}

注意:命令行参数优先级高于 SSH 配置值。例如,如果你指定了 --port 2222,它会覆盖 SSH 配置中的端口。

5. 🌐 通过代理连接

当目标主机只能通过代理访问时,可使用 --proxy 配置 SOCKS5、HTTP 或 HTTPS 代理。

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "192.168.1.1",
"--port", "22",
"--username", "root",
"--password", "pwd123456",
"--proxy", "http://username:password@proxy-host:proxy-port"
]
}
}
}

支持的 URL 格式:

socks://username:password@proxy-host:1080
socks5://username:password@proxy-host:1080
http://username:password@proxy-host:8080
https://username:password@proxy-host:8443

HTTP 和 HTTPS 代理通过 CONNECT 方法建立到 SSH 服务的隧道,用户名和密码使用 Basic 代理认证。HTTP、HTTPS 未填写端口时分别默认使用 80443;SOCKS5 必须填写端口。HTTPS 代理证书使用 Node.js 默认信任链进行验证。

原有 socksProxy 配置和 --socksProxy 参数继续兼容,但只接受 socks://socks5://。不要同时配置 proxysocksProxy

6. 📝 使用命令白名单 / 黑名单

通过 --whitelist--blacklist 限制服务器允许执行的命令范围。多个模式之间用逗号分隔,每个模式都是一个正则表达式。生产环境强烈建议配置

白名单示例(仅允许只读型查看命令):

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "192.168.1.1",
"--port", "22",
"--username", "root",
"--password", "pwd123456",
"--whitelist", "^ls( .*)?,^cat .*,^df.*"
]
}
}
}

黑名单示例(屏蔽危险命令):

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "192.168.1.1",
"--port", "22",
"--username", "root",
"--password", "pwd123456",
"--blacklist", "^rm .*,^shutdown.*,^reboot.*"
]
}
}
}

注意:如果同时指定了白名单和黑名单,系统会先检查命令是否在白名单中,再检查是否在黑名单中,命令必须同时通过两项检查才能被执行。

7. 🧩 使用命令模板包裹命令

commandTemplate 会把每条执行的命令套进一个模板里,适合切换用户(su)、放进容器、或经过跳板机的场景。当命令会作为 shell 参数传入时使用 <quotedCommand>,需要原样插入时使用 <command>;模板会在目录 cd 拼接之后应用,因此整个 cd ... && <实际命令> 都会被包裹起来。

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "10.0.0.1",
"--port", "22",
"--username", "deploy",
"--password", "xxx",
"--command-template", "su root -c <quotedCommand>"
]
}
}
}

当指定目录为 /data 执行 ls /app 时,实际发送的命令是:

su root -c 'cd -- '\''/data'\'' && ls /app'

其他常见模板:

sudo bash -c <quotedCommand>
docker exec -i mycontainer sh -c <quotedCommand>
ssh jumphost <quotedCommand>

8. 🚇 堡垒机 / 跳板机(transportMode: shell

transportMode 默认是 exec。出现下面这些情况时,应该切换到 shell

  • SSH 登录成功,但 exec 执行命令失败
  • 远端必须等登录 banner、profile、环境初始化完成后才能正常执行命令
  • 连接目标本质上是堡垒机或只暴露交互式 shell 的设备

两者差异:

  • exec:支持 execute-commanduploaddownload
  • shell:命令通过持久 shell 会话串行执行,内部带命令队列;但不支持upload / download,因为该模式下禁用了 SFTP
{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "bastion.example.com",
"--port", "22",
"--username", "ops",
"--password", "pwd123456",
"--transport-mode", "shell",
"--shell-ready-timeout", "15000"
]
}
}
}

JSON 配置文件中还可以通过 shellCommandTimeoutMs 覆盖 shell 模式下单条命令的默认超时。

9. 🔐 多因素认证(2FA / MFA)

当 SSH 服务器要求多因素认证(密码 + 私钥 + 2FA 验证码)时启用 tryKeyboard。密码和私钥会自动提供;对于非密码提示,请在连接前通过服务端环境变量 SSH_MCP_2FA_CODE 提供验证码。

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "example.com",
"--port", "22",
"--username", "user",
"--password", "your_password",
"--privateKey", "/path/to/key",
"--try-keyboard"
]
}
}
}

认证流程:

  1. 私钥认证(如果提供)
  2. 密码认证(如果提供)
  3. 键盘交互式认证通过 SSH_MCP_2FA_CODE 提供 2FA 验证码

10. 🧩 多 SSH 连接配置

需要在同一个 MCP server 里同时管理多个 SSH 目标时,给每个连接命名,调用时通过 connectionName 选择。共有三种配置方式:

📄 方式一:使用配置文件(推荐)

创建 JSON 配置文件(例如 ssh-config.json):

数组格式:

[
{
"name": "dev",
"host": "1.2.3.4",
"port": 22,
"username": "alice",
"password": "{abc=P100s0}",
"socksProxy": "socks://127.0.0.1:10808",
"commandTimeoutMs": 120000,
"maxOutputBytes": 10485760
},
{
"name": "bastion",
"host": "9.9.9.9",
"port": 22,
"username": "ops",
"password": "pwd123456",
"transportMode": "shell",
"shellReadyTimeoutMs": 15000,
"shellCommandTimeoutMs": 45000,
"connectionTimeoutMs": 30000,
"keepaliveIntervalMs": 10000,
"keepaliveCountMax": 3
},
{
"name": "prod",
"host": "5.6.7.8",
"port": 22,
"username": "bob",
"password": "yyy",
"socksProxy": "socks://127.0.0.1:10808"
},
{
"name": "secure-server",
"host": "secure.example.com",
"port": 22,
"username": "admin",
"password": "your_password",
"privateKey": "/path/to/private/key",
"tryKeyboard": true
}
]

对象格式:

{
"dev": {
"host": "1.2.3.4",
"port": 22,
"username": "alice",
"password": "{abc=P100s0}",
"socksProxy": "socks://127.0.0.1:10808",
"commandTimeoutMs": 120000,
"maxOutputBytes": 10485760
},
"bastion": {
"host": "9.9.9.9",
"port": 22,
"username": "ops",
"password": "pwd123456",
"transportMode": "shell",
"shellReadyTimeoutMs": 15000,
"shellCommandTimeoutMs": 45000
},
"prod": {
"host": "5.6.7.8",
"port": 22,
"username": "bob",
"password": "yyy",
"socksProxy": "socks://127.0.0.1:10808"
}
}

然后使用 --config-file 参数:

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--config-file", "ssh-config.json"
]
}
}
}

🔧 方式二:使用 JSON 格式的 --ssh 参数

可以直接传递 JSON 格式的配置字符串:

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--ssh", "{\"name\":\"dev\",\"host\":\"1.2.3.4\",\"port\":22,\"username\":\"alice\",\"password\":\"{abc=P100s0}\",\"socksProxy\":\"socks://127.0.0.1:10808\"}",
"--ssh", "{\"name\":\"bastion\",\"host\":\"9.9.9.9\",\"port\":22,\"username\":\"ops\",\"password\":\"pwd123456\",\"transportMode\":\"shell\",\"shellReadyTimeoutMs\":15000}",
"--ssh", "{\"name\":\"prod\",\"host\":\"5.6.7.8\",\"port\":22,\"username\":\"bob\",\"password\":\"yyy\",\"socksProxy\":\"socks://127.0.0.1:10808\"}"
]
}
}
}

📝 方式三:旧格式逗号分隔(向后兼容)

对于密码中不包含特殊字符的简单情况,仍可使用旧格式:

npx @fangjunjie/ssh-mcp-server \
--ssh "name=dev,host=1.2.3.4,port=22,user=alice,password=xxx" \
--ssh "name=prod,host=5.6.7.8,port=22,user=bob,password=yyy"

⚠️ 注意:旧格式在处理包含特殊字符(如 =,{})的密码时可能会有问题。如果密码包含特殊字符,请使用方式一或方式二。

在MCP工具调用时,通过 connectionName 参数指定目标连接名称,未指定时使用默认连接。

示例(在prod连接上执行命令):

{
"tool": "execute-command",
"params": {
"cmdString": "ls -al",
"connectionName": "prod"
}
}

示例(带超时选项的命令执行):

{
"tool": "execute-command",
"params": {
"cmdString": "ping -c 10 127.0.0.1",
"connectionName": "prod",
"timeout": 5000
}
}

⏱️ 命令执行超时

execute-command 工具支持超时选项,防止命令无限期挂起:

  • timeout: 单次调用的命令执行超时时间(毫秒,可选);传入时会覆盖连接配置,未传入时使用对应连接配置或其 30000ms 默认值
  • 在 JSON 配置文件里为单个连接设置 commandTimeoutMs,可以改掉这个默认值,避免每次调用都手动传 timeoutexec 模式)
  • shell 模式对应的配置项是 shellCommandTimeoutMs
  • 调用参数里的 timeout 始终优先于上面两个配置项
  • 连接默认启用 SSH keepalive(keepaliveIntervalMs: 10000,keepaliveCountMax: 3),并使用 connectionTimeoutMs 限制连接建立时间
  • SFTP 打开和传输操作使用 sftpTimeoutMs 控制超时(默认 300000ms)
  • 错误响应现在包含稳定的 codemessageretriable 字段,便于上层 Agent 处理

这对于像 pingtail -f 或其他可能阻塞执行的长时间运行进程特别有用。

📦 命令输出限制

会限制单条命令捕获的 stdoutstderr 总量,避免大文件或无限输出耗尽 MCP server 内存:

  • 在 JSON 连接配置中使用 maxOutputBytes 设置上限,默认值为 10485760(10 MiB)
  • maxOutputBytes 必须是非负整数;设置为 0 可禁用限制,但不建议对不受信任的命令禁用
  • 输出超过限制时,远端命令会被中止,工具返回 OUTPUT_LIMIT_EXCEEDED 错误和已经捕获的截断输出,不会把中止的命令误报为成功
  • ptyfalse 时,成功命令写入 stderr 的警告或进度信息会保留在 [stderr] 区段中
  • execshell 两种模式都会应用该限制。区别在于 exec 模式只关闭该命令的通道,而 shell 模式的通道由该连接上的所有命令共用、远端在中止后仍会继续写入,因此会断开连接(与 shell 模式命令超时的处理一致)

🗂️ 列出所有SSH服务器

可以通过MCP工具 list-servers 获取所有可用的SSH服务器配置:

调用示例:

{
"tool": "list-servers",
"params": {}
}

返回示例:

[
{ "name": "dev", "host": "1.2.3.4", "port": 22, "username": "alice" },
{ "name": "prod", "host": "5.6.7.8", "port": 22, "username": "bob" }
]

⚙️ 命令行选项参考

选项:
--config-file JSON 配置文件路径(推荐用于多服务器配置)
--ssh-config-file SSH 配置文件路径(默认: ~/.ssh/config)
--ssh SSH 连接配置(可以是 JSON 字符串或旧格式)
-h, --host SSH 服务器主机地址或 SSH 配置中的别名
-p, --port SSH 服务器端口
-u, --username SSH 用户名
-w, --password SSH 密码
-k, --privateKey SSH 私钥文件路径
-P, --passphrase 私钥密码(如果有的话)
-a, --agent SSH agent socket 路径
--try-keyboard 启用键盘交互式认证以支持 2FA/MFA(默认: false)
-W, --whitelist 命令白名单,以逗号分隔的正则表达式
-B, --blacklist 命令黑名单,以逗号分隔的正则表达式
--proxy 代理地址,支持 SOCKS5、HTTP 和 HTTPS
-s, --socksProxy 旧版 SOCKS5 代理地址(兼容参数)
--allowed-local-paths upload/download 允许访问的额外本地路径,逗号分隔
--allowed-remote-paths SFTP upload/download 允许访问的远端路径(POSIX 绝对路径),逗号分隔
--transport-mode SSH transport 模式: exec 或 shell(默认: exec)
--shell-ready-timeout shell 就绪探测超时,单位毫秒(默认: 10000)
--command-template 命令模板;shell 参数用 <quotedCommand>,原样插入用 <command>
--pty 为命令执行分配伪终端(默认: true)
--pre-connect 启动时预连接所有配置的 SSH 服务器
--version, -v 打印包版本
--help 打印帮助信息

🛡️ 安全注意事项

该服务器提供了在远程服务器上执行命令和传输文件的强大功能。为确保安全使用,请注意以下几点:

  • 命令白名单强烈建议 使用 --whitelist 选项来限制可执行的命令集合。如果没有白名单,任何命令都可以在远程服务器上执行,这可能带来重大的安全风险。
  • 私钥安全:服务器会将 SSH 私钥读入内存。请确保运行 ssh-mcp-server 的机器是安全的。不要将服务器暴露给不受信任的网络。
  • 拒绝服务攻击 (DoS):服务器没有内置的速率限制。攻击者可能通过向服务器发送大量连接请求或大文件传输来发起 DoS 攻击。建议在具有速率限制功能的防火墙或反向代理后面运行服务器。
  • 路径遍历:服务器内置了对本地文件系统路径遍历攻击的保护。但是,仍然需要注意在 uploaddownload 命令中使用的路径。
  • 本地传输范围:默认仅允许访问当前工作目录。只有在明确可信时,才建议通过 --allowed-local-paths 或配置文件中的 allowedLocalPaths 放宽范围。
  • 远端传输范围:SFTP upload/download 仅接受绝对 POSIX 路径。未配置 allowedRemotePaths(或 --allowed-remote-paths)时,任意远端路径都允许,但启动时会打印警告。强烈建议显式配置 allowedRemotePaths 白名单,避免模型被 prompt 注入后读写 ~/.ssh/authorized_keys/etc/sshd_config 之类敏感文件。

🌟 Star 历史

Star History

Star History Chart

About

基于 SSH 的 MCP 服务 🧙‍♀️。已被MCP官方收录 🎉。 SSH MCP Server 🧙‍♀️. It has been included in the community MCP repository 🎉.

Resources

Stars

822 stars

Watchers

3 watching

Forks

Releases

Packages

Used by

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content

Repository files navigation

ssh-mcp-server logo

ssh-mcp-server

NPM VersionGitHub forksGitHub Repo starsGitHub Issues or Pull RequestsGitHub Issues or Pull RequestsGitHub Issues or Pull RequestsGitHub Issues or Pull Requests

基于 SSH 的 MCP (Model Context Protocol) 服务器,允许通过 MCP 协议远程执行 SSH 命令。

English Document | 中文文档

📝 项目介绍

ssh-mcp-server 是一个桥接工具,可以让 AI 助手等支持 MCP 协议的应用通过标准化接口执行远程 SSH 命令。这使得 AI 助手能够安全地操作远程服务器,执行命令并获取结果,而无需直接暴露 SSH 凭据给 AI 模型。

💬 如有任何问题,欢迎加入微信群交流:

wechat

✨ 功能亮点

  • 🔒 安全连接:支持多种安全的 SSH 连接方式,包括密码认证和私钥认证(支持带密码的私钥)
  • 🛡️ 命令安全控制:通过灵活的黑白名单机制,精确控制允许执行的命令范围,防止危险操作
  • 🔄 标准化接口:符合 MCP 协议规范,与支持该协议的 AI 助手无缝集成
  • 🚇 双传输模式:同时支持 execshell 两种 transport,兼容直连主机与堡垒机或跳板机场景
  • 📂 文件传输:支持双向文件传输功能,可上传本地文件到服务器或从服务器下载文件
  • 🔑 凭据隔离:SSH 凭据完全在本地管理,不会暴露给 AI 模型,增强安全性
  • 🚀 即用即走:使用 NPX 可直接运行,无需全局安装,方便快捷

📦 开源仓库

GitHub:https://github.com/classfang/ssh-mcp-server

NPM: https://www.npmjs.com/package/@fangjunjie/ssh-mcp-server

🛠️ 工具列表

工具名称描述
execute-command命令执行工具在远程服务器上执行 SSH 命令并获取执行结果
upload文件上传工具将本地文件上传到远程服务器指定位置
download文件下载工具从远程服务器下载文件到本地指定位置
list-servers服务器列表工具列出所有可用SSH服务器配置

📚 使用方法

0. 🤖 通过 AI Skill 快速配置(推荐)

如果你使用支持 skill 的 AI 编程助手(如 Claude Code),可以直接使用内置的 ssh-mcp-helper skill 通过交互式问答完成安装和配置,无需手动编辑 JSON 文件。

使用方式:

  1. 从本仓库 skills/ 目录安装该 skill
  2. 告诉你的 AI 助手:"帮我配置 ssh-mcp-server" 或 "给 Cursor 加一个 SSH MCP 连接"
  3. skill 会逐步引导你:检查 Node.js 环境 → 选择 MCP 客户端 → 选择认证方式 → 收集连接参数 → 生成并写入配置

该 skill 支持下文所有场景(账号密码、私钥、SSH config 复用、SOCKS 代理、堡垒机、多连接、2FA、命令限制等),并自动生成格式正确的配置。


下面的章节按从简单到复杂的顺序排列,最简单的入门方式就是用账号密码连接服务器。直接复制对应场景下的 mcp.json 配置到你的 MCP 客户端即可使用。

⚠️ 重要提示:在 MCP 配置文件中,每个命令行参数和其值必须是 args 数组中的独立元素。不要用空格将它们连接在一起。例如,使用 "--host", "192.168.1.1" 而不是 "--host 192.168.1.1"

1. 🔑 账号密码(最简单)

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "192.168.1.1",
"--port", "22",
"--username", "root",
"--password", "pwd123456"
]
}
}
}

2. 🔐 账号 + 私钥

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "192.168.1.1",
"--port", "22",
"--username", "root",
"--privateKey", "~/.ssh/id_rsa"
]
}
}
}

3. 🔏 带密码的私钥

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "192.168.1.1",
"--port", "22",
"--username", "root",
"--privateKey", "~/.ssh/id_rsa",
"--passphrase", "pwd123456"
]
}
}
}

4. 📋 复用 ~/.ssh/config

如果你已经在 ~/.ssh/config 配置了主机别名,服务器会自动从中读取连接参数,mcp.json 里就不用再写一遍。

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "myserver"
]
}
}
}

假设你的 ~/.ssh/config 包含:

Host myserver
HostName 192.168.1.1
Port 22
User root
IdentityFile ~/.ssh/id_rsa

你也可以指定自定义的 SSH 配置文件路径:

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "myserver",
"--ssh-config-file", "/path/to/custom/ssh_config"
]
}
}
}

注意:命令行参数优先级高于 SSH 配置值。例如,如果你指定了 --port 2222,它会覆盖 SSH 配置中的端口。

5. 🌐 通过代理连接

当目标主机只能通过代理访问时,可使用 --proxy 配置 SOCKS5、HTTP 或 HTTPS 代理。

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "192.168.1.1",
"--port", "22",
"--username", "root",
"--password", "pwd123456",
"--proxy", "http://username:password@proxy-host:proxy-port"
]
}
}
}

支持的 URL 格式:

socks://username:password@proxy-host:1080
socks5://username:password@proxy-host:1080
http://username:password@proxy-host:8080
https://username:password@proxy-host:8443

HTTP 和 HTTPS 代理通过 CONNECT 方法建立到 SSH 服务的隧道,用户名和密码使用 Basic 代理认证。HTTP、HTTPS 未填写端口时分别默认使用 80443;SOCKS5 必须填写端口。HTTPS 代理证书使用 Node.js 默认信任链进行验证。

原有 socksProxy 配置和 --socksProxy 参数继续兼容,但只接受 socks://socks5://。不要同时配置 proxysocksProxy

6. 📝 使用命令白名单 / 黑名单

通过 --whitelist--blacklist 限制服务器允许执行的命令范围。多个模式之间用逗号分隔,每个模式都是一个正则表达式。生产环境强烈建议配置

白名单示例(仅允许只读型查看命令):

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "192.168.1.1",
"--port", "22",
"--username", "root",
"--password", "pwd123456",
"--whitelist", "^ls( .*)?,^cat .*,^df.*"
]
}
}
}

黑名单示例(屏蔽危险命令):

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "192.168.1.1",
"--port", "22",
"--username", "root",
"--password", "pwd123456",
"--blacklist", "^rm .*,^shutdown.*,^reboot.*"
]
}
}
}

注意:如果同时指定了白名单和黑名单,系统会先检查命令是否在白名单中,再检查是否在黑名单中,命令必须同时通过两项检查才能被执行。

7. 🧩 使用命令模板包裹命令

commandTemplate 会把每条执行的命令套进一个模板里,适合切换用户(su)、放进容器、或经过跳板机的场景。当命令会作为 shell 参数传入时使用 <quotedCommand>,需要原样插入时使用 <command>;模板会在目录 cd 拼接之后应用,因此整个 cd ... && <实际命令> 都会被包裹起来。

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "10.0.0.1",
"--port", "22",
"--username", "deploy",
"--password", "xxx",
"--command-template", "su root -c <quotedCommand>"
]
}
}
}

当指定目录为 /data 执行 ls /app 时,实际发送的命令是:

su root -c 'cd -- '\''/data'\'' && ls /app'

其他常见模板:

sudo bash -c <quotedCommand>
docker exec -i mycontainer sh -c <quotedCommand>
ssh jumphost <quotedCommand>

8. 🚇 堡垒机 / 跳板机(transportMode: shell

transportMode 默认是 exec。出现下面这些情况时,应该切换到 shell

  • SSH 登录成功,但 exec 执行命令失败
  • 远端必须等登录 banner、profile、环境初始化完成后才能正常执行命令
  • 连接目标本质上是堡垒机或只暴露交互式 shell 的设备

两者差异:

  • exec:支持 execute-commanduploaddownload
  • shell:命令通过持久 shell 会话串行执行,内部带命令队列;但不支持upload / download,因为该模式下禁用了 SFTP
{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "bastion.example.com",
"--port", "22",
"--username", "ops",
"--password", "pwd123456",
"--transport-mode", "shell",
"--shell-ready-timeout", "15000"
]
}
}
}

JSON 配置文件中还可以通过 shellCommandTimeoutMs 覆盖 shell 模式下单条命令的默认超时。

9. 🔐 多因素认证(2FA / MFA)

当 SSH 服务器要求多因素认证(密码 + 私钥 + 2FA 验证码)时启用 tryKeyboard。密码和私钥会自动提供;对于非密码提示,请在连接前通过服务端环境变量 SSH_MCP_2FA_CODE 提供验证码。

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "example.com",
"--port", "22",
"--username", "user",
"--password", "your_password",
"--privateKey", "/path/to/key",
"--try-keyboard"
]
}
}
}

认证流程:

  1. 私钥认证(如果提供)
  2. 密码认证(如果提供)
  3. 键盘交互式认证通过 SSH_MCP_2FA_CODE 提供 2FA 验证码

10. 🧩 多 SSH 连接配置

需要在同一个 MCP server 里同时管理多个 SSH 目标时,给每个连接命名,调用时通过 connectionName 选择。共有三种配置方式:

📄 方式一:使用配置文件(推荐)

创建 JSON 配置文件(例如 ssh-config.json):

数组格式:

[
{
"name": "dev",
"host": "1.2.3.4",
"port": 22,
"username": "alice",
"password": "{abc=P100s0}",
"socksProxy": "socks://127.0.0.1:10808",
"commandTimeoutMs": 120000,
"maxOutputBytes": 10485760
},
{
"name": "bastion",
"host": "9.9.9.9",
"port": 22,
"username": "ops",
"password": "pwd123456",
"transportMode": "shell",
"shellReadyTimeoutMs": 15000,
"shellCommandTimeoutMs": 45000,
"connectionTimeoutMs": 30000,
"keepaliveIntervalMs": 10000,
"keepaliveCountMax": 3
},
{
"name": "prod",
"host": "5.6.7.8",
"port": 22,
"username": "bob",
"password": "yyy",
"socksProxy": "socks://127.0.0.1:10808"
},
{
"name": "secure-server",
"host": "secure.example.com",
"port": 22,
"username": "admin",
"password": "your_password",
"privateKey": "/path/to/private/key",
"tryKeyboard": true
}
]

对象格式:

{
"dev": {
"host": "1.2.3.4",
"port": 22,
"username": "alice",
"password": "{abc=P100s0}",
"socksProxy": "socks://127.0.0.1:10808",
"commandTimeoutMs": 120000,
"maxOutputBytes": 10485760
},
"bastion": {
"host": "9.9.9.9",
"port": 22,
"username": "ops",
"password": "pwd123456",
"transportMode": "shell",
"shellReadyTimeoutMs": 15000,
"shellCommandTimeoutMs": 45000
},
"prod": {
"host": "5.6.7.8",
"port": 22,
"username": "bob",
"password": "yyy",
"socksProxy": "socks://127.0.0.1:10808"
}
}

然后使用 --config-file 参数:

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--config-file", "ssh-config.json"
]
}
}
}

🔧 方式二:使用 JSON 格式的 --ssh 参数

可以直接传递 JSON 格式的配置字符串:

{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--ssh", "{\"name\":\"dev\",\"host\":\"1.2.3.4\",\"port\":22,\"username\":\"alice\",\"password\":\"{abc=P100s0}\",\"socksProxy\":\"socks://127.0.0.1:10808\"}",
"--ssh", "{\"name\":\"bastion\",\"host\":\"9.9.9.9\",\"port\":22,\"username\":\"ops\",\"password\":\"pwd123456\",\"transportMode\":\"shell\",\"shellReadyTimeoutMs\":15000}",
"--ssh", "{\"name\":\"prod\",\"host\":\"5.6.7.8\",\"port\":22,\"username\":\"bob\",\"password\":\"yyy\",\"socksProxy\":\"socks://127.0.0.1:10808\"}"
]
}
}
}

📝 方式三:旧格式逗号分隔(向后兼容)

对于密码中不包含特殊字符的简单情况,仍可使用旧格式:

npx @fangjunjie/ssh-mcp-server \
--ssh "name=dev,host=1.2.3.4,port=22,user=alice,password=xxx" \
--ssh "name=prod,host=5.6.7.8,port=22,user=bob,password=yyy"

⚠️ 注意:旧格式在处理包含特殊字符(如 =,{})的密码时可能会有问题。如果密码包含特殊字符,请使用方式一或方式二。

在MCP工具调用时,通过 connectionName 参数指定目标连接名称,未指定时使用默认连接。

示例(在prod连接上执行命令):

{
"tool": "execute-command",
"params": {
"cmdString": "ls -al",
"connectionName": "prod"
}
}

示例(带超时选项的命令执行):

{
"tool": "execute-command",
"params": {
"cmdString": "ping -c 10 127.0.0.1",
"connectionName": "prod",
"timeout": 5000
}
}

⏱️ 命令执行超时

execute-command 工具支持超时选项,防止命令无限期挂起:

  • timeout: 单次调用的命令执行超时时间(毫秒,可选);传入时会覆盖连接配置,未传入时使用对应连接配置或其 30000ms 默认值
  • 在 JSON 配置文件里为单个连接设置 commandTimeoutMs,可以改掉这个默认值,避免每次调用都手动传 timeoutexec 模式)
  • shell 模式对应的配置项是 shellCommandTimeoutMs
  • 调用参数里的 timeout 始终优先于上面两个配置项
  • 连接默认启用 SSH keepalive(keepaliveIntervalMs: 10000,keepaliveCountMax: 3),并使用 connectionTimeoutMs 限制连接建立时间
  • SFTP 打开和传输操作使用 sftpTimeoutMs 控制超时(默认 300000ms)
  • 错误响应现在包含稳定的 codemessageretriable 字段,便于上层 Agent 处理

这对于像 pingtail -f 或其他可能阻塞执行的长时间运行进程特别有用。

📦 命令输出限制

会限制单条命令捕获的 stdoutstderr 总量,避免大文件或无限输出耗尽 MCP server 内存:

  • 在 JSON 连接配置中使用 maxOutputBytes 设置上限,默认值为 10485760(10 MiB)
  • maxOutputBytes 必须是非负整数;设置为 0 可禁用限制,但不建议对不受信任的命令禁用
  • 输出超过限制时,远端命令会被中止,工具返回 OUTPUT_LIMIT_EXCEEDED 错误和已经捕获的截断输出,不会把中止的命令误报为成功
  • ptyfalse 时,成功命令写入 stderr 的警告或进度信息会保留在 [stderr] 区段中
  • execshell 两种模式都会应用该限制。区别在于 exec 模式只关闭该命令的通道,而 shell 模式的通道由该连接上的所有命令共用、远端在中止后仍会继续写入,因此会断开连接(与 shell 模式命令超时的处理一致)

🗂️ 列出所有SSH服务器

可以通过MCP工具 list-servers 获取所有可用的SSH服务器配置:

调用示例:

{
"tool": "list-servers",
"params": {}
}

返回示例:

[
{ "name": "dev", "host": "1.2.3.4", "port": 22, "username": "alice" },
{ "name": "prod", "host": "5.6.7.8", "port": 22, "username": "bob" }
]

⚙️ 命令行选项参考

选项:
--config-file JSON 配置文件路径(推荐用于多服务器配置)
--ssh-config-file SSH 配置文件路径(默认: ~/.ssh/config)
--ssh SSH 连接配置(可以是 JSON 字符串或旧格式)
-h, --host SSH 服务器主机地址或 SSH 配置中的别名
-p, --port SSH 服务器端口
-u, --username SSH 用户名
-w, --password SSH 密码
-k, --privateKey SSH 私钥文件路径
-P, --passphrase 私钥密码(如果有的话)
-a, --agent SSH agent socket 路径
--try-keyboard 启用键盘交互式认证以支持 2FA/MFA(默认: false)
-W, --whitelist 命令白名单,以逗号分隔的正则表达式
-B, --blacklist 命令黑名单,以逗号分隔的正则表达式
--proxy 代理地址,支持 SOCKS5、HTTP 和 HTTPS
-s, --socksProxy 旧版 SOCKS5 代理地址(兼容参数)
--allowed-local-paths upload/download 允许访问的额外本地路径,逗号分隔
--allowed-remote-paths SFTP upload/download 允许访问的远端路径(POSIX 绝对路径),逗号分隔
--transport-mode SSH transport 模式: exec 或 shell(默认: exec)
--shell-ready-timeout shell 就绪探测超时,单位毫秒(默认: 10000)
--command-template 命令模板;shell 参数用 <quotedCommand>,原样插入用 <command>
--pty 为命令执行分配伪终端(默认: true)
--pre-connect 启动时预连接所有配置的 SSH 服务器
--version, -v 打印包版本
--help 打印帮助信息

🛡️ 安全注意事项

该服务器提供了在远程服务器上执行命令和传输文件的强大功能。为确保安全使用,请注意以下几点:

  • 命令白名单强烈建议 使用 --whitelist 选项来限制可执行的命令集合。如果没有白名单,任何命令都可以在远程服务器上执行,这可能带来重大的安全风险。
  • 私钥安全:服务器会将 SSH 私钥读入内存。请确保运行 ssh-mcp-server 的机器是安全的。不要将服务器暴露给不受信任的网络。
  • 拒绝服务攻击 (DoS):服务器没有内置的速率限制。攻击者可能通过向服务器发送大量连接请求或大文件传输来发起 DoS 攻击。建议在具有速率限制功能的防火墙或反向代理后面运行服务器。
  • 路径遍历:服务器内置了对本地文件系统路径遍历攻击的保护。但是,仍然需要注意在 uploaddownload 命令中使用的路径。
  • 本地传输范围:默认仅允许访问当前工作目录。只有在明确可信时,才建议通过 --allowed-local-paths 或配置文件中的 allowedLocalPaths 放宽范围。
  • 远端传输范围:SFTP upload/download 仅接受绝对 POSIX 路径。未配置 allowedRemotePaths(或 --allowed-remote-paths)时,任意远端路径都允许,但启动时会打印警告。强烈建议显式配置 allowedRemotePaths 白名单,避免模型被 prompt 注入后读写 ~/.ssh/authorized_keys/etc/sshd_config 之类敏感文件。

🌟 Star 历史

Star History

Star History Chart

About

基于 SSH 的 MCP 服务 🧙‍♀️。已被MCP官方收录 🎉。 SSH MCP Server 🧙‍♀️. It has been included in the community MCP repository 🎉.

Resources

Stars

822 stars

Watchers

3 watching

Forks

Releases

Packages

Used by

Contributors

Languages