Gmail MCP Server

Gmail MCP Server

Enables Claude to read unread emails and create draft replies in Gmail with proper threading.

Category
访问服务器

README

Gmail MCP Server

A Model Context Protocol (MCP) server that enables Claude to interact with Gmail — read unread emails and create draft replies.

Application project for the Founders and Coders AI programme. Full requirements.

Features

  • get_unread_emails — Retrieve unread emails with sender, subject, body, and thread ID
  • create_draft_reply — Draft replies that stay in the right thread
  • get_reply_context — Load a style guide that sneaks Ted Lasso quotes into replies

Tech Stack

  • Language: Python 3.12
  • MCP SDK: mcp with low-level Server API
  • Gmail API: google-api-python-client, google-auth-oauthlib

Setup

Prerequisites

  • Python 3.10+ (3.12 recommended)
  • Claude Desktop app
  • Google Cloud project with Gmail API enabled

1. Clone and install

git clone https://github.com/glrta/gmail-mcp-server.git
cd gmail-mcp-server
python3.12 -m venv venv
source venv/bin/activate
pip install -r requirements.txt

2. Set up Google Cloud credentials

Follow the detailed guide in docs/oauth-setup.md to:

  • Create a Google Cloud project
  • Enable the Gmail API
  • Configure OAuth consent screen
  • Download credentials.json into the project root

3. Authenticate

Run the authentication script once to authorize access to your Gmail:

python src/authenticate.py

This opens your browser for OAuth consent and saves the token to token.json.

4. Configure Claude Desktop

Add this to ~/Library/Application Support/Claude/claude_desktop_config.json:

{
  "mcpServers": {
    "gmail-mcp-server": {
      "command": "/FULL/PATH/TO/gmail-mcp-server/venv/bin/python",
      "args": ["/FULL/PATH/TO/gmail-mcp-server/src/server.py"]
    }
  }
}

Replace /FULL/PATH/TO/ with your actual project path.

5. Restart Claude Desktop

Completely quit (Cmd+Q) and reopen Claude Desktop.

Usage

Ask Claude things like:

  • "Show me my unread emails"
  • "What are my latest 5 unread emails?"
  • "Draft a reply to the email from [sender] about [topic] saying [your message]"
  • "Reply to thread [thread ID] with: Thanks, I'll look into it."

Project Structure

gmail-mcp-server/
├── src/
│   ├── server.py                 # MCP server — tool definitions and routing
│   ├── gmail_service.py          # Gmail API logic — auth, fetch, draft creation
│   └── authenticate.py           # One-time OAuth script (run before first use)
├── knowledge/
│   └── reply-guide.md            # Reply style guide — tone and templates
├── docs/
│   ├── oauth-setup.md            # Detailed Google Cloud setup guide
│   └── project-requirements.md   # Foundation Project requirements
├── credentials.json              # Google OAuth credentials (git-ignored)
├── token.json                    # Saved auth token (git-ignored)
├── .gitignore
└── README.md

Architecture

Claude Desktop
    ↓ (stdio / JSON-RPC)
MCP Server (src/server.py)
    ↓ (asyncio.to_thread)
Gmail Service (src/gmail_service.py)
    ↓ (REST API)
Gmail Account

Design decisions

File separation — 3 files Clean separation: MCP layer, Gmail logic, auth script

Async handling — asyncio.to_thread() The MCP server runs an async event loop. The Google Gmail client library is synchronous — it blocks while waiting for API responses. asyncio.to_thread() runs those blocking calls in a separate thread so they don't freeze the server.

Auth approach — Separate script The MCP server talks to Claude Desktop over stdout using JSON-RPC. If the OAuth flow ran inside the server, it would print browser prompts to stdout, corrupting the protocol. So auth is a separate script you run once before starting the server.

Error handling — Return TextContent When a tool fails (e.g. Gmail API error), instead of crashing the server, it catches the exception and returns the error as text to Claude. The server stays running so you don't have to restart it.

Body extraction — Recursive MIME parsing Emails aren't plain text — they're nested MIME structures. A message might have a multipart/mixed container holding text/plain and text/html parts, or nested deeper. The _extract_body function walks that tree recursively, preferring plain text over HTML.

Threading — In-Reply-To + References + threadId To make a reply appear in the same email thread, Gmail needs three things: the threadId (Gmail's internal grouping), In-Reply-To (the Message-ID of the email you're replying to), and References (the chain of all previous Message-IDs). Missing any of these can break threading in some email clients.

Troubleshooting

"token.json not found"

Run python src/authenticate.py first.

Token expired

The server auto-refreshes expired tokens. If it fails, delete token.json and run python src/authenticate.py again.

Server not connecting

  1. Check logs: tail -f ~/Library/Logs/Claude/mcp*.log
  2. Verify the Python path in config is correct (must be the venv Python)
  3. Ensure all dependencies are installed in the venv

Tools not appearing / changes not loading

Claude Desktop only reads MCP server config on launch. Any time you add a new tool, change a tool description, or update server code, you need to fully quit (Cmd+Q) and reopen Claude Desktop. A simple window close isn't enough.

Reflections

It's my first time using Python and I've implemented this project with Claude Code, with only this reflection section being typed by me. I first focused on understanding the flow with a simple tool to say hello, then moved to Google authentication and gmail tools. Claude initially suggested using FastMCP, a higher-level wrapper that simplifies server setup. I chose the low-level mcp.server API instead so I could better understand how the protocol works under the hood — the explicit tool registration and handler patterns map closely to how I'd build this in TypeScript, which is my day-to-day language at work.

Demo

Through testing, we found that saying "draft a reply" reliably triggers the tool workflow (including get_reply_context), while "write a reply" doesn't — Claude treats it as a general writing task instead. The tool name create_draft_reply seems to be what makes "draft" click.

Demo showing "write" fails but "draft" triggers the tool correctly

推荐服务器

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

官方
精选