SSH远程服务器管理工具

一个通过SSH执行命令和管理远程服务器文件的模型上下文协议(MCP)服务器,支持自动发现BinaryLane VPS服务器。

  • by termau
  • TypeScript
  • 本地部署
  • 2 星标
  • 更新于
在 GitHub 查看 termau/ssh-mcp

使用说明

SSH MCP服务器

一种模型上下文协议(MCP)服务器,用于通过SSH在远程服务器上执行命令和管理文件。

可选自动发现 BinaryLane VPS服务器,因此可以立即使用。

特性

  • 在远程服务器上执行shell命令
  • 远程读取文件和列出目录
  • 通过SFTP上传和下载文件
  • 重新启动时将持久连接保存到配置文件
  • BinaryLane自动发现活动服务器
  • 每个连接的SSH密钥配置
  • 具有明确优先级的多个连接源

安装

npm install
npm run build

配置

克劳德桌面/克劳德代码

添加到您的Claude配置中:

{
  "mcpServers": {
    "ssh": {
      "command": "node",
      "args": ["/path/to/ssh-mcp/dist/index.js"],
      "env": {
        "BINARYLANE_API_TOKEN": "your-token-here"
      }
    }
  }
}

这 BINARYLANE_API_TOKEN 是可选的--仅用于自动发现BinaryLane服务器。

配置文件

首次运行时,会在以下位置创建一个配置文件 ~/.config/ssh-mcp/connections.json:

{
  "defaultPrivateKeyPath": "~/.ssh/id_ed25519",
  "binarylane": {
    "enabled": true,
    "apiToken": "your-token-here",
    "defaultUsername": "root",
    "defaultPrivateKeyPath": "~/.ssh/id_ed25519"
  },
  "connections": [
    {
      "name": "my-server",
      "host": "10.0.0.5",
      "port": 22,
      "username": "deploy",
      "privateKeyPath": "~/.ssh/deploy_key"
    }
  ]
}

BinaryLane API令牌可以在此处设置,也可以设置为环境变量。

环境变量(遗留)

仍然支持向后兼容性:

export SSH_CONNECTIONS='[
  {"name": "web1", "host": "192.168.1.10", "port": 22, "username": "root", "privateKeyPath": "~/.ssh/id_rsa"}
]'

连接选项

选项 必填 默认 说明
name 是 - 连接的唯一标识符
host 是 - 服务器主机名或IP地址
port 无 22 SSH端口
username 是 - SSH用户名
privateKeyPath 无 - SSH私钥路径(支持 ~ 扩展)
password 无 - SSH密码(如果不使用密钥认证)

如果两者都没有 privateKeyPath 也不 password 如果提供了,服务器将尝试默认的SSH密钥位置(~/.ssh/id_ed25519, ~/.ssh/id_rsa).

连接优先级

当多个源定义相同的连接名称时:

  1. 配置文件 (最高)——手动连接 ~/.config/ssh-mcp/connections.json
  2. 环境变量 — SSH_CONNECTIONS
  3. BinaryLane自动发现 (最低)-API的活动服务器

可用工具

连接管理

  • list_connections --列出所有可用的SSH连接及其源信息
  • test_connection --使用延迟报告测试连接性
  • add_connection --添加连接(持久化到配置文件中)
  • remove_connection --从配置中删除手动连接
  • refresh_connections --重新发现BinaryLane服务器并重新加载所有连接
  • get_config --显示当前配置(标记已编辑)

命令执行

  • run_command --执行具有可配置超时的shell命令

文件操作

  • read_remote_file --通过SFTP读取文件内容
  • list_remote_directory --列出包含元数据的目录内容
  • upload_file --将本地文件上载到远程服务器
  • download_file --将远程文件下载到本地计算机

示例用法

# List all connections (shows source: config, env, or binarylane)
list_connections

# Test connection
test_connection connection="web1"

# Run a command
run_command connection="web1" command="uptime"

# Add a new persistent connection
add_connection name="staging" host="10.0.0.5" username="deploy" privateKeyPath="~/.ssh/deploy_key"

# Refresh after creating new BinaryLane servers
refresh_connections

安全须知

  • SSH密钥从本地文件系统读取,MCP从不传输或存储
  • 配置中的密码仅在操作期间保存在内存中
  • 配置文件应使用适当的文件系统权限进行保护
  • 命令以SSH用户的权限执行
  • 考虑使用具有有限权限的专用SSH密钥进行自动化

发展

npm run dev    # Watch mode
npm run build  # Compile TypeScript
npm start      # Run compiled server

项目结构

ssh-mcp/
├── src/
│   ├── index.ts        # Server entry point
│   ├── ssh-client.ts   # SSH connection manager
│   ├── config.ts       # Persistent config file management
│   ├── binarylane.ts   # BinaryLane auto-discovery
│   ├── tools.ts        # MCP tool definitions
│   └── handlers.ts     # Tool handler implementations
├── dist/               # Compiled JavaScript
├── package.json
└── tsconfig.json

许可证

麻省理工学院