Gmail MCP Server
A Model Context Protocol (MCP) server that provides Gmail integration capabilities through a clean, async API.
Features
- 🔍 Search emails with Gmail's powerful search syntax
- 📧 Send emails with support for CC/BCC
- 📋 Get recent emails from inbox
- 📄 Get detailed email content including full body
- 🏷️ List Gmail labels (system and custom)
- 📁 Get emails by label (INBOX, STARRED, etc.)
- 💚 Health check to verify server and API connectivity
- ⏱️ Timeout protection for all API calls
- 🔄 Token refresh capability (requires client credentials)
Quick Start
1. Install Dependencies
pip install fastmcp requests pydantic
2. Set up Gmail API Credentials
- Go to Google Cloud Console
- Create a new project or select existing one
- Enable the Gmail API
- Create OAuth 2.0 credentials
- Download the credentials file
3. Get Access and Refresh Tokens
You'll need to obtain OAuth 2.0 tokens for Gmail API access. You can use the Google OAuth 2.0 playground or create a simple script to get these tokens.
4. Run the Server
python trial1.py --access-token YOUR_ACCESS_TOKEN --refresh-token YOUR_REFRESH_TOKEN
5. Test the Server
python test_server.py
Available Tools
search_emails
Search Gmail using Gmail's search syntax.
Input:
{
"params": {
"query": "from:example@gmail.com",
"max_results": 10,
"include_body": false
}
}
Examples:
after:2025/06/17- Emails after specific datefrom:example@gmail.com- Emails from specific sendersubject:meeting- Emails with specific subjectis:unread- Unread emailshas:attachment- Emails with attachments
get_recent_emails
Get the most recent emails from inbox.
Input:
{
"max_results": 10
}
get_email_details
Get complete details of a specific email including full body content.
Input:
{
"params": {
"message_id": "18c1234567890abcdef"
}
}
send_email
Send an email via Gmail.
Input:
{
"params": {
"to": "recipient@email.com",
"subject": "Email Subject",
"body": "Email content",
"cc": "cc@email.com",
"bcc": "bcc@email.com"
}
}
list_gmail_labels
List all Gmail labels including system and user-created labels.
Input:
{}
get_emails_by_label
Fetch emails that belong to a specific Gmail label.
Input:
{
"label_id": "INBOX",
"max_results": 10
}
health_check
Check the health and connectivity of the Gmail MCP server.
Input:
{}
Configuration
Environment Variables
You can set these environment variables instead of passing tokens as arguments:
GMAIL_ACCESS_TOKEN- Your Gmail API access tokenGMAIL_REFRESH_TOKEN- Your Gmail API refresh token
Server Settings
The server is configured with:
- Port: 8010
- Log level: INFO
- Cache expiration: 60 seconds
- Gmail API timeout: 30 seconds
Error Handling
The server includes comprehensive error handling:
- Timeout protection for all API calls
- Graceful handling of expired tokens
- Detailed error messages for debugging
- Fallback responses for failed operations
Security Considerations
- Never commit tokens to version control
- Use environment variables for sensitive data
- Consider implementing token refresh for long-running sessions
- Validate all input parameters
Troubleshooting
Common Issues
-
"Invalid credentials" error
- Check that your access token is valid and not expired
- Verify your refresh token is correct
-
"Permission denied" error
- Ensure your Gmail API credentials have the necessary scopes
- Check that the Gmail API is enabled in your Google Cloud project
-
"Timeout" errors
- The server has a 30-second timeout for Gmail API calls
- For large email searches, try reducing
max_results
Testing
Run the test script to verify your setup:
python test_server.py
Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
License
This project is licensed under the MIT License - see the LICENSE file for details.
Metadata
Release files for gmail-mcp-server-ansh 1.0.5
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 | |
|---|---|---|---|---|
| gmail_mcp_server_ansh-1.0.5.tar.gz | 14.6 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| gmail_mcp_server_ansh-1.0.5-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 30.6 kB
Release files / gmail_mcp_server_ansh-1.0.5.tar.gz
| Download URL | gmail_mcp_server_ansh-1.0.5.tar.gz |
|---|---|
| Size | 14.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
1a41b68c2ecd6fc3caf91dc8620f78d7d018c5ed081f445712ded95cde58395b
|
|
BLAKE2b-256 checksum How to use checksums |
9cf04fc79180c6ff081e42bb840204ca4c2cf232c5a60b718f340516bf9462c4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.10.12
|
Release files / gmail_mcp_server_ansh-1.0.5-py3-none-any.whl
| Download URL | gmail_mcp_server_ansh-1.0.5-py3-none-any.whl |
|---|---|
| Size | 16.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c76d57c7096169ea5758a15773a4d0a83a2abdef56ec031e311a897a5cea0f77
|
|
BLAKE2b-256 checksum How to use checksums |
3ffde3f3478d0261d49bb61957eecb721293de207f26bf3298c3767f7d22adaf
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.10.12
|
Security for gmail-mcp-server-ansh 1.0.5
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.