Skip to main content
Glama
README.md
# agy-bus

一个轻量级本地异步任务总线(**Stdio MCP Server + 后台并发 Worker**),用于在 macOS 上以单向调度架构将 **Gemini 桌面客户端(Task 模式)** 与终端 **`agy` CLI** 解耦串联:

- **Gemini 客户端** 负责需求拆解、架构规划、派单与结果验收;
- **后台 `agy` Worker** 负责重度编码、多仓库并发执行与批量任务;
- **零全盘权限**:MCP Server 只读写位于已连接文件夹内的 SQLite(WAL)与日志,无需给 Gemini 客户端勾选 `Allow full file access`。

## 架构设计

```text
Gemini Desktop (Local MCP Sandbox)
        │  enqueue_task / get_task_status / list_tasks / cancel_task
        ▼
  agy-bus-mcp  ──(read/write)──▶  .agy_bus/bus.db (SQLite WAL) + .agy_bus/logs/
                                         ▲
                                         │  atomic claim (per-cwd mutex)
                                         │
  agy-bus-worker (常规终端 / LaunchAgent,继承完整 PATH / Git / SSH / Keychain)
        │
        ▼  subprocess: agy -p ... --output-format json --mode ...
                         [--sandbox] [--conversation ...] [--model ...]
                         [--effort ...] [--agent ...] [--add-dir ...]
     agy CLI
```

### 核心特性

- **同目录串行互斥,不同目录并发**:`claim_next_task()` 通过 `BEGIN IMMEDIATE` 原子抢占任务,跳过正在执行的 `cwd`,避免两个 `agy` 同时修改同一 Git 仓库。
- **传引用不传全文**:MCP 默认只返回任务状态、退出码、会话 ID、`<= 1500` 字精简摘要和日志文件路径,避免长输出撑爆客户端上下文。
- **短任务阻塞等待**:`get_task_status` 支持 `wait_seconds`(`0..20`),可等待短任务完成后再返回,减少轮询次数。
- **异常自动恢复**:Worker 启动时自动将异常中断遗留的 `running` 任务标记为 `failed`。
- **兼容 `agy` 多种输出**:自动解析单行 JSON、多行 NDJSON 与纯文本降级,提取 `conversation_id` 供续接会话。

## 快速开始

### 前置依赖

- macOS + Python 3.11+
- [`uv`](https://docs.astral.sh/uv/)(`brew install uv`)
- 已登录的 [`agy` CLI](https://antigravity.google)(已在 `agy 1.5.21` 实测验证)

### 第 1 步:在 Gemini 已连接文件夹一键安装

> **重要**:请先进入你在 Gemini 客户端中已授权的 **Connected Folder**(这样无需开启全盘文件访问权限)。安装脚本会自动拉取代码到当前目录下的 `agy-bus/`(也可通过 `--dir <路径>` 或 `AGY_BUS_INSTALL_DIR` 自定义目录)。

```bash
cd <你的-Gemini-已连接文件夹>
curl -fsSL https://raw.githubusercontent.com/backtomyfuture/agy-bus/main/install.sh | bash
```

安装脚本会自动完成以下工作:

- 自动拉取或更新 `agy-bus` 源码(支持 `git` 与无 `git` 环境的 tarball 下载);
- 安装依赖并构建 `.venv` 虚拟环境;
- 运行离线自检测试(`uv run --offline pytest -q`);
- 自动安装并启动 macOS `LaunchAgent` 后台常驻 Worker(支持开机自启;如需跳过可追加 `--no-service`);
- 自动将 MCP 命令路径复制到剪贴板(可直接 `Cmd + V` 粘贴)。

### 第 2 步:在 Gemini 桌面客户端添加 Local MCP

打开 Gemini 客户端 **Task 模式 → Connected Apps → Local MCP**,将已自动复制的命令直接 `Cmd + V` 粘贴到 **Command**,保持 **Allow full file access** 不勾选即可立即使用:

| 配置项 | 填写内容 |
| --- | --- |
| **Name** | `agy-bus` |
| **Description** | `Local async task bus for agy CLI` |
| **Command** | `Cmd + V` 粘贴(`<项目绝对路径>/.venv/bin/agy-bus-mcp`) |
| **Allow full file access** | **保持不勾选** |

> **为什么不填 `uv run`?** 在未勾选 `Allow full file access` 的沙箱中,`uv` 会尝试访问 `~/.cache/uv` 而被拦截;直接指向 `.venv/bin/agy-bus-mcp` 可免去 `uv` 依赖且毫秒级握手。

> 卸载后台服务:`curl -fsSL https://raw.githubusercontent.com/backtomyfuture/agy-bus/main/install.sh | bash -s -- --uninstall`

## MCP 工具列表

| 工具 | 说明 |
| --- | --- |
| `enqueue_task(prompt, cwd, mode="accept-edits", use_sandbox=True, conversation_id="", model="", effort="", agent="", add_dirs=[])` | 校验参数并入队,毫秒级返回 `task_id`、`status`、`log_path` 及任务配置 |
| `get_task_status(task_id, include_log_tail=False, tail_lines=40, wait_seconds=0)` | 查询状态、退出码、精简摘要、会话 ID 与日志路径;`wait_seconds` 支持 `0..20` 秒短暂阻塞等待至结束;按需附带日志末尾 |
| `list_tasks(limit=10, status="")` | 列出最近任务概览(支持按 `pending` / `running` / `done` / `failed` / `cancelled` 过滤) |
| `cancel_task(task_id)` | 取消尚处于 `pending` 状态的任务 |

### `enqueue_task` 支持的 `agy` 参数映射

| MCP 参数 | 对应 `agy` 选项 | 默认值 / 可选值 | 说明 |
| --- | --- | --- | --- |
| `prompt` | `-p` | 必填 | 给 `agy` 的完整任务指令(建议包含目标、约束与验收标准) |
| `cwd` | 工作目录 | 必填 | 目标工作目录路径(同目录串行互斥,不同目录并发) |
| `mode` | `--mode` | `"accept-edits"`(可选 `accept-edits` \| `auto` \| `plan`) | 执行权限模式 |
| `use_sandbox` | `--sandbox` | `True` | 为 `True` 时附加 `--sandbox` 终端限制 |
| `conversation_id` | `--conversation` | `""` | 传入既有会话 ID 以续接上下文;留空表示新会话 |
| `model` | `--model` | `""` | 指定模型名称;留空使用 `agy` 默认模型 |
| `effort` | `--effort` | `""`(可选 `low` \| `medium` \| `high` \| `xhigh` \| `max`) | 指定推理强度 |
| `agent` | `--agent` | `""` | 指定 `agy` Agent 名称 |
| `add_dirs` | `--add-dir` | `[]` | 附加到工作区的额外目录列表(自动展开为可重复 `--add-dir`) |

## 常用环境变量(Worker 侧)

| 变量 | 默认值 | 说明 |
| --- | --- | --- |
| `AGY_BUS_DIR` | `<项目目录>/.agy_bus` | SQLite 数据库与日志目录 |
| `AGY_BIN` | 自动探测(`which agy`) | `agy` CLI 可执行文件路径 |
| `AGY_POLL_INTERVAL` | `1.5` | 队列轮询间隔(秒) |
| `AGY_MAX_WORKERS` | `2` | 最大并发执行线程数 |
| `AGY_TASK_TIMEOUT` | `1800` | 单任务超时时间(秒) |
| `AGY_NOTIFY` | `1` | 完成后发送 macOS 原生通知(`0` 关闭) |

## 常见问题排查

- **Gemini 客户端点 Next 一直卡住**:先彻底退出客户端(`Cmd + Q`)再重启以重置本地 MCP 管理器;同时确保项目使用的是 `pyproject.toml` 中锁定的 `mcp>=1.6.0,<2.0.0`(稳定版 `FastMCP`)。
- **Worker 执行报错 `authentication required`**:请确认 Worker 是在常规 macOS 终端或 `LaunchAgent` 中启动,并且已在该终端执行过 `agy` 完成登录。
- **运行自动化测试**:
  ```bash
  uv run --offline pytest -q
  ```

## License

[MIT](./LICENSE)

TDQS

A4/5.0

Scored across 4 tools

Disambiguation5/5

Each tool targets a distinct queue operation: enqueue, list, cancel, and detailed status. The boundary between list_tasks (multi-task overview) and get_task_status (single-task detail) is clear from descriptions and arguments.

Naming Consistency5/5

All names follow a consistent snake_case verb_noun pattern: enqueue_task, list_tasks, cancel_task, get_task_status. The naming is predictable and readable.

Tool Count5/5

Four tools are well-scoped for a queue-management server, covering creation, listing, cancellation, and status inspection. No tool feels redundant or out of place.

Completeness4/5

The core task lifecycle is covered: enqueue, list, inspect status, and cancel. Minor gaps exist around retrying failed tasks, purging completed tasks, or retrieving logs separately, but the included log-tail option mitigates most debugging needs.

Maintenance

ActivityMaintained
ResponsivenessNo recent community issues