agy-colab-train
Creates, reuses, inspects and destroys Google Colab GPU/TPU runtimes (T4, L4, G4, A100, H100, CPU, TPU v5e/v6e), submits local Python training/fine-tuning/eval scripts to them with environment-variable injection, keeps or discards runtimes per TTL policy, and automatically reattaches to the same execution when hitting the ~600 s execute_code limit or transient network failures.
Synchronizes datasets, pretrained weights, checkpoints, logs and the training script itself with Google Drive through the REST API v3 (drivePull, drivePush, saveScriptToDrive), including resumable 308 chunked uploads, 4-thread bounded concurrent multi-file transfer, best-effort push-back even when the script fails, and OAuth token isolation — without interactive drive.mount().
This server does not currently have a deployable release on Glama. Follow the repository's setup instructions to install and configure it for your MCP client, including any required credentials and local dependencies.
agy-colab-train
极简单账号 Google Colab GPU 训练 MCP 插件(零第三方依赖),面向 Antigravity 及标准 MCP 客户端,只保留远程模型训练、微调、评估与产物拉取所需的最小核心能力,并针对代理兼容、并发调度、断流续接、Token 自愈与防实例泄漏做了工程加固。
✨ 具备的功能
1. 算力探测与实例生命周期管理
实时算力规格探测(
colab_specs):实时向 Google Colab MCP 查询(list_runtime_specs与get_subscription),返回当前账号有资格申请的runtimeSpecs(每条记录均附带标准化spec别名)、去重汇总的availableSpecs列表以及账号订阅等级subscriptionTier。内置覆盖全部 11 种规格:gpu_t4、gpu_t4_highmem、gpu_l4、gpu_g4、gpu_a100、gpu_a100_highmem、gpu_h100、cpu、cpu_highmem、tpu_v5e1、tpu_v6e1。实时性边界说明:
colab_specs反映的是账号级实时申请资格与订阅等级,而 Google 上游 Colab MCP API 并不暴露集群实时库存数量或剩余 Compute Units(若遇到容量不足或配额耗尽,会在调用create_runtime创建实例时由上游返回)。
创建与安全复用保留实例(
colab_ensure_runtime):申请指定规格运行时并在 25 分钟 TTL 内自动复用;除上述 11 种内置规格外,还支持针对实时list_runtime_specs的动态解析兜底,上游新增加速器/规格无需修改插件代码即可按colab_specs返回的spec别名直接使用。复用前强制检查:规格一致性与防孤儿实例(
replaceExisting默认true):当缓存的保留实例与请求的spec不同(例如从gpu_t4切换为gpu_a100)时,默认自动调用delete_runtime销毁旧缓存实例后再创建新实例,防止遗留后台计费(传入replaceExisting: false可保留旧实例);远端存活探测:通过
get_runtime确认远端未提前回收,若已失效则自动清理状态并重建。
实例巡检与销毁(
colab_list/colab_delete):列出账号下全部远端活跃 runtime 与本地缓存状态;支持一键销毁指定实例或当前保留实例,防止后台持续扣减计算单元。
2. 远程执行与长训练支持
本地训练与诊断脚本一键提交(
colab_run_script):读取本地
.py文件(包括诊断脚本、冒烟测试与正式训练脚本)提交至 Colab 执行,脚本始终保留在本地;同样支持上述 11 种内置规格与基于实时list_runtime_specs的动态规格解析兜底;清晰的运行时生命周期语义:
默认
keep: false(且未传runtime):直接创建一个独立临时(ephemeral)runtime,不读取也不覆盖state.json中单独保留的实例,并在try/finally中确保无论成功或报错/超时都只销毁该临时session与runtime,杜绝僵尸实例;传入
keep: true(且未传runtime):自动创建或复用state.json中记录的保留实例与/content文件,便于分步上传、执行与下载;显式传入
runtime:直接复用指定实例,执行结束后不会自动销毁(需手动调用colab_delete释放);
支持通过
env参数(如{"EPOCHS": "10", "LR": "1e-4"})安全注入远端os.environ(自动插入在 shebang、编码注释、模块 docstring 及from __future__ import语句之后),无需反复修改训练脚本源码。
600 秒断流自动续接(
executionIdReattach):Colab 官方 MCP 单次
execute_code超时上限约 600 秒;遇到超时、502/503/504 或网络瞬断时,服务端会复用同一个executionId(也可显式传入executionId)自动重新挂载并继续等待输出(maxReattach默认6,上限36)。
3. 数据集、代码与模型产物传输(本地 & Google Drive 直连同步)
流式分块上传(
colab_upload):将本地脚本、配置或数据集上传至容器/content/<remotePath>,自动创建远端父目录;超过4 MiB自动按4 MiB切片逐块流式读取上传(不将整文件载入内存),并在遇到瞬断或 5xx 错误时从首块起完整幂等重试(最多 3 次),避免单块重复续写错位。流式原子落盘下载(
colab_download):从/content/<remotePath>拉取 checkpoint(.pt/.safetensors)、训练日志或评估结果;优先走/files/流式写入.part临时文件,并在原子重命名前校验响应完成状态(检测流异常中止或提前关闭)与未压缩Content-Length字节数,若发生截断流则清理临时文件并报错而非保留残缺文件;失败自动回退/api/contents通道(同样校验响应完成状态)。Google Drive 直连同步(
colab_run_script内置drivePull/drivePush/saveScriptToDrive):无需交互式
google.colab.drive.mount(),通过纯 Python 标准库实现的 tools/colab_drive_helper.py 在隔离命名空间中直接调用 Google Drive REST API v3(支持X-Goog-User-Project配额项目、批量目录列举与 4 线程有界并发多文件传输);完整 HTTP 308 偏移量查询断点续传:8 MiB 分块上传严格遵循 Drive
308 Resume Incomplete返回的Range(bytes=0-N)与更新的Location头推进偏移;遭遇瞬断或 429/5xx 时,自动发送PUTContent-Range: bytes */<total>查询服务端已提交偏移量后精确续传(遇410 Gone自动重建上传会话一次);saveScriptToDrive:在执行前将提交的本地.py脚本原样归档到 Google Drive;drivePull:执行前从 Drive 递归拉取数据集/预训练权重到 Colab 运行时(自动跳过 Google Docs 等原生文档,单文件拉取原生文档会直接拒绝);流式写入.part临时文件后会在原子重命名前检测截断流(校验未压缩Content-Length与响应完整性),若截断则清理.part文件并自动重试,避免覆盖目标文件;drivePush:执行后强制刷新 OAuth Token 并递归推送 checkpoint/日志回 Drive(同名文件原地PATCH覆盖,支持 0 字节文件);即使用户脚本执行报错,仍会以allowMissing: true尽力回传已有 checkpoint 后再销毁实例,同时保留ok: false;小文件打包建议:若数据集包含数千个以上零散小文件,建议先打包为
.tar或.zip归档再通过 Drive 同步到 Colab 后本地解压;即使已支持单目录批量列举与 4 线程并发,归档传输仍能大幅降低逐文件 API 请求开销并避免触发 Google Drive HTTP 429 限流;Token 隔离与最小暴露:OAuth Token 与同步参数不嵌入
execute_code源码(因此 IPythonIn[n]与history.sqlite均不会记录 Token),也不会写入训练脚本的os.environ或打印到输出/错误信息;而是由服务端通过 Jupyter/api/contents/直接上传为临时文件,tools/colab_drive_helper.py 读取后会在发起任何 Drive 网络请求前立即在finally中以全零字节(\x00)覆写并unlink删除(MCP 服务端finally也会再次兜底清理),并在隔离命名空间中运行后销毁引用。注意:与任何共享虚拟机容器一样,当keep: true容器正在执行 Drive 同步时,请勿在同容器内运行不可信代码。
4. 底层运行保障与 Antigravity 集成
全自动非阻塞鉴权与故障自愈:通过异步
gcloudADC 刷新 OAuth Token(内存缓存 45 分钟,同配置并发鉴权通过tokenInFlight去重);遇HTTP 401自动清缓存重新鉴权,遇 MCP 会话(mcp-session-id)过期自动重新initialize并重试请求。零依赖兼容 Node 18+ 系统代理:内置基于
node:http/node:https/node:tls的 HTTPCONNECT隧道,自动读取环境变量代理(HTTPS_PROXY/HTTP_PROXY)及 Windows 注册表系统代理(15 秒 TTL 缓存),支持 HTTP 与 HTTPS 代理的Proxy-Authorization认证,并自动绕过localhost、127.0.0.1与NO_PROXY。并发 MCP 派发、高可靠 stdio 与原子状态:
stdin按字节缓冲按行 UTF-8 解码(避免中文跨分包乱码);采用非阻塞并发派发(inFlight追踪在途请求),长训练colab_run_script运行期间可随时并发调用colab_list、colab_specs、colab_delete而不被阻塞,SIGINT/SIGTERM时优雅等待在途请求收尾;状态文件state.json采用临时文件原子重命名落盘;工具返回ok: false时统一标记isError: true。零日常上下文占用:仅提供按需加载的训练技能 skills/colab-train/SKILL.md,不安装常驻全局
rules/AGENTS.md。
Related MCP server: colab-fleet
🛠️ 暴露的 7 个 MCP 工具
工具名 | 关键参数 | 作用 |
| — | 实时查询 Colab MCP 可用规格与资格,返回 |
|
| 创建或复用同规格且存活的保留运行时(支持 11 种内置规格与实时 |
|
| 提交本地 |
|
| 流式分块上传本地文件到 |
|
| 流式下载 |
| — | 查看远端运行中的 runtime 与本地缓存状态 |
|
| 销毁指定 runtime(省略时销毁当前保留实例) |
✂️ 刻意不包含的内容
为了保持最小体积与高可靠性,本仓库刻意剔除了原版中的非训练模块:
不包含 Blender 3D 渲染农场调度器与冒烟脚本;
不包含 AI 视频 / 音乐 / TTS / ASR 等媒体 Notebook(
cells/、notebooks/);不包含内联代码执行工具(统一通过本地
.py+colab_run_script执行,脚本保留在本地);不使用交互式
google.colab.drive.mount():无头 MCP 运行时没有 Colab Web UI 来完成交互式 OAuth 传播,drive.mount()会挂起或失败;取而代之的是通过colab_run_script的drivePull/drivePush/saveScriptToDrive直接走 Google Drive REST API v3;不包含多账号独立 MCP 进程注册脚本;
不包含浏览器 CDP 自动化脚本或常驻上下文规则。
📁 目录结构
agy-colab-train/
├── plugin.json # Antigravity 插件清单
├── package.json # 零依赖包元信息与 npm test 入口
├── mcp_config.example.json # 单账号 MCP 配置模板
├── install.ps1 # Windows 一键安装脚本
├── install.sh # Linux / macOS 一键安装脚本
├── skills/
│ └── colab-train/
│ └── SKILL.md # 按需加载的 Colab 训练工作流技能
├── tools/
│ ├── colab_client.mjs # 零依赖 MCP JSON-RPC + 代理隧道 + 流式文件传输层
│ ├── colab_drive_helper.py # 纯标准库 Google Drive v3 并发同步与 308 断点续传脚本
│ └── colab-mcp-server.mjs # stdio MCP 服务端(7 工具,并发派发)
└── tests/
└── colab_mcp.test.mjs # 基于本地 Mock 服务的离线自动化测试🚀 授权、安装与使用
1. 前置准备(Google Cloud 一次性授权)
确保本机已安装 Node.js 18+(推荐 20+)与 Google Cloud SDK (gcloud)。由于 Google 会拦截 gcloud 默认客户端请求敏感的 drive 权限,需要先在自己的 GCP 项目中创建一个桌面端 OAuth 客户端并添加测试用户,再完成 ADC 授权:
创建项目并启用 API(
colaboratory/drive/cloudresourcemanager):
gcloud projects create <YOUR_PROJECT_ID> --name="colab-train"
gcloud services enable colaboratory.googleapis.com drive.googleapis.com cloudresourcemanager.googleapis.com --project=<YOUR_PROJECT_ID>
gcloud projects add-iam-policy-binding <YOUR_PROJECT_ID> \
--member="user:<YOUR_EMAIL>" --role="roles/serviceusage.serviceUsageConsumer"(Windows PowerShell 下请将行尾续行符 \ 替换为 `。)
在 Google Auth Platform 中配置该项目的 OAuth 同意屏幕(受众群体选 External,并在 Test users 中添加你自己的
<YOUR_EMAIL>),随后在 Clients → Create client 选择 Desktop app(若用于 Agent / MCP 调用,可勾选 Use this client for an AI-powered agent)并下载client_secret.json;使用
--client-id-file完成本地 ADC 登录并绑定配额项目:
gcloud auth application-default login --launch-browser \
--client-id-file="<PATH_TO_client_secret.json>" \
--scopes="https://www.googleapis.com/auth/colaboratory,https://www.googleapis.com/auth/cloud-platform,https://www.googleapis.com/auth/drive,openid,https://www.googleapis.com/auth/userinfo.email"
gcloud auth application-default set-quota-project <YOUR_PROJECT_ID>若仅需运行训练而不使用 Google Drive 同步,可省略
--client-id-file与drivescope。
⚠️ OAuth
Testing状态 Refresh Token 7 天过期提醒若 GCP OAuth 同意屏幕处于 External +
Testing状态,Google 会每 7 天 使 Refresh Token 过期失效(报错invalid_grant)。遇到该报错时,只需重新执行上方的gcloud auth application-default login --client-id-file=...与gcloud auth application-default set-quota-project <YOUR_PROJECT_ID>即可秒级恢复;如需免去 7 天重新登录,可在 Google Auth Platform 将应用发布为In production(注:包含drive敏感权限的 External 应用需通过 Google 审核),或使用 Google Workspace 账号配置Internal应用类型。
2. 安装到 Antigravity
Windows(PowerShell)
git clone https://gitlab.com/jorgescanlon7/agy-colab-train.git
cd agy-colab-train
.\install.ps1 -ProjectId <YOUR_PROJECT_ID>插件会安装到 %USERPROFILE%\.gemini\config\plugins\colab-train 并生成 mcp_config.json(默认跳过已有配置,传入 -Force 可重生成)。
Linux / macOS(Bash / POSIX)
git clone https://gitlab.com/jorgescanlon7/agy-colab-train.git
cd agy-colab-train
chmod +x install.sh
./install.sh -p <YOUR_PROJECT_ID>插件会安装到 $HOME/.gemini/config/plugins/colab-train 并生成 mcp_config.json(默认跳过已有配置,传入 -f / --force 可重生成;可用 -h / --help 查看参数说明)。完成后重启 Antigravity 或重载插件即可使用。
3. 典型训练流程
调用
colab_specs实时确认账号可用算力(查看availableSpecs与subscriptionTier,内置覆盖gpu_t4/gpu_t4_highmem/gpu_l4/gpu_g4/gpu_a100/gpu_a100_highmem/gpu_h100/cpu/cpu_highmem/tpu_v5e1/tpu_v6e1及实时返回的动态规格别名);若需分步上传本地文件并保留环境,调用
colab_ensure_runtime({ spec: "gpu_t4" })申请保留实例(后续切换如gpu_a100时默认replaceExisting: true会自动清理旧缓存实例);调用
colab_upload上传本地训练脚本与数据集到/content/train/(或直接在colab_run_script中通过drivePull从 Google Drive 拉取数据集;若包含大量小文件建议先打成.tar/.zip);调用
colab_run_script({ path: "./train.py", keep: true, env: { EPOCHS: "10" }, maxReattach: 12, saveScriptToDrive: "colab-train/runs/exp1/train.py", drivePull: [{ drivePath: "datasets/exp1.tar", remotePath: "data/exp1.tar" }], drivePush: [{ remotePath: "runs", drivePath: "colab-train/runs/exp1/out" }] })启动训练;训练期间仍可随时并发调用colab_list查看实例状态;若未配置
drivePush,可调用colab_download将/content/runs/best.pt等产物拉回本地;若使用了
keep: true或显式runtime,完成后调用colab_delete释放实例并调用colab_list确认零残留;若使用默认keep: false(不传runtime),服务端会在隔离临时实例上完成执行与drivePush后自动销毁该临时实例(且不会影响state.json中另外保留的实例)。
4. 运行离线测试
npm test
# 或直接运行:
node --test tests/colab_mcp.test.mjs
python -m py_compile tools/colab_drive_helper.py # Linux/macOS 可用 python3📄 致谢与许可
本项目基于 holoiallo/agy-colab-plugin(MIT License)精简重构而来,采用 MIT License 开源。
Tool Schema Changelog
Recent tool additions, removals, and schema changes observed during successful MCP inspections.
Loading tool schema changelog…
Related MCP Connectors
Remote MCP access to user-authorized Linux, Windows and Intel Mac devices for AI assistants.
Drive Google Antigravity from the Gemini app: remote MCP with OAuth 2.1 + PKCE.
Remote MCP for Gemini upgrade evals, prompt regressions, output diffs, and eval receipts.
Account-less access to 45 Gemini models & autonomous agents via multi-rail MCP.
Related MCP Servers
- AlicenseBqualityDmaintenanceLocal-first MCP server for controlling Google Colab as a development, shell, file, and training runtime, with tools for notebook editing, GPU acceleration, and file transfer.599Apache 2.0
- AlicenseNot gradedqualityDmaintenanceEnables offloading heavy CPU/ML workloads to free Google Colab from any MCP client, with automatic job management and free-tier enforcement.16MIT
- FlicenseNot gradedqualityDmaintenanceEnables MCP clients to execute Python code on Google Colab GPU/TPU runtimes via the official Colab CLI, with session, file, and package management capabilities.-
- AlicenseBqualityCmaintenanceLets any MCP-compatible agent use Google Colab or Jupyter runtimes as an execution backend, enabling code execution, notebook management, package installation, hardware introspection, training jobs, and file handling.401MIT