Skip to main content
Glama
README.md
<div align="center">
  <img src="packages/web/public/favicon.svg" width="60" height="60" alt="gemini-agent-mcp logo" />
  <h1>gemini-agent-mcp</h1>
  <p>
    <a href="https://github.com/XTxiaoting14332/gemini-agent-mcp/actions/workflows/ci.yml"><img src="https://github.com/XTxiaoting14332/gemini-agent-mcp/actions/workflows/ci.yml/badge.svg" alt="CI" /></a>
    <a href="https://github.com/XTxiaoting14332/gemini-agent-mcp/releases"><img src="https://img.shields.io/github/v/release/XTxiaoting14332/gemini-agent-mcp?include_prereleases" alt="Release" /></a>
    <a href="LICENSE"><img src="https://img.shields.io/badge/license-GPL--3.0-blue.svg" alt="License: GPL-3.0" /></a>
    <img src="https://img.shields.io/badge/Cloudflare-Workers-F38020?logo=cloudflare&logoColor=white" alt="Cloudflare Workers" />
    <img src="https://img.shields.io/badge/Client-Go%201.24+-00ADD8?logo=go&logoColor=white" alt="Go Client" />
  </p>
</div>

[中文 README](README.zh-CN.md)

A remote MCP (Model Context Protocol) agent harness that lets Gemini Web or CLI clients inspect workspaces, read and edit files, track diffs, and execute shell commands on local machines behind NAT via a lightweight gateway and Go client binary.

## Architecture

```text
Gemini Web / CLI
   │  HTTPS (POST/GET /mcp/<deviceId>, Bearer or X-Gateway-Token)
   ▼
Gateway Server (Cloudflare Workers or self-hosted workerd)
   ├── RegistryHub (SQLite DO)  ── device list, clientToken, PBKDF2 admin password
   ├── RelayHub[deviceId] (SQLite DO + WebSocket Hibernation)
   │      ├── workspaces, diffs, command logs, pending approvals, device gatewayToken
   │      └── /ws/ui (live WebSocket updates to the web console)
   └── ASSETS (packages/web static bundle)
          ▲
          │  Outbound WSS (/ws/client, X-Client-Token + X-Device-Id)
          ▼
Local Go Client (gemini-mcp)
   └── ~/.gemini-mcp/config.json  ── serverUrl, clientToken, deviceId, mcpUrl, gatewayToken
```

Each connected machine receives its own `deviceId`, an isolated `/mcp/<deviceId>` endpoint, and a server-issued `gatewayToken`. Rotating or setting a custom device password in the web console updates the connected client's local configuration file automatically.

## Server deployment

### Option A: Cloudflare Workers

#### One-click deploy

[![Deploy to Cloudflare](https://deploy.workers.cloudflare.com/button)](https://deploy.workers.cloudflare.com/?url=https://github.com/XTxiaoting14332/gemini-agent-mcp)

During deployment, Cloudflare prompts for the secrets defined in [.dev.vars.example](.dev.vars.example):

- `ADMIN_PASSWORD`: initial administrator password for the web console (can be changed later inside the WebUI).
- `CLIENT_TOKEN`: pre-shared registration token used by local client binaries to connect via WebSocket.
- `SESSION_SECRET`: HMAC-SHA256 key used to sign WebUI session cookies.

#### Manual deploy via Wrangler

```bash
npm install
npm run build

npx wrangler secret put ADMIN_PASSWORD
npx wrangler secret put CLIENT_TOKEN
npx wrangler secret put SESSION_SECRET

npx wrangler deploy
```

The root [wrangler.jsonc](wrangler.jsonc) runs the `@gemini-mcp/shared` and `@gemini-mcp/web` builds automatically before packaging the Worker and static assets.

### Option B: Self-hosted outside Cloudflare (VPS / Docker)

The server runs on any Linux/macOS host or container using the open-source `workerd` runtime via Wrangler with local SQLite persistence, without requiring a Cloudflare account.

#### Run directly on a VPS

```bash
npm install
npm run build -w @gemini-mcp/shared && npm run build -w @gemini-mcp/web

cp .dev.vars.example .dev.vars
# Edit .dev.vars with your ADMIN_PASSWORD, CLIENT_TOKEN, and SESSION_SECRET

npm run start:server
```

This listens on `0.0.0.0:8787` and stores SQLite Durable Object state under `./data`.

#### Run with Docker

```bash
cp .dev.vars.example .dev.vars
# Edit .dev.vars with your secrets

docker build -t gemini-agent-mcp .
docker run -d \
  --name gemini-agent-mcp \
  --restart unless-stopped \
  -p 8787:8787 \
  -v "$(pwd)/data:/app/data" \
  -v "$(pwd)/.dev.vars:/app/.dev.vars:ro" \
  gemini-agent-mcp
```

#### Reverse proxy notes

Gemini Web requires HTTPS for remote MCP endpoints. Place Caddy, Nginx, or Traefik in front of port `8787` with WebSocket upgrade enabled and `X-Forwarded-Proto` / `X-Forwarded-Host` headers forwarded so the gateway assigns public `https://` MCP URLs to connecting clients.

Example Caddyfile:

```caddyfile
mcp.example.com {
  reverse_proxy 127.0.0.1:8787
}
```

## Client usage

### Download prebuilt binaries

Prebuilt static binaries for `windows/amd64`, `linux/amd64`, `linux/arm64`, and `linux/armhf` are published on [GitHub Releases](https://github.com/XTxiaoting14332/gemini-agent-mcp/releases).

Example on Linux (`amd64`):

```bash
curl -fsSL -o gemini-mcp https://github.com/XTxiaoting14332/gemini-agent-mcp/releases/latest/download/gemini-mcp-linux-amd64
chmod +x gemini-mcp
sudo mv gemini-mcp /usr/local/bin/gemini-mcp
```

### First-run setup and configuration

Run the binary with flags once, or run `gemini-mcp` in an interactive terminal to follow the prompt:

```bash
gemini-mcp \
  --url https://your-gateway.example.com \
  --token <CLIENT_TOKEN> \
  --name workstation
```

On connection, the server assigns `deviceId`, `mcpUrl`, and `gatewayToken`, writes them to `~/.gemini-mcp/config.json` (mode `0600`), and prints the assigned endpoint and password to stdout. Subsequent runs only require:

```bash
gemini-mcp
```

To use a custom config file path or update connection fields:

```bash
gemini-mcp --config /path/to/config.json
gemini-mcp init --url https://your-gateway.example.com --token <CLIENT_TOKEN> --name server-01
```

## Gemini MCP configuration

### Gemini Web (`gemini.google.com`)

In Gemini Web under custom MCP server connections (**Connect to MCP server**), the gateway exposes OAuth 2.1 discovery (`/.well-known/oauth-protected-resource`, `/.well-known/oauth-authorization-server`) and PKCE authorization (`/oauth/authorize`, `/oauth/token`):

1. **MCP Server URL:** `https://your-gateway.example.com/mcp/<deviceId>`
2. Expand **Other settings** (optional if using dynamic client registration, or fill directly from the WebUI **Overview** tab):
   - **Client ID (`OAuth Client ID`):** `<deviceId>`
   - **Client Secret (`OAuth Client Secret`):** `<DEVICE_GATEWAY_TOKEN>`
3. Click **Next** and approve access on the gateway authorization page using your device password (`<DEVICE_GATEWAY_TOKEN>`) or WebUI admin password.

### Gemini CLI / Gateway clients

Clients that support custom HTTP headers can connect directly using static Bearer or gateway headers:

```json
{
  "mcpServers": {
    "workstation": {
      "httpUrl": "https://your-gateway.example.com/mcp/<deviceId>",
      "headers": {
        "Authorization": "Bearer <DEVICE_GATEWAY_TOKEN>",
        "X-Gateway-Token": "<DEVICE_GATEWAY_TOKEN>"
      }
    }
  }
}
```

## Command policy and security

- **Workspace sandboxing:** file operations (`list_dir`, `find_files`, `read_file`, `write_file`, `edit_file`, `delete_file`, `grep_search`, `git_status`, `git_diff`) and `run_command` working directories are validated on the client against enabled workspace paths registered in the WebUI using symlink-aware realpath resolution.
- **`auto` mode (default):** `run_command` requires `risk_level` (`safe`, `moderate`, or `high`) and `risk_reason`, combined with deterministic server-side pattern escalation in [packages/shared/src/risk.ts](packages/shared/src/risk.ts) (`rm -rf`, `sudo`, piped remote scripts, `git push --force`, etc.). `safe` commands execute immediately; `moderate` and `high` commands pause with `APPROVAL_REQUIRED` and an `approval_id`, requiring user confirmation in chat (`允许` / `拒绝`) or via the WebUI **Commands** tab.
- **`bypass` mode:** executes workspace commands directly without approval prompts while still recording stdout, stderr, exit codes, and duration.
- **Credential storage:** the WebUI administrator password is stored as a salted PBKDF2-SHA256 hash inside `RegistryHub` SQLite. Changing the password in WebUI Settings increments the password version and invalidates prior session cookies.

## Repository layout

```text
.github/workflows/   CI checks and multi-arch client release workflow
packages/
  shared/            shared TypeScript protocol types, MCP tool schemas, risk classifier
  worker/            gateway router, MCP Streamable HTTP handler, RelayHub & RegistryHub DOs
  web/               React + Vite + Tailwind CSS v4 web console
  client-go/         standalone Go client binary (cmd/gemini-mcp)
  client/            Node.js/TypeScript reference client and esbuild single-file bundle
scripts/             end-to-end integration test against local wrangler dev
Dockerfile           self-hosted container image using workerd + local SQLite persistence
wrangler.jsonc       root Workers configuration for one-click, CLI, and self-hosted runs
```

## Development

Requirements: Node.js 20+ (tested on Node 22) and Go 1.24+.

```bash
npm install
npm run build
npm run typecheck
```

Run the end-to-end integration test (starts local `wrangler dev`, connects both the Go binary and Node bundle clients simultaneously, and verifies auth, multi-device isolation, diffs, `auto` approval, and `bypass` execution):

```bash
npm test
```

Cross-compile all release binaries locally:

```bash
npm run build:binary-all
```

## License

Source code is licensed under the **GNU General Public License v3.0 (GPL-3.0)**; see [LICENSE](LICENSE).