SSH/SFTP远程执行工具

一个基于Rust的MCP服务器,用于通过SSH/SFTP执行远程命令和文件传输,使AI代理能够像执行本地命令一样操作远程主机。

  • by Brainwires
  • Rust
  • 本地部署
  • 1 星标
  • 更新于
在 GitHub 查看 Brainwires/mcp-secure-shell

使用说明

mcp安全外壳

用于SSH/SFTP远程shell执行和文件传输的Rust MCP(模型上下文协议)服务器。这允许AI代理在远程主机上执行命令,就像执行本地shell命令一样简单。

特性

  • SSH命令执行:通过SSH在远程主机上执行shell命令
  • SFTP文件传输:将文件上传到远程主机或从远程主机下载文件
  • SSH配置支持:使用您现有的 ~/.ssh/config 主机配置文件
  • 基于密钥的身份验证:支持基于SSH密钥的身份验证(无密码身份验证)
  • 连接池:保持连接,以便高效重复操作

先决条件

  1. SSH密钥设置:确保为目标主机配置了基于SSH密钥的身份验证
  2. SSH配置:在中配置您的主机 ~/.ssh/config (可选但推荐)
  3. SFTP已启用:对于文件传输,请确保在远程服务器上启用了SFTP(Subsystem sftp internal-sftp 在sshd_config中)

安装

cargo install --path .

或者从源代码构建:

cargo build --release

二进制文件将位于 target/release/mcp-secure-shell.

MCP服务器配置

添加到您的MCP客户端配置中(例如,Claude Desktop):

{
  "mcpServers": {
    "ssh": {
      "command": "/path/to/mcp-secure-shell",
      "args": []
    }
  }
}

可用工具

ssh_exec

在远程主机上执行shell命令。

参数:

  • host (必需):SSH主机别名来自 ~/.ssh/config 或直接主机名
  • command (必需):要执行的Shell命令
  • cwd (可选):运行命令的工作目录

例子:

{
  "host": "myserver",
  "command": "ls -la /var/log",
  "cwd": "/home/user"
}

sftp_upload

将本地文件上载到远程主机。

参数:

  • host (必需):SSH主机别名或主机名
  • local_path (必需):本地文件的路径
  • remote_path (必需):远程主机上的目标路径

例子:

{
  "host": "myserver",
  "local_path": "/tmp/myfile.txt",
  "remote_path": "/home/user/myfile.txt"
}

sftp_下载

从远程主机下载文件。

参数:

  • host (必需):SSH主机别名或主机名
  • remote_path (必需):远程主机上文件的路径
  • local_path (必需):本地计算机上的目标路径

例子:

{
  "host": "myserver",
  "remote_path": "/var/log/app.log",
  "local_path": "/tmp/app.log"
}

ssh-list_hosts

列出中配置的所有SSH主机 ~/.ssh/config.

参数: 无

ssh_host_info

获取特定SSH主机的连接详细信息。

参数:

  • host (必需):获取SSH主机别名的信息

SSH配置示例

# ~/.ssh/config

Host myserver
    HostName 192.168.1.100
    User admin
    Port 22
    IdentityFile ~/.ssh/id_ed25519

Host devbox
    HostName dev.example.com
    User developer
    IdentityFile ~/.ssh/id_rsa

环境变量

  • RUST_LOG:设置日志记录级别(例如。, RUST_LOG=mcp_secure_shell=debug)

安全考虑

  • 此服务器使用配置的SSH用户的权限在远程主机上执行命令
  • 仅支持基于密钥的身份验证(不支持密码身份验证)
  • 验证主机密钥 ~/.ssh/known_hosts 默认情况下
  • 请注意配置和访问哪些主机
  • 连接超时设置为30秒,以防止挂起

发展

建筑

cargo build

运行测试

单元测试:

cargo test

集成测试 (需要Docker):

  1. 启动测试SSHD容器:

    ./scripts/test-sshd.sh start
    
  2. 运行集成测试:

    cargo test --test integration_test
    
  3. 完成后停止容器:

    ./scripts/test-sshd.sh stop
    

测试容器

该项目包括一个基于Docker的测试环境,使用 帕努博/sshd,一个带有OpenSSH的最小Alpine Linux映像。

集装箱管理:

./scripts/test-sshd.sh start    # Start container
./scripts/test-sshd.sh stop     # Stop container
./scripts/test-sshd.sh status   # Check status
./scripts/test-sshd.sh test     # Test SSH connection
./scripts/test-sshd.sh logs     # View logs
./scripts/test-sshd.sh shell    # Open shell in container

环境变量:

  • SSH_PORT:要打开SSHD的端口(默认值:2222)
  • TEST_SSH_HOST:集成测试主机(默认:localhost)
  • TEST_SSH_PORT:集成测试端口(默认值:2222)
  • TEST_SSH_USER:集成测试用户(默认:root)

Docker Compose替代方案:

docker compose -f docker-compose.test.yml up -d

添加到SSH配置以进行手动测试

启动测试容器后,将其添加到 ~/.ssh/config:

Host test-container
    HostName localhost
    Port 2222
    User root
    StrictHostKeyChecking no
    UserKnownHostsFile /dev/null

然后使用以下工具测试MCP服务器 test-container 主持人。

许可证

麻省理工学院