gmail-reader-mcp
# gmail-reader-mcp
MCP (Model Context Protocol) server for reading Gmail through the Gmail API. Read-only by design: list, search, and fetch emails and whole threads — single or batched — and download attachments safely to disk.
## Tools
| Tool | Description |
| ---------------------- | ------------------------------------------------------------------------------------ |
| `authenticate` | Sign in with a Google URL shown directly in the chat; reports current account |
| `sign_out` | Revoke access at Google and remove the saved session; cancels any pending sign-in |
| `list_emails` | List emails in a label (default `INBOX`), optionally narrowed by a Gmail query |
| `search_emails` | Search all mail with Gmail search syntax (`from:`, `has:attachment`, `after:`, …) |
| `get_email` | Full content of one email (plain-text body, HTML converted) plus its attachment list |
| `get_emails` | Full content of up to 50 emails in one call |
| `get_thread` | Every message of a conversation, in order |
| `list_labels` | System and user labels with IDs and unread/total counts |
| `list_attachments` | Attachment part IDs, names, sizes, types, and inline flag for an email |
| `download_attachment` | Save one attachment to disk |
| `download_attachments` | Save several (or all) attachments of an email in one call |
## Setup
### 1. Create a Google Cloud OAuth client
You need your own OAuth client of type **Desktop app** with the Gmail API enabled. Using the Gmail API is free; no billing account is required.
**Option A — personal mode (recommended).** Open Google Cloud's [personal mode onboarding](https://console.cloud.google.com/auth/overview/personal?apis=ws&clientType=desktop) (the link preselects a **Desktop app** client and enables the Google Workspace APIs, Gmail included). Choose **Create a new project**, give the client a name, accept the User Data Policy, and click **Continue**. Copy the **Client ID** and **Client secret** it shows. The client only works for your own Google account and needs no consent-screen setup, test users, or app verification, so sign-in shows no "unverified app" warning.
**Option B — standard project** (if you can't use personal mode).
1. In the [Google Cloud console](https://console.cloud.google.com/projectcreate), create a project.
2. Enable the [Gmail API](https://console.cloud.google.com/apis/library/gmail.googleapis.com) for it.
3. Open [Google Auth Platform](https://console.cloud.google.com/auth/overview) and configure it: any app name, your email as support/contact, audience **External**.
4. Under **Data Access**, add the scope `https://www.googleapis.com/auth/gmail.readonly`.
5. Under **Audience**, add your Google account as a **test user**.
6. Under **Clients**, create a client of type **Desktop app**, and copy its **Client ID** and **Client secret**.
> **Testing mode limits:** while a standard project's publishing status is _Testing_, Google expires refresh tokens after 7 days, so you will be asked to sign in again about once a week (the tools tell the assistant when). Publishing the app removes that limit but requires a homepage, a privacy-policy URL and authorized domains on the Branding page. Standard projects also show a "Google hasn't verified this app" screen at sign-in — choose **Continue**, since it is your own app.
### 2. Configure your MCP client
```json
{
"mcpServers": {
"gmail-reader": {
"command": "npx",
"args": ["-y", "gmail-reader-mcp"],
"env": {
"GOOGLE_CLIENT_ID": "your-client-id.apps.googleusercontent.com",
"GOOGLE_CLIENT_SECRET": "your-client-secret"
}
}
}
}
```
Optional: `GMAIL_ACCOUNT=you@gmail.com` pre-selects that account on the Google sign-in page.
### 3. First run
Ask your assistant to run the `authenticate` tool (or just ask it to read your mail — any tool that needs auth will tell it to). The chat shows a Google sign-in URL: open it in a browser on the same machine and grant read-only Gmail access. The browser redirects to a short-lived listener on `127.0.0.1`, which completes the sign-in. The session is saved, so this only happens once per machine.
Google's device-code flow does not allow Gmail scopes, so the server uses the loopback flow (authorization code + PKCE) recommended for desktop apps.
## Credentials & security
- The OAuth client secret is never bundled with the package: you provide it via environment variables. For "Desktop app" clients, Google does not treat it as confidential, but keep it out of version control anyway.
- The refresh token is stored in the OS credential store (Windows Credential Manager / macOS Keychain / Linux Secret Service). If none is available it falls back to a permission-restricted file in the per-user app-data directory (`%LOCALAPPDATA%\gmail-reader-mcp`, `~/Library/Application Support/gmail-reader-mcp`, or `$XDG_CONFIG_HOME/gmail-reader-mcp`) and warns on stderr.
- The only scope requested is `gmail.readonly`; this server cannot send, modify, or delete mail.
- Attachment filenames are sanitized (path traversal, reserved characters) before writing to disk, and existing files are never overwritten.
- `sign_out` revokes the token at Google and deletes it locally. You can also revoke access anytime at [myaccount.google.com/permissions](https://myaccount.google.com/permissions).
## Development
```bash
npm install
cp .env.example .env # fill in GOOGLE_CLIENT_ID / GOOGLE_CLIENT_SECRET
npm run mcp # run from source (tsx)
npm run build # compile to dist/
npm run format # prettier
```
## License
GPL-3.0
TDQS
Scored across 11 tools
Most tools are cleanly separated, and the descriptions explicitly cross-reference alternatives (get_email vs get_emails vs get_thread, download_attachment vs download_attachments). The main soft spot is list_emails vs search_emails: list_emails also accepts a Gmail search query, so an agent may hesitate over which to use. That overlap is real but minor, since one is label-scoped paging and the other is global search.
Almost everything follows a consistent snake_case verb_noun pattern (list_emails, get_thread, list_labels, download_attachment). The singular/plural pairs (get_email/get_emails, download_attachment/download_attachments) are systematic rather than confusing. Only the bare verbs authenticate and sign_out break the noun pattern, which is a small deviation.
Eleven tools is well within the ideal range and every one earns its place: two auth lifecycle tools, listing/search/read tools, thread and label inspection, and attachment listing plus single/bulk download. Nothing looks redundant or filler.
For a deliberately read-only Gmail reader the lifecycle is nearly complete: sign-in, sign-out, label discovery, paged listing, search, single/bulk message fetch, threads, and attachment listing plus download. Minor gaps exist (no profile/account info tool, no way to page or list threads directly, attachments only saved to disk), but agents can work around all of these.