Gmail MCP Server

Gmail MCP Server

Production-ready MCP server for Gmail, enabling AI agents to search, read, send, draft, and manage emails, labels, and attachments via the Google Gmail API.

Category
访问服务器

README

Gmail MCP Server

Production-ready Model Context Protocol (MCP) server for Gmail. Exposes search, read, send, draft, attachment, label, and delete operations to AI agents via FastMCP and the Google Gmail REST API.

Architecture

AI Agent (Claude, Cursor, VS Code)
        │
        ▼
   MCP Protocol (stdio)
        │
        ▼
   FastMCP Server (app.py)
        │
   ┌────┴────┬──────────┐
   ▼         ▼          ▼
 Tools   Resources   Prompts
   │         │          │
   └────┬────┴──────────┘
        ▼
   GmailClient (gmail_client.py)
        │
        ▼
   Authentication (auth.py)
        │
        ▼
   Google Gmail REST API

Design principles:

  • MCP tools never call Google APIs directly — all requests go through GmailClient
  • Authentication lives exclusively in auth.py
  • Configuration lives exclusively in config.py
  • Business logic is separated from the MCP layer

Features

Tools

Tool Description
search_email Search by sender, recipient, subject, label, date, attachments, unread, or custom query
read_email Read full email with subject, body, attachments, labels, thread ID
send_email Send plain text or HTML with CC, BCC, attachments
create_draft Create drafts with HTML and attachments
download_attachment Download a single attachment
download_all_attachments Download all attachments from a message
save_attachment Save attachment to local disk
list_labels List all Gmail labels
create_label Create a new label
delete_label Delete a user label
modify_label Update label properties
delete_email Permanently delete a message
trash_email Move message to trash
restore_email Restore message from trash

Resources (read-only)

URI Description
gmail://inbox Recent inbox messages
gmail://unread Unread messages
gmail://labels All labels
gmail://drafts Draft messages
gmail://recent Messages from the last 7 days

Prompts

Prompt Description
summarize_unread_emails Search unread → read → summarize workflow
find_invoice_emails Find invoices → download PDFs → return metadata

Installation

Prerequisites

  • Python 3.11+
  • A Google Cloud project with Gmail API enabled
  • OAuth 2.0 Desktop client credentials

Setup

# Clone or navigate to the project
cd gmail_mcp_server

# Create virtual environment
python3 -m venv .venv
source .venv/bin/activate

# Install dependencies
pip install -r requirements.txt

# Configure environment
cp .env.example .env

Place your OAuth client secrets file at credentials.json in the project root (or set GMAIL_CREDENTIALS_PATH in .env).

Docker (recommended)

Run the server in a container — no local Python virtualenv required.

Prerequisites

  • Docker and Docker Compose
  • credentials.json in the project root
  • touch token.json before first run (empty file so Docker can mount it)

First-time OAuth (inside Docker)

docker compose --profile auth run --rm --service-ports gmail-auth

Opens a browser on your machine. After sign-in, token.json is saved on the host.

Run the MCP server

docker compose run --rm -i gmail-mcp

Connect Cursor via Docker

{
  "mcpServers": {
    "gmail": {
      "command": "docker",
      "args": [
        "compose",
        "-f",
        "/Users/varunnegi/gmail_mcp_server/docker-compose.yml",
        "run",
        "--rm",
        "-i",
        "gmail-mcp"
      ]
    }
  }
}

Use the absolute path to your docker-compose.yml.

Docker vs local venv

Approach Pros Cons
Docker Reproducible, no local Python setup, easy cleanup Slightly slower startup, OAuth needs one extra step
Local venv Faster startup, simpler OAuth in browser Must manage Python version and dependencies

You can delete .venv if you use Docker exclusively.

Local Installation (without Docker)

Prerequisites

  • Python 3.11+
  • A Google Cloud project with Gmail API enabled
  • OAuth 2.0 Desktop client credentials

Setup

# Clone or navigate to the project
cd gmail_mcp_server

# Create virtual environment
python3 -m venv .venv
source .venv/bin/activate

# Install dependencies
pip install -r requirements.txt

# Configure environment
cp .env.example .env

Place your OAuth client secrets file at credentials.json in the project root.

  1. Go to Google Cloud Console
  2. Create a new project (or select an existing one)
  3. Enable the Gmail API: APIs & Services → Library → search "Gmail API" → Enable
  4. Configure OAuth consent screen: APIs & Services → OAuth consent screen
    • Choose External (or Internal for Workspace)
    • Add scopes: gmail.readonly, gmail.send, gmail.modify, gmail.labels, gmail.compose
    • Add your email as a test user (while in testing mode)
  5. Create credentials: APIs & Services → Credentials → Create Credentials → OAuth client ID
    • Application type: Desktop app
    • Download the JSON file and save as credentials.json

OAuth Setup

On first run, the server opens a browser window for Google OAuth consent. After approval, a token.json file is created and reused on subsequent runs. Tokens refresh automatically.

python app.py

To force re-authentication, delete token.json and restart.

Running the Server

source .venv/bin/activate
python app.py

The server uses stdio transport — it communicates over stdin/stdout with the MCP host.

Connecting Claude Desktop

Edit ~/Library/Application Support/Claude/claude_desktop_config.json:

{
  "mcpServers": {
    "gmail": {
      "command": "/path/to/gmail_mcp_server/.venv/bin/python",
      "args": ["/path/to/gmail_mcp_server/app.py"]
    }
  }
}

Restart Claude Desktop after saving.

Connecting Cursor

Add to Cursor MCP settings (.cursor/mcp.json or Settings → MCP):

{
  "mcpServers": {
    "gmail": {
      "command": "/path/to/gmail_mcp_server/.venv/bin/python",
      "args": ["/path/to/gmail_mcp_server/app.py"]
    }
  }
}

Connecting VS Code

Add to .vscode/mcp.json in your workspace:

{
  "servers": {
    "gmail": {
      "type": "stdio",
      "command": "/path/to/gmail_mcp_server/.venv/bin/python",
      "args": ["/path/to/gmail_mcp_server/app.py"]
    }
  }
}

Example MCP Tool Calls

Search unread emails from a sender:

{
  "tool": "search_email",
  "arguments": {
    "sender": "billing@example.com",
    "unread": true,
    "max_results": 10
  }
}

Read a specific email:

{
  "tool": "read_email",
  "arguments": {
    "message_id": "18abc123def456"
  }
}

Send an email:

{
  "tool": "send_email",
  "arguments": {
    "to": "recipient@example.com",
    "subject": "Hello from MCP",
    "body": "This email was sent via the Gmail MCP Server."
  }
}

Troubleshooting

Issue Solution
credentials.json not found Download OAuth client JSON from Google Cloud Console
Browser auth fails Ensure redirect URI http://localhost is allowed for Desktop OAuth
TokenExpiredError Delete token.json and re-authenticate
403 Insufficient Permission Re-auth with updated scopes; delete old token.json
Server not appearing in host Verify absolute paths in MCP config; check python app.py runs without errors
Empty search results Confirm Gmail query syntax; try custom_query: "in:inbox"

Enable debug logging:

LOG_LEVEL=DEBUG python app.py

Security

  • OAuth tokens and email bodies are never logged
  • Attachment contents are never logged
  • Gmail queries are sanitized before API calls
  • Never commit credentials.json, token.json, or .env

Project Structure

gmail_mcp_server/
├── app.py              # FastMCP entry point
├── config.py           # Configuration and constants
├── auth.py             # OAuth2 authentication
├── gmail_client.py     # Gmail API business layer
├── requirements.txt
├── .env.example
├── tools/              # MCP tool modules
├── resources/          # MCP read-only resources
├── prompts/            # MCP prompt templates
└── utils/              # Parser, formatter, logger, exceptions

Future Roadmap

  • [ ] Google Drive MCP integration
  • [ ] Microsoft Outlook MCP integration
  • [ ] Slack MCP integration
  • [ ] Notion MCP integration
  • [ ] SharePoint MCP integration
  • [ ] Batch email operations
  • [ ] Pub/Sub push notification support
  • [ ] Multi-account support
  • [ ] HTTP transport for remote deployment

License

Apache 2.0

推荐服务器

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 模型以安全和受控的方式获取实时的网络信息。

官方
精选