内容
# MCP VPS Initialize
一个用于自动化 VPS 初始化和管理的模型上下文协议 (MCP) 服务器。该工具提供基于 SSH 的自动化功能,用于使用常用服务和 CI/CD 管道设置新的 VPS 实例。
## 功能
- 🔐 **SSH Authentication**: 支持密码和私钥身份验证
- 🛠️ **VPS Initialization**: 自动设置 Node.js, PM2, Rust, Nginx, Redis
- 🌐 **Nginx Configuration**: 使用反向代理和 SSL 证书进行域名设置
- 🚀 **GitHub CI/CD**: 自动生成部署密钥和工作流程
- 📝 **Comprehensive Logging**: 基于 Winston 的结构化日志记录
- 🔧 **TypeScript**: 完全类型化,具有严格的 TypeScript 配置
## 安装
```bash
# Clone the repository
git clone <repository-url>
cd mcp-vps-initialize
# Install dependencies
npm install
# Build the project
npm run build
# Start the server
npm start
```
## 开发
```bash
# Run in development mode
npm run dev
# Lint code
npm run lint
npm run lint:fix
# Format code
npm run format
npm run format:check
```
## MCP Tools
### 1. SSH Connect
通过 SSH 使用密码或私钥身份验证连接到 VPS。
**Parameters:**
- `host` (required): VPS IP 地址或主机名
- `username` (required): SSH 用户名
- `port` (optional): SSH 端口(默认值:22)
- `password` (optional): SSH 密码
- `privateKeyPath` (optional): 私钥文件路径
- `passphrase` (optional): 私钥密码
**Example:**
```json
{
"host": "192.168.1.100",
"username": "root",
"password": "your-password"
}
```
### 2. VPS Initialize
使用系统更新和可选服务初始化新的 VPS。
**Parameters:**
- `services` (optional): 指定要安装哪些服务的对象
- `nodejs` (boolean): 安装 Node.js LTS
- `pm2` (boolean): 安装 PM2 进程管理器
- `rust` (boolean): 安装 Rust 工具链
- `nginx` (boolean): 安装和配置 Nginx
- `redis` (boolean): 安装和配置 Redis
**Example:**
```json
{
"services": {
"nodejs": true,
"pm2": true,
"nginx": true,
"redis": false
}
}
```
### 3. Nginx Setup
使用域名、反向代理和 SSL 证书配置 Nginx。
**Parameters:**
- `domain` (required): 站点的域名
- `port` (required): 将请求代理到的后端端口
- `ssl` (optional): 使用 Certbot 启用 SSL(默认值:true)
**Example:**
```json
{
"domain": "example.com",
"port": 3000,
"ssl": true
}
```
### 4. GitHub CI/CD Setup
使用部署密钥和自动化工作流程设置 GitHub CI/CD。
**Parameters:**
- `repoUrl` (required): GitHub 仓库 URL
- `deployPath` (required): 服务器上的部署路径
**Example:**
```json
{
"repoUrl": "https://github.com/username/repo.git",
"deployPath": "/opt/deployments/myapp"
}
```
### 5. Execute Command
在连接的 VPS 上执行任意命令。
**Parameters:**
- `command` (required): 要执行的命令
**Example:**
```json
{
"command": "systemctl status nginx"
}
```
## Workflow
### Basic VPS Setup
1. **Connect to VPS:**
```json
{
"tool": "ssh_connect",
"args": {
"host": "your-vps-ip",
"username": "root",
"password": "your-password"
}
}
```
2. **Initialize VPS:**
```json
{
"tool": "vps_initialize",
"args": {
"services": {
"nodejs": true,
"pm2": true,
"nginx": true
}
}
}
```
3. **Setup Nginx:**
```json
{
"tool": "nginx_setup",
"args": {
"domain": "yourdomain.com",
"port": 3000,
"ssl": true
}
}
```
4. **Setup CI/CD:**
```json
{
"tool": "github_cicd_setup",
"args": {
"repoUrl": "https://github.com/username/repo.git",
"deployPath": "/opt/deployments/myapp"
}
}
```
### GitHub CI/CD Instructions
运行 `github_cicd_setup` 工具后,您将收到:
1. **Deploy Key**: 添加到您的 GitHub 仓库的 Deploy Keys
2. **GitHub Secrets**: 添加到您的仓库的 Actions secrets
3. **Workflow File**: 添加到 `.github/workflows/deploy.yml`
生成的工作流程将:
- 在推送到 main/master 分支时触发
- 运行测试(如果可用)
- 构建应用程序(如果存在构建脚本)
- 自动部署到您的 VPS
## Security Features
- **Firewall Configuration**: 具有安全默认设置的自动 UFW 设置
- **SSL Certificates**: 自动生成 Let's Encrypt 证书
- **SSH Key Management**: 安全的部署密钥生成
- **Process Isolation**: 可选的部署用户创建
## Logging
日志写入到:
- `logs/combined.log`: 所有日志级别
- `logs/error.log`: 仅错误日志
- Console: 带有颜色的格式化输出
日志级别:`error`, `warn`, `info`, `debug`
## Configuration
可以通过环境变量配置服务器:
- `LOG_LEVEL`: 设置日志记录级别(默认值:'info')
- `PORT`: 服务器端口(如果独立运行)
## Troubleshooting
### Common Issues
1. **SSH Connection Failed**
- 验证 IP 地址和凭据
- 检查防火墙设置
- 确保 SSH 服务正在运行
2. **SSL Certificate Issues**
- 验证域名 DNS 指向服务器
- 检查防火墙是否允许 HTTP/HTTPS
- 确保电子邮件对于 Let's Encrypt 有效
3. **Service Installation Failed**
- 检查系统更新状态
- 验证互联网连接
- 查看错误日志
### Debug Mode
启用调试日志记录:
```bash
LOG_LEVEL=debug npm start
```
## Requirements
- Node.js 18+
- Fresh Ubuntu/Debian VPS
- 域名(用于 SSL 设置)
- GitHub 仓库(用于 CI/CD)
## License
MIT License - 有关详细信息,请参见 LICENSE 文件。