A Gmail service implementation using MCP
Project description
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mcp_gmail-0.1.0.tar.gz.
File metadata
- Download URL: mcp_gmail-0.1.0.tar.gz
- Upload date:
- Size: 6.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
261016da2ee1d61a493a2ee1bd75677b0d69c832cff1356ec37a621d1a16ac06
|
|
| MD5 |
c6aaaab281985fc8f5718f0e2723b475
|
|
| BLAKE2b-256 |
1c25df9f868705b825d91262ac055be94c7ae8d732bbef317b575ace98acfd3e
|
File details
Details for the file mcp_gmail-0.1.0-py3-none-any.whl.
File metadata
- Download URL: mcp_gmail-0.1.0-py3-none-any.whl
- Upload date:
- Size: 6.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9cdadd06a5f18b245e729671d373c1984a4a752fa171a5c0b2e871449d80d8fa
|
|
| MD5 |
67d149b6cbba33b0a1e9b09258c77082
|
|
| BLAKE2b-256 |
1a3b19ee57cbc0c839acc25d0a28fceb45a32ed1d9c7815bab063358d0d1c2da
|