Gmail MCP Server

Gmail MCP Server

Enables AI assistants to review unread emails and perform email management operations like delete, archive, and label through Gmail integration.

Category
访问服务器

README

Gmail MCP Server

A purpose-built Model Context Protocol (MCP) server for Gmail integration, allowing AI assistants to review unread emails and perform email management operations.

Features

  • List Unread Emails: Retrieve unread emails from Gmail inbox with optional subject filtering
  • Email Content: Access complete email content including headers, body, and metadata
  • Delete Emails: Permanently delete emails by ID
  • Archive Emails: Archive emails (remove from inbox) by ID
  • Web Dashboard: Beautiful, responsive dashboard for intelligent inbox management
  • Auto-Triage: Automatic email classification and organization every 15 minutes
  • Auto-Cleanup: Intelligent deletion of trivial emails and archiving of calendar invites

Installation

  1. Clone this repository:
git clone <repository-url>
cd gmail-mcp-server
  1. Install dependencies:
pip install -r requirements.txt
  1. Set up Google OAuth 2.0 credentials:
    • Go to Google Cloud Console
    • Create a new project or select an existing one
    • Enable the Gmail API
    • Create OAuth 2.0 credentials (Desktop application)
    • Download the credentials JSON file and save as credentials.json in the project root

Start the server:

python -m gmail_mcp_server.server

Web Dashboard & Inbox Management

The Gmail MCP Server includes a powerful web-based dashboard for intelligent inbox management with automatic triaging and organization.

Quick Start

Start the dashboard with:

make dashboard

Or manually:

python3 app.py

The dashboard will be available at http://localhost:5000

Dashboard Features

  • Auto-Triage Every 15 Minutes: Automatically classifies and organizes emails
  • Intelligent Organization: Groups emails by priority (Critical → Important → Info)
  • Auto-Cleanup: Automatically deletes trivial field changes and archives calendar invites
  • Real-time Stats: View total emails, last sync time, and next sync countdown
  • Quick Navigation: Click email groups to preview Gmail search results
  • Responsive Design: Works on desktop, tablet, and mobile devices
  • Manual Refresh: Trigger triage immediately with the refresh button

Using with Claude Code

When using Claude Code, you can leverage this Gmail MCP server to manage your email directly from your development environment:

  1. Inbox Triaging: Use the /triage command to automatically organize and clean your inbox
  2. Integration in Workflows: Claude Code can help analyze email content and suggest actions
  3. Automated Management: Set up the dashboard to run in the background and manage emails while you code
  4. Easy Access: Check your organized inbox without leaving your IDE

To use with Claude Code:

  1. Ensure the MCP server is configured in your .mcp.json
  2. Claude Code will have access to the Gmail tools for email management
  3. Use natural language commands to manage emails (e.g., "delete these spam emails", "archive calendar invites")

See DASHBOARD.md for comprehensive dashboard documentation.

MCP Configuration

To use this Gmail MCP server with Claude or gemini-cli, you need to configure a .mcp.json file. This file tells the AI assistant how to connect to your MCP server.

.mcp.json Configuration

Create a .mcp.json file in your home directory or project directory with the following configuration:

{
  "mcpServers": {
    "gmail": {
      "command": "python",
      "args": ["-m", "gmail_mcp_server.server"],
      "cwd": "/path/to/gmail-mcp-server"
    }
  }
}

Configuration Details:

  • command: The Python interpreter to use
  • args: Arguments to pass to the Gmail MCP server module
  • cwd: The working directory where the Gmail MCP server is installed

For Claude Desktop: Place the .mcp.json file in your Claude Desktop configuration directory:

  • macOS: ~/Library/Application Support/Claude/
  • Windows: %APPDATA%\Claude\
  • Linux: ~/.config/claude/

For gemini-cli: Place the .mcp.json file in your home directory or specify the path when running gemini-cli.

Example Usage

Once configured, you can use the Gmail MCP server with AI assistants by passing it in your client configuration.

Dashboard PIN Security

The dashboard can be protected with a 4-digit PIN. When configured, the dashboard shows a PIN entry screen on every new session (sessions last 4 hours).

Setting a PIN

make set-pin
# Enter new PIN: ****
# Confirm PIN: ****
# PIN saved.

Or use the Python CLI directly:

python3 app.py --set-pin

This writes a PBKDF2-SHA256 hashed PIN to .pincode in the project root. The raw PIN is never stored. Both .pincode and .flask_secret are gitignored.

To remove PIN protection, delete .pincode:

rm .pincode

Running in Kubernetes

All secrets are consolidated in a single gmail-mcp-secrets Kubernetes Secret (see k8s/secret.yaml_example). When using PIN protection, include the pre-hashed .pincode value there rather than generating it on-disk.

1. Generate the PIN hash locally:

make set-pin        # writes .pincode to repo root
cat .pincode        # copy the "salt:hash" string

Or generate it directly:

python3 -c "
import secrets, hashlib
pin = '1234'  # replace with your PIN
salt = secrets.token_hex(16)
h = hashlib.pbkdf2_hmac('sha256', pin.encode(), salt.encode(), 260000).hex()
print(f'{salt}:{h}')
"

2. Add it to your k8s/secret.yaml (alongside the other secrets):

stringData:
  .pincode: "salt:hash-from-above"
  FLASK_SECRET_KEY: "$(python3 -c 'import secrets; print(secrets.token_hex(32))')"
  # ... other fields from k8s/secret.yaml_example

3. Apply and deploy:

kubectl apply -f k8s/secret.yaml
kubectl apply -f k8s/deployment.yaml

The entrypoint copies .pincode from the read-only /secrets/ mount to /app/ on startup. FLASK_SECRET_KEY is injected as an environment variable to keep sessions stable across pod restarts.

Make Commands

Use the included Makefile for quick access to common tasks:

# Display available commands
make help

# Initialize Gmail OAuth authentication (requires credentials.json)
make auth

# Set or change the dashboard PIN
make set-pin

# Start the web dashboard
make dashboard

# Stop the running dashboard
make kill-dashboard

# Run inbox triage once (email classification and organization)
make triage

# Watch inbox every 10 minutes (runs triage repeatedly)
make watch

You can specify which Claude model to use with the MODEL variable:

make triage MODEL=haiku        # Fast triage with Haiku (default)
make triage MODEL=sonnet       # Balanced triage with Sonnet
make triage MODEL=opus         # Most capable triage with Opus
make watch MODEL=opus

Available Tools

1. list_unread_emails

Lists unread emails in Gmail inbox with optional filtering. Rebuilds the in-memory position map used by delete/archive/modify tools.

Parameters:

  • subject_filter (optional): Filter emails by subject text
  • max_results (optional): Maximum number of emails to return (default: 50)

2. delete_emails

Moves emails to trash and marks them as read. Accepts position numbers from the last list_unread_emails call and/or explicit Gmail message IDs.

Parameters:

  • positions (optional): Array of 1-based position numbers from the email list
  • message_ids (optional): Array of Gmail message IDs

3. archive_emails

Archives emails (removes from inbox) and marks them as read.

Parameters:

  • positions (optional): Array of 1-based position numbers
  • message_ids (optional): Array of Gmail message IDs

4. list_labels

Returns all Gmail labels (system + user-defined).

Parameters: None

5. create_label

Creates a new Gmail label with optional color.

Parameters:

  • name (required): Label name (e.g., Triage/Security)
  • background_color (optional): Hex color (e.g., #4a86e8) — must be a predefined Gmail color
  • text_color (optional): Hex text color — must be paired with background_color

6. modify_labels

Adds and/or removes labels on emails. When adding a Triage/* label, all other Triage/* labels on the email are automatically removed (one-label-per-email invariant).

Parameters:

  • positions (optional): Array of 1-based position numbers
  • message_ids (optional): Array of Gmail message IDs
  • add_labels (optional): Array of label names to add
  • remove_labels (optional): Array of label names to remove

7. list_recent_actions

Returns the in-memory log of recent email operations (capped at 100).

Parameters:

  • limit (optional): Maximum number of actions to return (default: 10)

Authentication

Initial Setup

On first run, the server requires authentication. Use the provided authentication helper:

make auth

Or manually:

python -m gmail_mcp_server.auth

This will:

  1. Check that credentials.json exists in the project root
  2. Open a browser window for OAuth 2.0 authentication
  3. Request permission to access your Gmail account
  4. Save the authentication token to token.json for future use

Getting Credentials

Before running make auth, you need to set up Google OAuth 2.0 credentials:

  1. Go to Google Cloud Console
  2. Create a new project or select an existing one
  3. Enable the Gmail API
  4. Create OAuth 2.0 credentials (Desktop application)
  5. Download the credentials JSON file and save as credentials.json in the project root

How It Works

  • The server checks for an existing authentication token (token.json) on startup
  • If the token exists and is valid, the server uses it automatically
  • If the token is expired but has a refresh token, it refreshes automatically
  • If no token exists, the server will request authentication using the make auth command

Required Gmail API Scopes

  • https://www.googleapis.com/auth/gmail.readonly - Read emails
  • https://www.googleapis.com/auth/gmail.modify - Delete and archive emails

Security Notes

  • Keep your credentials.json and token.json files secure
  • These files are automatically ignored by git
  • The server only requests minimal required permissions
  • All operations are performed through official Gmail API

Development

Install with dev dependencies:

pip install -e ".[dev]"

Run tests:

make test          # run all tests
make test-cov      # run with coverage report

Lint and format:

make lint          # check with ruff
make format        # auto-format and fix imports with ruff

Run the MCP server directly:

python -m gmail_mcp_server        # short form (via __main__.py)
python -m gmail_mcp_server.server # explicit
gmail-mcp-server                  # installed entry point

Test the server interactively with the MCP Inspector:

npx @modelcontextprotocol/inspector python3 -m gmail_mcp_server.server

推荐服务器

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

官方
精选