multi-gmail-mcp

multi-gmail-mcp

An MCP server that provides read, label, and draft access to multiple Gmail accounts from a single server, never sending email.

Category
访问服务器

README

multi-gmail-mcp

A Model Context Protocol (MCP) server that gives an LLM read/label/draft access to multiple Gmail accounts from a single server.

The tool interface deliberately mirrors the standard Gmail MCP connector — same tool names, parameters, and semantics — with one addition: every tool accepts an optional account parameter selecting which mailbox to operate on (by alias or email). A list_accounts tool is added so the model can discover what's configured.

This server creates drafts only — it never sends email.


Tools

Tool Purpose
search_threads Search threads with full Gmail query syntax; returns per-message metadata (no bodies).
get_thread Fetch a thread; FULL_CONTENT decodes the plain-text body and lists attachments.
create_draft Build an RFC 2822 draft (text/html/attachments/replies). Never sends.
list_drafts List drafts with subject/to/snippet.
list_labels List user-defined labels (id, name, colors).
create_label Create a label (supports / nesting and palette colors).
update_label Rename / recolor a label.
delete_label Delete a user label (refuses system labels).
label_message / unlabel_message Add/remove labels on a message.
label_thread / unlabel_thread Add/remove labels on a whole thread.
list_accounts List configured accounts with email, default flag, and auth status.

Every tool above (including list_accounts) accepts an optional account parameter.

The account parameter

  • A value can be an alias ("work", "personal") or the account's email address. Matching is case-insensitive.
  • If omitted, the configured default account is used.
  • If there is no default and more than one account, the call fails with an error listing the available accounts.
  • An unknown account fails with an error listing valid accounts.

Prerequisites

  • Node.js 18+
  • A Google Cloud project with the Gmail API enabled and an OAuth Desktop-app credential (see below).

1. Google Cloud setup (one-time)

  1. Create / pick a project at https://console.cloud.google.com/.
  2. Enable the Gmail API: APIs & Services → Library → "Gmail API" → Enable.
  3. Configure the OAuth consent screen: APIs & Services → OAuth consent screen.
    • User type External (or Internal if you're in a Google Workspace org).
    • Fill in app name + support email.
    • Scopes: you can leave the scope list empty here; the server requests https://www.googleapis.com/auth/gmail.modify and https://www.googleapis.com/auth/gmail.labels at auth time.
    • Test users: while the app is in Testing, add every Gmail address you intend to connect as a test user. (Test-mode refresh tokens expire after 7 days — publish the app to Production to get long-lived tokens.)
  4. Create credentials: APIs & Services → Credentials → Create Credentials → OAuth client ID.
    • Application type: Desktop app.
    • Download the JSON (it looks like { "installed": { "client_id": ..., "client_secret": ... } }).

Desktop-app clients are allowed to use a http://localhost:<port> loopback redirect with a dynamic port, which is exactly what the auth flow uses — no redirect URIs to register.


2. Install

From npm (when published):

npm install -g multi-gmail-mcp     # or just use `npx multi-gmail-mcp ...`

From source:

git clone <this-repo> && cd multi-gmail-mcp
npm install        # also builds via the `prepare` script
npm run build      # (if needed) compile TypeScript to dist/

3. Provide your OAuth client credentials

The server resolves your OAuth app credentials from the first of these that is present:

# Option A — environment variables
export GOOGLE_CLIENT_ID="xxxxxxxx.apps.googleusercontent.com"
export GOOGLE_CLIENT_SECRET="xxxxxxxx"

# Option B — point at the downloaded credentials.json
export GOOGLE_OAUTH_CREDENTIALS="/absolute/path/to/credentials.json"

# Option C — no env var needed: just drop the downloaded credentials.json into
#            ~/.multi-gmail-mcp/credentials.json  (or the current directory)
cp ~/Downloads/client_secret_*.json ~/.multi-gmail-mcp/credentials.json

Resolution order is A → B → C. The credentials.json is the file you downloaded from Google Cloud Console; both the Desktop-app shape ({ "installed": {...} }) and the web shape ({ "web": {...} }) are accepted.

These are only the app credentials. Per-account refresh tokens are obtained in the next step.


4. Add Gmail accounts

Run the interactive auth flow once per account. It opens a browser, runs Google consent against a localhost loopback redirect, and stores the refresh token.

# alias the account however you like
npx multi-gmail-mcp auth work --default
npx multi-gmail-mcp auth personal
  • --default marks the account as the default used when account is omitted. (The first account added becomes the default automatically.)
  • Re-running auth <alias> re-authorizes (e.g. after a revoked token) and keeps the same alias.

Token storage

Tokens are written to ~/.multi-gmail-mcp/accounts.json with permissions 0600:

{
  "accounts": {
    "work":     { "email": "me@company.com", "refresh_token": "1//...", "default": true },
    "personal": { "email": "me@gmail.com",   "refresh_token": "1//..." }
  }
}

Access tokens are refreshed automatically and cached in memory per account. If a refresh token is revoked or expires, tool calls for that account return an actionable error telling you to re-run auth <alias>. (You can override the config directory with MULTI_GMAIL_MCP_HOME.)

Verify everything is connected:

# from an MCP client, call list_accounts — or check status quickly:
npx multi-gmail-mcp   # starts the server; use your MCP client to call list_accounts

5. Configure your MCP client

Claude Desktop / Claude Code (mcpServers JSON)

Add to your MCP client config (e.g. claude_desktop_config.json, or a project .mcp.json):

{
  "mcpServers": {
    "multi-gmail": {
      "command": "npx",
      "args": ["-y", "multi-gmail-mcp"],
      "env": {
        "GOOGLE_CLIENT_ID": "xxxxxxxx.apps.googleusercontent.com",
        "GOOGLE_CLIENT_SECRET": "xxxxxxxx"
      }
    }
  }
}

Running from a local checkout instead:

{
  "mcpServers": {
    "multi-gmail": {
      "command": "node",
      "args": ["/absolute/path/to/multi-gmail-mcp/dist/index.js"],
      "env": { "GOOGLE_OAUTH_CREDENTIALS": "/absolute/path/to/credentials.json" }
    }
  }
}

Claude Code CLI

claude mcp add multi-gmail \
  --env GOOGLE_CLIENT_ID=xxxx.apps.googleusercontent.com \
  --env GOOGLE_CLIENT_SECRET=xxxx \
  -- npx -y multi-gmail-mcp

The auth step must be run separately in a terminal (it opens a browser); the MCP host only runs the server.


Example invocations

Once connected, natural-language requests map to tool calls like:

  • Search the work inbox: search_threads with { "query": "in:inbox is:unread newer_than:7d", "account": "work" }
  • Read a thread from personal: get_thread with { "threadId": "18c...", "messageFormat": "FULL_CONTENT", "account": "personal" }
  • Draft a reply from personal: create_draft with { "to": ["friend@example.com"], "body": "Sounds good!", "replyToMessageId": "18c...", "account": "personal" }
  • Label a thread in work: label_thread with { "threadId": "18c...", "labelIds": ["Label_42"], "account": "work" }
  • List accounts: list_accounts with {}.

Omitting account uses the default account.


Gmail query syntax (search_threads)

Supports the full Gmail operator set, e.g. from:, to:, cc:, subject:, label:<id>, in:(inbox|sent|trash|spam|anywhere), is:(unread|starred|important), has:attachment, filename:, after:/before:YYYY/MM/DD, newer_than:7d, older_than:1y, larger:/smaller:, grouping with ()/{}, OR, and - to exclude. Use label IDs (from list_labels), not display names, with label:. Drafts are excluded by default; set includeTrash: true to also search Trash/Spam.


Development

npm run build        # compile TypeScript -> dist/
npm test             # vitest: alias resolution, MIME building, query passthrough
node scripts/smoke.mjs   # connect over stdio; verify 13 tools + optional `account`

Source layout:

File Responsibility
src/index.ts MCP server, tool registry, CLI dispatcher (auth vs. server).
src/accounts.ts Config + token store, OAuth client construction, alias resolution.
src/gmail.ts Gmail REST wrappers, query building, body decoding, retry/error mapping.
src/auth-cli.ts auth <alias> loopback OAuth flow.
src/mime.ts RFC 2822 MIME construction for drafts.

Behavior notes & limits

  • Drafts only. There is no send capability by design.
  • Bodies over ~50KB are truncated (flagged); raw attachment bytes are never returned by get_thread — only metadata + attachmentId.
  • Attachments in create_draft must total ≤ 25MB; link large files from Drive instead.
  • Rate limits / 5xx are retried up to 3 times with exponential backoff; persistent failures return a clear error.
  • Auth errors (401 / invalid_grant) tell you exactly which account to re-auth.
  • System labels can be used directly by ID (INBOX, TRASH, SPAM, STARRED, UNREAD, IMPORTANT, DRAFT, SENT); they cannot be deleted or modified.

License

MIT

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选