内容
# Vast.ai MCP 服务器
Vast.ai **模型上下文协议(MCP)** 服务器,用于 [Vast.ai](https://vast.ai) GPU 云平台。
控制您的 GPU 实例,搜索报价,管理 SSH 密钥,跟踪账单 — 所有这些都可以通过您的 AI 助手完成。
支持 **stdio** (Claude Desktop / Claude Code)和 **SSE/HTTP** (LangGraph / LangChain)双重传输。
---
## 演示
https://github.com/user-attachments/assets/5415f5b1-91a5-4113-b26d-9d866d1abde6
## 功能
- **15 个 MCP 工具**,涵盖整个 Vast.ai 工作流程
- **双重传输**:stdio 用于 Claude Desktop,SSE/HTTP 用于 Python 代理
- **TypeScript 严格** — 完全类型化,无 `any`
- **Zod 验证**,适用于所有工具输入
- **自动重试**,适用于 HTTP 429(速率限制)
- **人类可读** 输出 — 无原始 JSON 转储
---
## 要求
- Node.js >= 18
- 具有 API 密钥的 [Vast.ai](https://vast.ai) 账户
- API 密钥权限:**用户**、**实例**、**账单** — 读取 + 写入,**无需 2FA**
---
## 安装
### 选项 A — npx(推荐,无需安装)
```json
{
"mcpServers": {
"vastai": {
"command": "npx",
"args": ["-y", "vastai-mcp-server"],
"env": {
"VAST_API_KEY": "your_api_key_here"
}
}
}
}
```
### 选项 B — 克隆并构建
```bash
git clone https://github.com/muhammedehab35/VAST.AI-MCP-SERVER
cd vastai-mcp-server
npm install
npm run build
```
---
## API 密钥设置
1. 前往 [console.vast.ai](https://console.vast.ai) → **账户** → **API 密钥**
2. 点击 **创建 API 密钥**
3. 启用:**用户**(读取+写入),**实例**(读取+写入),**账单**(读取+写入),**杂项**
4. **禁用所有 2FA 切换**(每行权限必须有灰色 2FA 切换)
5. 点击 **保存** 并复制密钥
---
## 配置
```bash
cp .env.example .env
# 添加您的密钥:
# VAST_API_KEY=your_key_here
```
---
## 用法
### Claude Desktop(stdio)
添加到 `claude_desktop_config.json`:
**macOS**:`~/Library/Application Support/Claude/claude_desktop_config.json`
**Windows**:`%APPDATA%\Claude\claude_desktop_config.json`
```json
{
"mcpServers": {
"vastai": {
"command": "npx",
"args": ["-y", "vastai-mcp-server"],
"env": {
"VAST_API_KEY": "your_api_key_here"
}
}
}
}
```
重启 Claude Desktop — 15 个 Vast.ai 工具将自动出现。
---
### Claude Code(VS Code 扩展)
添加到项目根目录的 `.mcp.json`:
```json
{
"mcpServers": {
"vastai": {
"command": "npx",
"args": ["-y", "vastai-mcp-server"],
"env": {
"VAST_API_KEY": "your_api_key_here"
}
}
}
}
```
---
### SSE/HTTP 模式 — LangGraph / LangChain
启动 HTTP 服务器:
```bash
npm run start:http
# 或
PORT=3000 VAST_API_KEY=your_key npx vastai-mcp-server http
```
端点:
| 端点 | 描述 |
|---|---|
| `GET /sse` | MCP 客户端的 SSE 连接 |
| `POST /messages` | 消息处理程序 |
| `GET /health` | 健康检查 |
#### Python(LangGraph)
```python
from langchain_mcp_adapters.client import MultiServerMCPClient
client = MultiServerMCPClient({
"vastai": {
"url": "http://localhost:3000/sse",
"transport": "sse"
}
})
tools = await client.get_tools()
```
#### Python(LangChain 直接)
```python
from langchain_mcp_adapters.client import MCPClient
async with MCPClient("http://localhost:3000/sse", transport="sse") as client:
tools = await client.list_tools()
```
---
### 开发模式
```bash
npm run dev # stdio — 无需构建
npm run dev:http # SSE/HTTP — 无需构建
```
---
## 可用工具(共 15 个)
### 实例
| 工具 | 描述 | 必需参数 | 可选参数 |
|---|---|---|---|
| `list_instances` | 列出所有实例 | — | — |
| `get_instance` | 获取实例详情 | `id` | — |
| `create_instance` | 从报价租用 GPU | `offer_id`、`image`、`disk` | `ssh`、`direct`、`label`、`env`、`onstart` |
| `start_instance` | 启动停止的实例 | `id` | — |
| `stop_instance` | 停止运行的实例 | `id` | — |
| `destroy_instance` | 永久销毁实例 | `id` | — |
| `reboot_instance` | 重启实例 | `id` | — |
### GPU 报价
| 工具 | 描述 | 可选参数 |
|---|---|---|
| `search_offers` | 搜索可用的 GPU,按价格排序 | `gpu_name`、`num_gpus`、`max_price`、`min_ram`、`disk_space`、`cuda_version`、`limit` |
### SSH 密钥
| 工具 | 描述 | 必需参数 |
|---|---|---|
| `list_ssh_keys` | 列出账户上的 SSH 密钥 | — |
| `add_ssh_key` | 将公钥 SSH 密钥添加到账户 | `ssh_key` |
| `attach_ssh_key` | 将 SSH 密钥附加到实例 | `instance_id`、`ssh_key` |
| `detach_ssh_key` | 从实例分离 SSH 密钥 | `instance_id`、`key_id` |
### 账单和账户
| 工具 | 描述 | 必需参数 |
|---|---|---|
| `get_account_info` | 账户信息 + 当前余额 | — |
| `get_billing_history` | 账单摘要 | — |
| `get_instance_cost` | 运行实例的估计成本 | `id` |
---
## 示例提示
```
搜索 RTX 4090 GPU,价格低于 0.40 美元/小时,内存至少 24GB
查找最便宜的可用 H100,支持 CUDA 12+
列出所有正在运行的实例
使用 offer ID 12345 创建实例,使用 pytorch/pytorch:latest,50GB 磁盘,启用 SSH
停止实例 99999
销毁实例 88888 — 训练完成
重启实例 77777,似乎冻结了
我的当前余额是多少?
实例 12345 到目前为止的费用是多少?
显示我的 SSH 密钥
将此 SSH 密钥添加到我的账户:ssh-ed25519 AAAA...
搜索用于分布式训练的 4x A100 GPU
```
---
## 工具输出示例
```
#1 报价 ID:12345678
GPU:1x RTX 4090
价格:$0.2900/小时
内存:24576 MB VRAM
内存:64 GB RAM
磁盘:80 GB
CUDA:12.2
网络:↑1000 Mbps / ↓1000 Mbps
国家:美国
可靠性:99.2%
```
---
## 项目结构
```
vastai-mcp-server/
├── src/
│ ├── index.ts # 入口点 — stdio 模式
│ ├── server.ts # MCP 服务器核心 + 工具注册
│ ├── http.ts # 入口点 — SSE/HTTP 模式
│ └── vastai/
│ ├── client.ts # Vast.ai REST API 包装器(fetch + 重试)
│ ├── instances.ts # 实例管理(7 个工具)
│ ├── offers.ts # GPU 报价搜索(1 个工具)
│ ├── ssh.ts # SSH 密钥管理(4 个工具)
│ └── billing.ts # 账单和账户(3 个工具)
├── package.json
├── tsconfig.json
└── .env.example
```
---
## 错误处理
| 错误 | 原因 | 解决方案 |
|---|---|---|
| `401 无效用户密钥` | 错误的 API 密钥 | 检查您的 `VAST_API_KEY` |
| `401 需要 2FA` | 密钥上启用 2FA | 使用所有 2FA 切换为 OFF 重新创建密钥 |
| `404 实例未找到` | 错误的实例 ID | 使用 `list_instances` 获取有效 ID |
| `429 速率限制` | 请求过多 | 自动重试,延迟 1 秒 |
---
## 许可证
MIT