Gmail MCP Server

Gmail MCP Server

Enables natural language interaction with Gmail, providing email search, categorized daily summaries, and Home Assistant integration through REST API with read-only access to your inbox.

Category
访问服务器

README

Gmail MCP Server

A Model Context Protocol (MCP) server for Gmail that provides natural language interaction with your inbox, daily summaries organized by category, and Home Assistant integration.

Features

  • 🔍 Natural Language Search - Search emails using plain language or Gmail query syntax
  • 📊 Category-Based Summaries - Automatic categorization: Navy, Kids, Financial, Action Items
  • 📱 Home Assistant Integration - REST API with sensors and notifications support
  • 🐳 Docker Support - Easy deployment with Docker Compose
  • 🔒 Read-Only - Currently read-only access to your inbox (safe!)

Quick Start

1. Google Cloud Setup

  1. Go to Google Cloud Console
  2. Create a new project or select existing
  3. Enable the Gmail API:
    • Navigate to "APIs & Services" → "Enable APIs"
    • Search for "Gmail API" and enable it
  4. Create OAuth credentials:
    • Go to "APIs & Services" → "Credentials"
    • Click "Create Credentials" → "OAuth client ID"
    • Choose "Web application" (or "Desktop app")
    • For Web application, add http://localhost:8080 to Authorized redirect URIs
    • Copy the Client ID and Client Secret

2. Configuration

# Clone and enter directory
cd mcp_gmail

# Copy example environment file
cp .env.example .env

# Edit .env with your credentials
# GOOGLE_CLIENT_ID=your-client-id.apps.googleusercontent.com
# GOOGLE_CLIENT_SECRET=your-client-secret

3. Initial Authentication (OAuth)

For the first-time setup, you need to authenticate with Google:

# Run the auth setup container
docker compose --profile setup run --rm gmail-mcp-auth

This will:

  1. Open a browser window for Google authentication
  2. Request permission to read your Gmail
  3. Save the OAuth token to ./credentials/token.json

4. Start the Server

# Start the main server
docker compose up -d

# Check logs
docker compose logs -f gmail-mcp

The REST API will be available at http://localhost:8000

API Endpoints

Health & Status

Endpoint Description
GET /health Health check with auth status
GET /api/stats Inbox statistics

Email Data (for Home Assistant Sensors)

Endpoint Description
GET /api/unread Total unread count
GET /api/unread/categories Unread count per category

Summaries

Endpoint Description
GET /api/summary/daily Full daily summary (JSON)
GET /api/summary/daily/text Daily summary as text
GET /api/summary/category/{category} Category-specific summary

Home Assistant Integration

Endpoint Description
POST /api/webhook/trigger Send summary to HA webhook

Home Assistant Configuration

REST Sensors

Add to your configuration.yaml:

sensor:
  - platform: rest
    name: Gmail Unread
    resource: http://gmail-mcp:8000/api/unread
    value_template: "{{ value_json.unread }}"
    scan_interval: 300
    json_attributes:
      - timestamp

  - platform: rest
    name: Gmail Categories
    resource: http://gmail-mcp:8000/api/unread/categories
    value_template: "{{ value_json.total }}"
    scan_interval: 300
    json_attributes:
      - navy
      - kids
      - financial
      - action_required
      - other

Template Sensors (from attributes)

template:
  - sensor:
      - name: "Gmail Navy Unread"
        state: "{{ state_attr('sensor.gmail_categories', 'navy') }}"
        icon: mdi:anchor

      - name: "Gmail Kids Unread"
        state: "{{ state_attr('sensor.gmail_categories', 'kids') }}"
        icon: mdi:human-child

      - name: "Gmail Financial Unread"
        state: "{{ state_attr('sensor.gmail_categories', 'financial') }}"
        icon: mdi:currency-usd

      - name: "Gmail Action Required"
        state: "{{ state_attr('sensor.gmail_categories', 'action_required') }}"
        icon: mdi:alert-circle

Automation: Daily Summary Notification

automation:
  - alias: "Daily Email Summary"
    trigger:
      - platform: time
        at: "07:00:00"
    action:
      - service: rest_command.gmail_summary
      - service: notify.mobile_app
        data:
          title: "📧 Email Summary"
          message: "{{ states('sensor.gmail_summary_text') }}"

rest_command:
  gmail_summary:
    url: "http://gmail-mcp:8000/api/webhook/trigger"
    method: POST

Webhook Integration

Configure in your .env:

HA_WEBHOOK_URL=http://homeassistant.local:8123/api/webhook/gmail_summary
HA_LONG_LIVED_TOKEN=your-long-lived-access-token

Then create an automation triggered by the webhook:

automation:
  - alias: "Gmail Webhook Handler"
    trigger:
      - platform: webhook
        webhook_id: gmail_summary
    action:
      - service: notify.mobile_app
        data:
          title: "📧 {{ trigger.json.event_type }}"
          message: "{{ trigger.json.data.text_summary }}"

MCP Server Usage

The MCP server can be used with Claude Desktop, Home Assistant, or other MCP clients via SSE transport.

Claude Desktop Configuration

Add to your Claude Desktop config (claude_desktop_config.json):

{
  "mcpServers": {
    "gmail": {
      "command": "npx",
      "args": ["-y", "mcp-remote", "http://YOUR_SERVER_IP:8001/mcp/sse", "--allow-http"]
    }
  }
}

Home Assistant MCP Integration

The server exposes SSE endpoints for MCP connections:

  • /mcp/sse - Primary MCP SSE endpoint
  • /sse - Alias endpoint for Home Assistant compatibility

Available MCP Tools

Tool Description
gmail_search Search emails with query and optional filters
gmail_get_email Get full email content by ID
gmail_list_unread List unread emails with optional category filter
gmail_daily_summary Generate categorized daily summary
gmail_category_summary Summary for a specific category
gmail_inbox_stats Current inbox statistics
gmail_list_labels List all Gmail labels
gmail_create_label Create a new Gmail label
gmail_delete_label Delete a Gmail label
gmail_get_categories Show configured categories
gmail_mark_as_read_by_ids Mark specific emails as read by IDs
gmail_mark_as_read_by_query Mark emails matching a query as read
gmail_send_email Send an email (plain text or HTML)
gmail_add_labels_to_messages Add labels to messages
gmail_remove_labels_from_messages Remove labels from messages

Example Queries

  • "What unread emails do I have about Navy?"
  • "Show me my daily email summary"
  • "Are there any action items I need to handle?"
  • "Search for emails from the school"
  • "What financial emails came in this week?"

Customizing Categories

Edit config/categories.yaml to customize email categorization:

categories:
  navy:
    name: "Navy / Military"
    priority: high
    matchers:
      senders:
        - "@navy.mil"
        - "@mail.mil"
      subjects:
        - "orders"
        - "deployment"
      labels:
        - "Navy"

Matcher Types

  • senders: Partial match on sender email/name
  • subjects: Partial match on subject line
  • labels: Exact match on Gmail labels

Development

Local Setup (without Docker)

# Create virtual environment
python -m venv venv
source venv/bin/activate  # or `venv\Scripts\activate` on Windows

# Install in development mode
pip install -e ".[dev]"

# Run authentication
mcp-gmail-auth

# Run the API server
mcp-gmail-api

# Or run the MCP server (stdio)
mcp-gmail

Running Tests

pytest
pytest --cov=mcp_gmail

Architecture

┌─────────────────────────────────────────────────────────────┐
│                    Gmail MCP Server                         │
├─────────────────────────────────────────────────────────────┤
│  MCP Interface (stdio)          REST API (FastAPI)          │
│  └── Tools for Claude           └── Endpoints for HA        │
├─────────────────────────────────────────────────────────────┤
│                    Gmail Client                             │
│  ├── Search & List              ├── Categorization          │
│  └── OAuth2 Auth                └── Summary Generation      │
├─────────────────────────────────────────────────────────────┤
│                    Gmail API                                │
│  └── google-api-python-client                               │
└─────────────────────────────────────────────────────────────┘

Roadmap

  • [x] Read-only email access
  • [x] Category-based summaries
  • [x] Home Assistant REST API
  • [x] Docker deployment
  • [x] Email sending
  • [x] Gmail labels management (create, delete, add to messages, remove from messages)
  • [x] Mark emails as read
  • [x] SSE transport with session isolation
  • [x] Gemini-compatible tool schemas
  • [ ] Scheduled summary notifications
  • [ ] IMAP fallback option

License

MIT License

Troubleshooting

OAuth Token Expired

# Re-run authentication
docker compose --profile setup run --rm gmail-mcp-auth

Container Won't Start

Check that credentials exist:

ls -la credentials/
# Should contain token.json

Home Assistant Can't Connect

Ensure the container is on the same Docker network or use the host IP:

resource: http://192.168.1.100:8000/api/unread

推荐服务器

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

官方
精选