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.
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.jsonin the project roottouch token.jsonbefore 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.
- Go to Google Cloud Console
- Create a new project (or select an existing one)
- Enable the Gmail API: APIs & Services → Library → search "Gmail API" → Enable
- 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)
- 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
Recommended Servers
playwright-mcp
A Model Context Protocol server that enables LLMs to interact with web pages through structured accessibility snapshots without requiring vision models or screenshots.
Magic Component Platform (MCP)
An AI-powered tool that generates modern UI components from natural language descriptions, integrating with popular IDEs to streamline UI development workflow.
Audiense Insights MCP Server
Enables interaction with Audiense Insights accounts via the Model Context Protocol, facilitating the extraction and analysis of marketing insights and audience data including demographics, behavior, and influencer engagement.
VeyraX MCP
Single MCP tool to connect all your favorite tools: Gmail, Calendar and 40 more.
graphlit-mcp-server
The Model Context Protocol (MCP) Server enables integration between MCP clients and the Graphlit service. Ingest anything from Slack to Gmail to podcast feeds, in addition to web crawling, into a Graphlit project - and then retrieve relevant contents from the MCP client.
Kagi MCP Server
An MCP server that integrates Kagi search capabilities with Claude AI, enabling Claude to perform real-time web searches when answering questions that require up-to-date information.
E2B
Using MCP to run code via e2b.
Neon Database
MCP server for interacting with Neon Management API and databases
Exa Search
A Model Context Protocol (MCP) server lets AI assistants like Claude use the Exa AI Search API for web searches. This setup allows AI models to get real-time web information in a safe and controlled way.
Qdrant Server
This repository is an example of how to create a MCP server for Qdrant, a vector search engine.