About Gmail MCP Server
Gmail MCP Server exposes Gmail email operations and prompts to AI assistants. It also includes a Gemini-powered client that turns natural-language requests into tool calls.
Key features
- 01Send and manage emails
- 02Read unread and specific messages
- 03Open messages in a browser
- 04OAuth 2.0 authentication
- 05Gemini-based natural-language client
Use cases
- 01Send an email from a natural-language request
- 02Review unread or recent messages
- 03Move a message to trash or mark it read
MCP support
The MCP server exposes Gmail operations as tools and includes prompts for drafting and managing email. MCP clients can use these tools after Google OAuth authentication.
Configuration
Local server config is the same for every client. Copy it into your client’s MCP config file.
We haven’t collected a package or remote endpoint for this server. Follow the instructions in the project README.
Tools
Local servers expose tools only once running on your machine, and the maintainer has not provided a tool list.
Details
From the project READMEGmail MCP Server
A Gmail integration with FastMCP (Model Control Protocol) that enables AI assistants to interact with Gmail through a set of tools and prompts.
Overview
This project provides a server-client architecture that allows AI assistants to perform Gmail operations such as:
- Sending emails
- Reading unread emails
- Viewing email content
- Moving emails to trash
- Opening emails in browser
- Marking emails as read
The system uses Google's OAuth 2.0 for authentication and the Gmail API for email operations. It now includes an LLM-powered client that understands natural language requests.
Components
1. Gmail Server (gmail_server.py)
The core server that provides MCP tools for Gmail operations:
- FastMCP Integration: Uses the FastMCP framework to expose Gmail operations as tools
- OAuth Authentication: Handles Gmail API authentication, token management and refreshing
- Email Operations: Implements email sending, reading, and management functionality
- Prompts: Includes special prompts for email drafting and management
2. Smart Gmail Client (test_gmail_server.py)
A natural language interface to the Gmail server:
- LLM Integration: Uses Google's Generative AI (Gemini) to interpret user requests
- Natural Language Understanding: Converts plain English requests into tool calls
- Parameter Extraction: Automatically extracts required parameters from user queries
- Interactive Interface: Provides a conversational experience for managing emails
- Comprehensive Logging: Includes detailed logging for debugging communication
Setup and Requirements
Prerequisites
- Python 3.6+
- Gmail API credentials (for authentication with Gmail)
- Google Generative AI API key (for the LLM-powered client)
- Packages from requirements.txt:
google-auth>=2.22.0 google-auth-oauthlib>=1.0.0 google-api-python-client>=2.95.0 fastmcp>=0.1.0 typing-extensions>=4.5.0 google-generativeai>=0.1.0 python-dotenv>=0.19.0
Initial Setup After Cloning
-
Install dependencies:
pip install -r requirements.txt -
Set up Gmail API credentials:
- Go to the Google Cloud Console
- Create a new project or select an existing one
- Enable the Gmail API for your project
- Create OAuth 2.0 credentials (Desktop client type)
- Download the credentials JSON file
- Rename it to
credentials.jsonand place it in the project root
-
Set up Gemini API Key:
- Go to Google AI Studio
- Create a new API key
- Create a
.envfile based on theexample.envtemplate:cp example.env .env - Add your Gemini API key to the
.envfile:GOOGLE_API_KEY=your_api_key_here
-
First run will generate token:
- The first time you run the Gmail server, it will open a browser window
- Log in to your Google account and grant the requested permissions
- This will generate a
token.jsonfile for future authentication
Authentication Files
The system requires several authentication files:
credentials.json: OAuth client ID credentials from Google Cloud Console for Gmail APItoken.json: Generated token file for authenticated access to Gmail.env: Environment variables file containing your Google Generative AI API key
Usage
Starting the Gmail Server
Start the Gmail server:
python gmail_server.py --creds credentials.json --token token.json
The server exposes the following tools:
send_email: Send emails with recipient, subject, and messageget_unread_emails: Retrieve unread messagesread_email: Get contents of specific emailstrash_email: Move emails to trashopen_email: Open emails in browsermark_email_as_read: Mark emails as read
Using the Smart Gmail Client
The smart client allows you to interact with Gmail using natural language:
python test_gmail_server.py
Example Commands:
- "Send an email to [recipient] about [topic]"
- "Check my unread emails"
- "Read my most recent message"
- "Open the latest email in browser"
- "Move an email from [sender] to trash"
- "Mark my unread emails as read"
The client will:
- Process your request using the LLM
- Determine which Gmail operation you want to perform
- Extract necessary parameters from your request
- Request any missing information
- Confirm sensitive operations before executing them
- Execute the operation and show you the results
Implementation Details
Email Content Handling
- Supports parsing and decoding MIME messages
- Handles multipart messages and attachments
- Properly decodes email headers
LLM Integration
- Uses Gemini 1.5 Flash for natural language understanding
- Converts LLM responses to structured tool calls
- Includes fallback mechanisms for handling malformed LLM responses
- Handles parameter extraction from free-form text
Error Handling
- Comprehensive logging throughout the application
- Graceful handling of HTTP errors from Gmail API
- Fallback mechanisms when the LLM response isn't formatted as expected
- Clear error responses for client applications
Development
Extending the Smart Client
To add support for new Gmail operations:
- Add the appropriate tool implementation in
gmail_server.py - The smart client will automatically discover and expose the new tools
- The LLM will learn to understand requests for the new operations from the tool descriptions
Customizing LLM Integration
You can modify the LLM prompt in test_gmail_server.py to:
- Improve parameter extraction for specific use cases
- Add specialized handling for particular types of requests
- Tune the system for your specific Gmail usage patterns
Security Note
This repository does not include credential files. Never commit your:
credentials.jsonfiletoken.jsonfile.envfile with API keys
These files are included in the .gitignore file to prevent accidental commits of sensitive information.
Reference
https://medium.com/@jason.summer/create-a-gmail-agent-with-model-context-protocol-mcp-061059c07777
Comments
New to MCP?
From what the protocol is to installing your first server, these answers cover most first questions.
An open protocol that defines how AI applications connect to external tools and data in a uniform way. Clients talk to the model; MCP servers provide tools, resources and prompt templates. They exchange standard messages, so one server can be reused by many clients.