Gmail MCP Server
A Model Context Protocol (MCP) server built with FastMCP for interacting with Gmail. This server provides a robust interface for managing emails, handling authentication, and performing various Gmail operations.
Features
-
Email Management
- List and search emails
- Send new emails
- Reply to existing emails
- Get message details
- Get conversation threads
- Manage email labels
-
Authentication
- Multiple authentication methods support
- OAuth 2.0 flow
- Service Account authentication
- Application Default Credentials
- Automatic token refresh
-
Search Capabilities
- Full Gmail search syntax support
- Filter by labels, sender, subject, etc.
- Custom search queries
Prerequisites
- Python 3.10 or higher
- Google Cloud Project with Gmail API enabled
- Gmail account with API access
Installation
- Clone the repository:
git clone <repository-url>
cd gmail-mcp
- Create and activate a virtual environment:
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
- Install dependencies:
pip install -r requirements.txt
Configuration
Authentication Setup
You have three options for authentication:
-
OAuth 2.0 (Recommended for user-based access)
- Download OAuth 2.0 credentials from Google Cloud Console
- Save as
credentials.jsonin the project root - First run will open browser for authentication
-
Service Account
- Create a service account in Google Cloud Console
- Download the service account key
- Save as
service_account.jsonor setSERVICE_ACCOUNT_PATH
-
Environment Variables
export CREDENTIALS_CONFIG="base64_encoded_credentials" # or export GOOGLE_APPLICATION_CREDENTIALS="/path/to/service-account.json"
Usage
- Start the server:
python gmail_mcp_server.py
- Example API calls:
# List unread messages
messages = list_messages(query="is:unread", max_results=10)
# Send an email
result = send_message(
to="recipient@example.com",
subject="Test Email",
message_text="Hello, this is a test email!"
)
# Reply to a message
reply = reply_to_message(
message_id="message_id_here",
reply_text="Thank you for your email!"
)
# Get message details
message = get_message(message_id="message_id_here")
# Get conversation thread
thread = get_thread(thread_id="thread_id_here")
# Modify labels
result = modify_labels(
message_id="message_id_here",
add_labels=["IMPORTANT"],
remove_labels=["UNREAD"]
)
Gmail Search Syntax
The server supports Gmail's search syntax for queries:
is:unread- Unread messagesfrom:example@gmail.com- Messages from specific sendersubject:meeting- Messages with specific subjecthas:attachment- Messages with attachmentslabel:important- Messages with specific labelafter:2024/01/01- Messages after specific datebefore:2024/02/01- Messages before specific date
API Reference
Tools
-
list_messages(query: str = '', max_results: int = 10)- List messages matching the query
- Returns list of message objects
-
send_message(to: str, subject: str, message_text: str)- Send a new email
- Returns message details
-
reply_to_message(message_id: str, reply_text: str)- Reply to an existing message
- Returns reply details
-
get_message(message_id: str)- Get details of a specific message
- Returns message object
-
get_thread(thread_id: str)- Get all messages in a conversation
- Returns list of message objects
-
modify_labels(message_id: str, add_labels: List[str] = None, remove_labels: List[str] = None)- Modify labels on a message
- Returns updated message details
-
batch_modify_labels(message_ids: List[str], add_labels: List[str] = None, remove_labels: List[str] = None)- Modify labels on multiple messages
- Returns batch operation results
Resources
gmail://{message_id}/info- Get basic information about a message
- Returns JSON string with message metadata
Error Handling
The server includes comprehensive error handling:
- Authentication errors
- API rate limiting
- Invalid requests
- Network issues
All errors are returned with descriptive messages to help with debugging.
Contributing
- Fork the repository
- Create a feature branch
- Commit your changes
- Push to the branch
- Create a Pull Request
License
This project is licensed under the MIT License - see the LICENSE file for details.
Support
For support, please:
- Check the documentation
- Search existing issues
- Create a new issue if needed
Acknowledgments
- Google Gmail API
- FastMCP framework
- Python community
Metadata
Release files for mcp-gmail 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| mcp_gmail-0.1.0.tar.gz | 6.2 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| mcp_gmail-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 12.4 kB
Release files / mcp_gmail-0.1.0.tar.gz
| Download URL | mcp_gmail-0.1.0.tar.gz |
|---|---|
| Size | 6.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
261016da2ee1d61a493a2ee1bd75677b0d69c832cff1356ec37a621d1a16ac06
|
|
BLAKE2b-256 checksum How to use checksums |
1c25df9f868705b825d91262ac055be94c7ae8d732bbef317b575ace98acfd3e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.10.16
|
Release files / mcp_gmail-0.1.0-py3-none-any.whl
| Download URL | mcp_gmail-0.1.0-py3-none-any.whl |
|---|---|
| Size | 6.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9cdadd06a5f18b245e729671d373c1984a4a752fa171a5c0b2e871449d80d8fa
|
|
BLAKE2b-256 checksum How to use checksums |
1a3b19ee57cbc0c839acc25d0a28fceb45a32ed1d9c7815bab063358d0d1c2da
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.10.16
|
Security for mcp-gmail 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.