ag-mcp
# ag-mcp
An MCP server that lets Claude Code drive [`agy`](https://antigravity.google/) (Google's
Antigravity CLI) as a cheap, sandboxed exploration agent.
`agy` defaults to inexpensive Gemini Flash models and is fast at broad, greedy exploration —
but it hallucinates more than Claude does. This server is built around that tradeoff: it exposes
`agy` as a delegate that Claude can send exploration/search/summarization tasks to, with an
explicit per-call permission preset, and every result comes back tagged with a reminder that
`agy`'s claims (file paths, line numbers, quoted code) need to be verified with Claude's own
tools before being trusted or repeated to the user.
## How permissions work
`agy` runs in headless print mode (`agy -p ...`), where every tool call is auto-denied unless
pre-approved in its `~/.gemini/antigravity-cli/settings.json`. This server does **not**
permanently change that file. Instead, for each call it:
1. Reads the current real `settings.json` and remembers its exact bytes.
2. Writes a temporary version with `permissions.allow` set to just the tools for the
requested preset.
3. Runs `agy`.
4. Restores the original file byte-for-byte (or deletes it if it didn't exist before),
even on error or if the process is killed mid-call.
Calls are serialized in-process so concurrent tool calls can't race on that file.
Three presets, chosen per call via the `permission` argument:
| preset | what agy can do |
|--------------|------------------------------------------------------------------------------|
| `none` | Nothing — answers from its own context only, no tool calls at all. |
| `read_only` | `read_file`, `glob`, `codebase_search`, plus a curated set of read-only shell commands: `ls`, `cat`, `head`, `tail`, `wc`, `pwd`, `file`, `stat`, `du`, and `git log`/`diff`/`show`/`status`/`blame` (any arguments, but no chaining/piping/substitution/redirection — verified empirically, see `src/permissions.js`). Cannot write anything. **Default.** |
| `read_write` | Full access via `--dangerously-skip-permissions` (read, write, edit, run shell commands). No guardrails — only use when you actually want agy to make changes. |
Every call also gets a prompt prefix (`src/agy.js`) telling agy up front exactly what it can and
can't reach for the given preset — without it, cheap/low-effort models tend to reach for a denied
shell command first and give up instead of falling back to an allowed tool.
## Streaming and progress
Calls run with `agy --output-format stream-json`, parsed event-by-event as they arrive (not
buffered and parsed as one blob at the end). Each event — agy starting, a tool call
starting/finishing, incremental response text — is forwarded live:
- If the calling client requested progress tracking (sent a `progressToken`), it gets real
`notifications/progress` events tied to that call.
- Otherwise it gets `notifications/message` (logging) events instead — a connection-level
stream that needs no client opt-in, as a fallback for clients that don't support per-call
progress tracking.
Either way the final tool result is unchanged; this only affects what you see while it's running.
## Watching a background task without polling
`agy_status`'s `wait_seconds` long-polls, but it's capped at 25s: a real observed failure was a
120s long-poll exceeding the calling client's own tool-call timeout, which caused the client to
kill/restart this MCP server to recover — destroying every task tracked in it, including whatever
was still running. Long-polling this server is inherently risky for that reason.
The more robust pattern is `log_file`: pass a path on `agy_ask`/`agy_continue` (with
`background: true`) and the task's activity streams to that file as plain text. The file is
created immediately — before anything async even starts — so a wait-loop never spins on one that
doesn't exist yet. Once the task reaches a terminal state (success, a real error, or even a crash
before it properly started), the raw response/error text is appended between
`###AGY_RESPONSE_START###` / `###AGY_RESPONSE_END###` lines, then the full JSON result, then a
closing marker — `###AGY_TASK_DONE###` on success, `###AGY_TASK_ERROR###` on any failure, always
exactly one of the two, guaranteed (see `finalizeLog` / the top-level try/catch in
`runOrBackground`, `src/index.js`). Wait on that file with your own Bash tool's
`run_in_background: true` instead of polling this server:
```bash
until grep -qE '###AGY_TASK_DONE###|###AGY_TASK_ERROR###' /path/to/log; do sleep 5; done
```
That gets you an ordinary background-task completion notification from your own tooling — on
success *or* failure — with no risk of a long-poll tripping a client-side timeout and taking the
whole server down with it.
To extract just the response/error text without JSON-parsing the full result block:
```bash
sed -n '/###AGY_RESPONSE_START###/,/###AGY_RESPONSE_END###/p' /path/to/log | sed '1d;$d'
```
This matters in practice: agy can return `status: "ERROR"` (e.g. a network failure finalizing a
huge-context turn) while `response` still contains a complete, valid answer — the error status
doesn't mean the content is worthless, so pull `response` out and check it before assuming a
failed call produced nothing useful.
A [full home-directory sandbox](https://www.antigravity.google) (fake `$HOME`) was tried first so
presets wouldn't need to touch the real settings file at all, but `agy` also validates auth
against the macOS Keychain, which isn't affected by `$HOME` — so that approach breaks login and
was dropped in favor of the transient-swap approach above.
## Tools
- **`agy_ask(prompt, permission?, cwd?, add_dir?, model?, effort?, mode?, timeout_seconds?, background?, log_file?)`**
Starts a new `agy` conversation. Returns `conversation_id`, `status`, `response`, token
`usage`, and duration. `timeout_seconds` maxes out at 3600, but for a blocking call (default)
that just wastes the whole call sitting there — prefer `background: true` for anything that
might run long. `log_file` streams activity to a file ending in a `###AGY_TASK_DONE###` marker —
see "Watching a background task without polling" below.
- **`agy_continue(conversation_id, prompt, permission?, ...)`**
Sends a follow-up into an existing conversation, preserving its context. Same parameters
(including `background`) as `agy_ask`.
- **`agy_status(task_id, wait_seconds?)`**
Checks a background task started with `background: true`. Returns its status
(`running`/`done`/`error`), a log of recent activity (tool calls, response text as it streamed
in), and — once finished — the same result object a blocking call would have returned.
`wait_seconds` (max 120) long-polls instead of returning `running` immediately.
- **`agy_list_tasks()`**
Lists every background task from this session (any status), so a lost `task_id` can be
recovered.
- **`agy_cancel(task_id)`**
Kills a still-running background task.
- **`agy_list_models()`**
Lists models available to `agy` (mostly Gemini Flash tiers at various reasoning efforts,
plus a couple of Claude/GPT-OSS options) for use with the `model` parameter.
Background tasks are tracked in-memory for the life of this server process (`src/tasks.js`) —
they don't survive an MCP reconnect/restart, and (like the underlying permission-preset
mutation) `none`/`read_only` tasks still queue behind each other one at a time; only
`read_write` calls, which skip the settings-file swap entirely, can truly run concurrently
with another call.
## Install
```bash
cd /path/to/AG-mcp
npm install
```
Register with Claude Code:
```bash
claude mcp add ag-mcp -- node /path/to/AG-mcp/src/index.js
```
Or add directly to your Claude Code MCP config:
```json
{
"mcpServers": {
"ag-mcp": {
"command": "node",
"args": ["/path/to/AG-mcp/src/index.js"]
}
}
}
```
Requires `agy` to already be authenticated (`agy` will prompt to log in on first interactive
run if it isn't) and on `$PATH` as `agy` (override with the `AGY_MCP_BIN` env var if it's
installed elsewhere).
## Known limitations
- Headless mode has no interactive "always allow" flow, so `agy` shouldn't legitimately write
to `settings.json` mid-call — but if it ever did, this server's restore-on-finally would
discard that write along with the temporary permission patch.
- `read_write` gives `agy` unrestricted shell/file access for the duration of that single call.
Scope `cwd` / `add_dir` tightly and only use it when you mean it.
TDQS
Scored across 6 tools
Each tool targets a distinct operation: start a conversation (agy_ask), continue one (agy_continue), enumerate models (agy_list_models), poll a background task (agy_status), enumerate tasks (agy_list_tasks), and kill a task (agy_cancel). The ask/continue split is explicitly clarified in the descriptions, and status vs list_tasks serve clearly different roles (single task detail vs session-wide inventory). No two tools appear interchangeable.
All tools share the agy_ prefix and follow a verb_noun or verb pattern (ask, continue, list_models, list_tasks, cancel), which is predictable and readable. The only deviation is agy_status, which uses a noun rather than a verb like get_status, a minor inconsistency.
Six tools is well-scoped for a wrapper around a single CLI agent: one entry point, one continuation, and a small background-task lifecycle plus model discovery. Every tool maps to a real need (start, follow up, monitor, enumerate, cancel, discover models) with no filler.
The surface covers the full lifecycle of a delegation session: initiate, continue with context, run in background, poll status, list tasks, cancel, and pick a model. Minor gaps exist — no way to enumerate or resume conversations from prior sessions, and no tool to abort a blocking call — but agents can work around these.