gmail-tidy-mcp is an MCP (Model Context Protocol) server that lets AI assistants read and organize multiple Gmail accounts at the same time. Its core design goal is to give the model a smaller permission commitment: the assistant can help organize the inbox but cannot send or delete any email.
Purpose and main capabilities
- Multi-account management: each account is assigned a short label, and operations can be specified in natural language, for example "archive all newsletters older than one month in the work account."
- Email operations: search and read emails, add or modify labels, archive, set filters, and compose drafts.
- Operations explicitly not performed: do not send, reply, forward, or delete emails, and do not move emails to trash or spam. The toolset is fixed in code, and the model cannot call arbitrary Gmail endpoints or add sending or deletion capabilities on its own. Tests cause the build to fail if send or delete calls appear in the code.
Security mechanisms
- Permission scopes: because Google does not have a scope for "allow drafts but not sending," the server requests gmail.modify (which technically includes sending) and gmail.settings.basic (for filters). The no-send guarantee comes from the code, not from token permissions.
- Token protection: tokens are stored encrypted with AES-256-GCM in ~/.config/gmail-tidy-mcp/tokens.enc (permissions 600). The key is stored in the macOS Keychain, Linux Secret Service (secret-tool), or a DPAPI-protected file on Windows, and can also be provided by the user via GMAIL_TIDY_KEY (64 hexadecimal characters).
- Login flow: uses PKCE and a random state, and redirects are restricted to 127.0.0.1. After login, the actual address obtained is verified with Gmail; if it does not match the requested account or the scopes are inconsistent, the token is revoked and not saved.
- Own OAuth client: no shared client or third-party server is used, and emails go only from Google to the user's local machine.
- Untrusted content isolation: content read from Gmail is placed in blocks with a random marker for each call to prevent emails from closing the block early; header control characters are removed; HTML retains only visible text, discarding scripts, styles, and content hidden via display:none, visibilityHidden text hidden via :hidden, font-size:0, opacity:0.
- Invisible character cleanup: strips Unicode tag characters, zero-width characters, and text direction control characters from headers, body, and attachments; tag characters are reported.
- Server-side security hints: get_email and get_attachment append a findings list above untrusted content blocks, including typical prompt injection phrasing, SPF/DKIM/DMARC failures, Reply-To pointing to a free mailbox when the sender fails DMARC, display names spoofing other addresses, mismatches between link display text and actual target, etc. Gmail and Outlook redirect wrappers are unwrapped first; tracking links in mailing lists are not flagged.
- Lookalike sender flagging: a warning is given when the sender domain differs from a connected account by one or two letters, or uses characters from other writing systems that resemble Latin letters; webmail such as Gmail compares mailbox names; messages between your own accounts are not flagged.
- Draft attachment restrictions: attachments must be located in Downloads, Desktop, or Documents (or directories specified by GMAIL_TIDY_ATTACH_DIRS), and must not be in hidden folders or the server's own configuration directory; links are resolved before checking, preventing shortcuts from pointing to ~/.ssh.
- Local audit log: labels, archiving, filters, drafts, signature changes, and saved files are appended to ~/.config/gmail-tidy-mcp/audit.log (permissions 600); message bodies, draft text, or signatures themselves are not recorded; recent_changes can be used to view changes.
- Dangerous attachment blocking: executables and scripts, disguised names (such as factura.pdf.exe, right-to-left override tricks), files whose bytes do not match the extension, and archives containing the above are refused for saving unless allow_risky: true is specified at call time. Password-protected archives, macro-enabled Office files, and PDFs containing JavaScript or auto-run actions are flagged. Saved files do not overwrite existing files, cannot be written outside the Downloads folder, and on macOS are given a quarantine flag so Gatekeeper can check them.
- Draft header injection prevention: rejects line breaks in To, Cc, Subject, or reply headers to prevent hidden Bcc.
- Parameter validation and MCP annotations: parameters are inValidation before reaching Gmail; modification-type tools carry the destructiveHint annotation, so clients can ask the user first.
- Quota handling: sender_stats, newsletters, and search are fetched in batches and back off when Gmail requires it; large mailboxes become slower rather than failing.
- Dependencies: only two runtime dependencies (@modelcontextprotocol/sdk and googleapis), pinned to exact versions, neither with install scripts.
Usage Requires Node.js 22 or later.
- Create a Google OAuth client (one-time): create a project in Google Cloud Console and enable the Gmail API; in Google Auth Platform, set the app to External, add your email as the support and contact address, and publish it (Audience → Publish app). Unverified apps in production retain refresh tokens; in testing mode they expire after 7 days. In Clients, create a Desktop app type client and download the JSON, save it as ~/.config/gmail-tidy-mcp/oauth-client.json, and run chmod 600 on macOS and Linux. When connecting an account, Google will warn that the app is unverified; this is expected, just continue through Advanced.
- Register with an MCP client: other MCP clients can run npx -y gmail-tidy-mcp via stdio. To run from source, clone the repository, run npm ci --ignore-scripts, and point the client to node /path/to/gmail-tidy-mcp/bin/gmail-tidy-mcp.js. On Linux, install libsecret-tools first so the key is stored in the keyring.
- Connect an account: ask the assistant to connect an account, which opens the Google sign-in page in the browser; you can also do this from the terminal. Removing an account deletes its token locally; to also revoke access on the Google side, go to myaccount.google.com/permissions. Google Workspace accounts may require an administrator to allow unverified apps.
Use cases Suited for users who want an AI assistant to help organize multiple Gmail inboxes but do not want to grant send and delete permissions, for example bulk-archiving old newsletters, setting up filters, or writing drafts for review.
Limitations and risks
- Prompt injection cannot be made completely impossible; security prompts are heuristic rules, and a clean result does not mean it is safe. A malicious email that persuades the model could still cause re-labeling or archiving of emails (reversible), creation of filters that hide new mail (only when skip_inbox: true, and clients can ask first), deletion of filters (unrecoverable), writing misleading drafts (not sent automatically), renaming or recoloring labels, changing signatures or rewriting drafts (reversible, audit log retains changes), and saving flagged dangerous attachments when allow_risky: true is also set (whether to open them is still up to the user).
- The token itself has send capability; the "never send" guarantee is in the code, not in the token; if the token and key are both stolen, an attacker gains the capabilities allowed by gmail.modify.
- On macOS, the Keychain item trusts /usr/bin/security, so any process running as the current user can read the key without prompting; this avoids the key touching disk, but cannot defend against malware running as that user.
Test coverage includes the no-send/no-delete guarantee, token encryption and key handling, login callback (state, errors, timeouts), post-login checks, draft header and body decoding; CI runs on Linux, macOS, and Windows, including the real DPAPI path on Windows. The project started from gx-55/multi-gmail-mcp (MIT), retains some building blocks, and storage, login flow, toolset, and security model were redesigned; licensed under MIT.