Gmail MCP Safe Bridge
by mateo627
README.md
# gmail-mcp-safe-bridge
Zero-dependency Node.js `stdio` bridge to Google's official hosted Gmail MCP server (`https://gmailmcp.googleapis.com/mcp/v1`, MCP `2025-03-26`), with PKCE OAuth2 and a default `--mode=safe` guardrail.
## Why This Exists
Google's hosted Gmail MCP server speaks Streamable HTTP and requires OAuth2 bearer tokens. Most local coding agents (Claude Code, Codex CLI, Gemini / Antigravity) prefer local `stdio` MCP servers, and giving an agent direct access to `send_message`, `reply`, `forward`, or `trash_thread` is risky.
This single-file Node.js bridge:
- Uses **zero npm dependencies** (Node.js 18+ built-ins only).
- Handles **PKCE (`S256`) OAuth2 login** via an ephemeral `127.0.0.1` loopback callback and automatic token refresh.
- Locks credentials to the current user (`0700`/`0600` on POSIX; user-only `icacls` on Windows).
- Runs in **`--mode=safe` by default**, enforcing safety at both the OAuth scope layer and the MCP tool layer.
## Safety Modes
| Mode | OAuth Scopes | Exposed Tools | Blocked Tools |
| --- | --- | --- | --- |
| `--mode=safe` *(default)* | `gmail.readonly`, `gmail.compose`, `gmail.labels` | **19** read, search, label, filter, and draft tools (`search_threads`, `get_thread`, `get_message`, `list_drafts`, `get_draft`, `create_draft`, `update_draft`, `list_labels`, `create_label`, `update_label`, `label_thread`, `label_message`, `update_message_labels`, `list_filters`, `create_filter`, `untrash_*`, `unmark_*_spam`) | **11** outbound/destructive tools (`send_message`, `reply`, `forward`, `trash_thread`, `trash_message`, `mark_thread_spam`, `mark_message_spam`, `unlabel_thread`, `unlabel_message`, `apply_sensitive_thread_label`, `apply_sensitive_message_label`) |
| `--mode=full` | `gmail.modify`, `gmail.compose`, `gmail.send`, `gmail.settings.basic` | All **30** official Google Gmail MCP tools | None |
In `--mode=safe`, blocked tools are stripped from `tools/list` and hard-blocked in `tools/call` before any network request is sent. Agents can still draft replies with `create_draft` / `update_draft` for you to review and send manually.
## Quick Start (~2 Minutes)
### 1. Create a Google Cloud OAuth Desktop Client
1. Create or select a Google Cloud project: https://console.cloud.google.com/projectcreate
2. Enable both APIs on that project:
- Gmail API (`gmail.googleapis.com`): https://console.cloud.google.com/apis/library/gmail.googleapis.com
- Gmail MCP API (`gmailmcp.googleapis.com`): https://console.cloud.google.com/apis/library/gmailmcp.googleapis.com
3. Configure the OAuth Consent Screen: https://console.cloud.google.com/auth/overview
- Audience: **External** (Testing mode) and add your Gmail address under **Test users**.
4. Create an OAuth Client ID: https://console.cloud.google.com/auth/clients/create
- Application type: **Desktop app**
- Check **"Use this client for an AI-powered agent"** (required by `gmailmcp.googleapis.com`).
- Download the JSON file and save it as `~/.gmail-mcp/gcp-oauth.keys.json`.
### 2. Authorize Once
```bash
node gmail-mcp-bridge.mjs auth
# Or for full mode:
# node gmail-mcp-bridge.mjs auth --mode=full
```
Verify config, token state, and upstream reachability:
```bash
node gmail-mcp-bridge.mjs status
```
> Note: Personal `@gmail.com` OAuth apps in Google Cloud "Testing" mode expire refresh tokens after 7 days. If you see `invalid_grant`, re-run `node gmail-mcp-bridge.mjs auth`.
### 3. Wire Your MCP Clients
Use `wire_clients.py` (Python 3.11+, stdlib only) to atomically add the bridge to standard client configs with backups and key-preservation checks:
```bash
# Preview changes without modifying any files
python wire_clients.py --dry-run
# Wire ~/.gemini/config/mcp_config.json, ~/.claude.json, and ~/.codex/config.toml in --mode=safe
python wire_clients.py --mode=safe
# Wire additional custom JSON or TOML config files
python wire_clients.py --mode=safe --extra-json /path/to/mcp_config.json --extra-toml /path/to/config.toml
```
Or configure manually in any stdio MCP client:
```json
{
"mcpServers": {
"gmail": {
"command": "node",
"args": ["/absolute/path/to/gmail-mcp-bridge.mjs", "--mode=safe"]
}
}
}
```
## Environment Variables
| Variable | Default | Description |
| --- | --- | --- |
| `GMAIL_MCP_MODE` | `safe` | Default mode when `--mode=...` is not passed (`safe` or `full`). |
| `GMAIL_MCP_CONFIG_DIR` | `~/.gmail-mcp` | Directory for `gcp-oauth.keys.json` and `tokens.json`. |
| `GMAIL_MCP_KEYS_PATH` | `<CONFIG_DIR>/gcp-oauth.keys.json` | Override path to Google Cloud OAuth client JSON. |
| `GMAIL_MCP_TOKENS_PATH` | `<CONFIG_DIR>/tokens.json` | Override path to stored OAuth tokens JSON. |
| `GMAIL_MCP_PROJECT_ID` | `project_id` from keys JSON | Override `x-goog-user-project` header. |
| `GMAIL_MCP_ENDPOINT` | `https://gmailmcp.googleapis.com/mcp/v1` | Override upstream Streamable HTTP MCP endpoint. |
## Tests
```bash
# Hermetic E2E stdio suite (mock Streamable HTTP server: JSON + SSE, Mcp-Session-Id, all 11 safe-mode blocks)
npm test
# Also verify unauthenticated initialize + tools/list against live Google Gmail MCP
npm run test:live
```
See [FAILURE_MODES.md](FAILURE_MODES.md) for the security and reliability design rationale.
## License
MIT - see [LICENSE](LICENSE).
This server cannot be deployed
Maintenance
ActivityMaintained
ResponsivenessNo recent community issues