Gmail MCP Server

Gmail MCP Server

A Windows-first Python MCP server for Gmail that reads unread emails and creates correctly threaded draft replies using the Gmail API.

Category
访问服务器

README

Gmail MCP Server

A Windows-first Python MCP server for Gmail that reads unread emails and creates correctly threaded draft replies using the Gmail API, OAuth 2.0 Desktop credentials, and local stdio transport.

Overview

This project implements a local Model Context Protocol server for Gmail with Python 3.11 and the official MCP Python SDK using FastMCP.

It exposes two focused tools:

  • get_unread_emails
  • create_draft_reply

The server is designed for local Windows development and Claude Desktop-compatible MCP configuration. It is verified locally through Gmail OAuth, direct Python smoke testing, manual stdio server startup, and Claude Desktop configuration pickup through main.log.

What this project demonstrates

  • MCP server implementation with Python and FastMCP
  • Gmail API integration with OAuth 2.0 Desktop App credentials
  • correctly threaded Gmail draft reply creation
  • Windows-first local developer workflow
  • Claude Desktop local MCP configuration on Windows
  • practical verification of a local MCP server independent of hosted execution constraints

The key implementation detail is Gmail threading correctness. Draft replies are created using the original Gmail threadId, RFC Message-ID, In-Reply-To, References, and matching subject so that the resulting draft remains attached to the correct conversation.

Tech stack

  • Python 3.11
  • MCP Python SDK with FastMCP
  • Gmail API
  • Google OAuth 2.0 Desktop App credentials
  • local stdio MCP transport
  • Claude Desktop-compatible Windows configuration

MCP tools

get_unread_emails(limit: int = 10)

Reads unread Gmail messages from the authenticated mailbox and returns structured message data including:

  • message_id
  • thread_id
  • sender
  • subject
  • snippet
  • body_text
  • internal_date

create_draft_reply(reply_body: str, original_message_id: str | None = None, thread_id: str | None = None)

Creates a correctly threaded Gmail draft reply.

Accepted inputs:

  • original_message_id
  • thread_id

Returned metadata includes:

  • status
  • draft_id
  • draft_message_id
  • thread_id
  • reply_to
  • subject
  • source_message_id
  • in_reply_to
  • references

Architecture

Runtime flow

  1. The local MCP server exposes Gmail operations as MCP tools.
  2. OAuth credentials are loaded from local token storage or refreshed when needed.
  3. Gmail API calls are executed on behalf of the authenticated user.
  4. The server runs over stdio for local MCP compatibility.
  5. Claude Desktop can be configured to launch the server locally on Windows.

Thread-safe draft creation

To create correctly threaded Gmail draft replies, the implementation uses:

  • Gmail threadId
  • original RFC Message-ID
  • In-Reply-To
  • References
  • matching subject

This avoids creating detached drafts that appear as new conversations instead of replies.

Project structure

gmail-mcp-server/
├── .env
├── .env.example
├── .gitignore
├── authenticate.py
├── LICENSE
├── README.md
├── requirements.txt
├── run_server.py
├── smoke_test.py
├── pyproject.toml
├── gmail_mcp_server/
│   ├── __init__.py
│   ├── auth.py
│   ├── config.py
│   ├── gmail_client.py
│   └── server.py
├── secrets/
│   ├── credentials.json
│   └── token.json
└── docs/
    └── screenshots/

Local setup

Prerequisites

  • Windows
  • Python 3.11
  • Git
  • Visual Studio Code
  • a Gmail account
  • a Google Cloud project with Gmail API enabled
  • OAuth 2.0 Desktop App credentials
  • Claude Desktop for local MCP configuration

Create and activate the virtual environment

Set-Location D:\Projects\gmail-mcp-server
py -3.11 -m venv .venv
.\.venv\Scripts\Activate.ps1

If PowerShell blocks activation:

Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass
.\.venv\Scripts\Activate.ps1

Install dependencies

Set-Location D:\Projects\gmail-mcp-server
.\.venv\Scripts\Activate.ps1
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install -e .

Create the runtime environment file

Set-Location D:\Projects\gmail-mcp-server
Copy-Item .env.example .env -Force

Environment variables

GOOGLE_CLIENT_SECRET_PATH=D:\Projects\gmail-mcp-server\secrets\credentials.json
GOOGLE_TOKEN_PATH=D:\Projects\gmail-mcp-server\secrets\token.json
GMAIL_USER_ID=me
DEFAULT_UNREAD_LIMIT=10
LOG_LEVEL=INFO

Gmail API configuration

  1. Create a Google Cloud project

  2. Enable the Gmail API

  3. Configure the OAuth consent screen

  4. Add these scopes:

    • https://www.googleapis.com/auth/gmail.readonly
    • https://www.googleapis.com/auth/gmail.compose
  5. Create an OAuth client with application type Desktop app

  6. Save the downloaded client JSON as:

D:\Projects\gmail-mcp-server\secrets\credentials.json

Authentication

Run:

Set-Location D:\Projects\gmail-mcp-server
.\.venv\Scripts\Activate.ps1
python .\authenticate.py

This opens a browser-based Gmail OAuth flow and saves the token at:

D:\Projects\gmail-mcp-server\secrets\token.json

Do not press Ctrl + C during normal browser authentication. Press Ctrl + C only if the process is clearly stuck and no browser window appears.

Verification

Verify OAuth token creation

Test-Path D:\Projects\gmail-mcp-server\secrets\token.json

Expected result:

True

Verify Gmail read access

Set-Location D:\Projects\gmail-mcp-server
.\.venv\Scripts\Activate.ps1
python .\smoke_test.py

Expected result:

  • valid JSON output
  • unread message count
  • structured email objects
  • no exception

Verify MCP server startup

Set-Location D:\Projects\gmail-mcp-server
.\.venv\Scripts\Activate.ps1
python .\run_server.py

Expected result:

  • startup log appears
  • no immediate traceback
  • the process blocks normally as a stdio server

After confirming startup, press:

Ctrl + C

to stop the server.

Claude Desktop integration

On Windows, Claude Desktop MCP configuration is stored at:

$env:APPDATA\Claude\claude_desktop_config.json

Example server entry:

{
  "mcpServers": {
    "gmail-mcp-server": {
      "command": "D:\\Projects\\gmail-mcp-server\\.venv\\Scripts\\python.exe",
      "args": [
        "D:\\Projects\\gmail-mcp-server\\run_server.py"
      ],
      "env": {
        "PYTHONUNBUFFERED": "1"
      }
    }
  }
}

If your existing Claude config already contains a preferences block, keep it.

Verify Claude Desktop config pickup

Get-Content "$env:APPDATA\Claude\logs\main.log" -Tail 120
Select-String -Path "$env:APPDATA\Claude\logs\main.log" -Pattern "gmail-mcp-server","Launching MCP Server"

Expected evidence includes:

  • MCP Server connection requested for: gmail-mcp-server
  • Launching MCP Server: gmail-mcp-server

Example Claude prompts

These are representative prompts for local MCP usage once the server is configured:

Read my 5 unread emails and summarize the sender, subject, and key message.
Create a draft reply to the unread email from John saying: Thanks for the update. I will review this today and respond with next steps.
Reply to the latest message in this thread with: Thanks — this looks good to me. Please go ahead.

Verification note

This repository accurately documents:

  • local Gmail OAuth success
  • Python smoke test success
  • manual stdio server startup success
  • Claude Desktop configuration presence
  • Claude main.log launch-attempt evidence

If Claude Desktop cannot execute MCP tools in-chat on a Free plan, that is a client-plan limitation rather than a server implementation failure. For that reason, this README documents Claude Desktop compatibility and local verification without overstating in-chat execution results that were not directly captured.

Screenshots

Project structure

VS Code project structure

Dependency installation

Terminal dependency installation

Gmail API enabled

Google Cloud Gmail API enabled

OAuth Desktop app client

Google OAuth client configuration

Claude Desktop MCP config

Claude Desktop config

MCP server startup

MCP server running locally

Smoke test success

Smoke test success

Claude log launch attempt

Claude log launch attempt

OAuth token creation

Token file created

GitHub repository page

GitHub repository main page

GitHub rendered README

GitHub rendered README

Security notes

  • Never commit .env
  • Never commit secrets/credentials.json
  • Never commit secrets/token.json
  • Never publish token contents or credential contents in screenshots
  • Redact mailbox-sensitive data before publishing screenshots

Stretch goal direction

A natural extension is adding external or local reply context to improve draft quality, for example:

  • a local style guide
  • reusable reply templates
  • a lightweight knowledge folder for organization-specific context

This keeps the Gmail integration focused while making generated draft content more useful and more tailored.

License

This project is licensed under the MIT License.

See the LICENSE file for full details.

推荐服务器

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

官方
精选