Skip to main content

A Model Context Protocol (MCP) server for Gmail integration.

Project description

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

  1. Go to Google Cloud Console
  2. Create a new project or select existing one
  3. Enable the Gmail API
  4. Create OAuth 2.0 credentials
  5. 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 date
  • from:example@gmail.com - Emails from specific sender
  • subject:meeting - Emails with specific subject
  • is:unread - Unread emails
  • has: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 token
  • GMAIL_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

  1. "Invalid credentials" error

    • Check that your access token is valid and not expired
    • Verify your refresh token is correct
  2. "Permission denied" error

    • Ensure your Gmail API credentials have the necessary scopes
    • Check that the Gmail API is enabled in your Google Cloud project
  3. "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

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests if applicable
  5. Submit a pull request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gmail_mcp_server_ansh-1.0.1.tar.gz (14.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gmail_mcp_server_ansh-1.0.1-py3-none-any.whl (15.8 kB view details)

Uploaded Python 3

File details

Details for the file gmail_mcp_server_ansh-1.0.1.tar.gz.

File metadata

  • Download URL: gmail_mcp_server_ansh-1.0.1.tar.gz
  • Upload date:
  • Size: 14.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for gmail_mcp_server_ansh-1.0.1.tar.gz
Algorithm Hash digest
SHA256 e34e27baef321550d46e2cd51fc73f47a3f7b2d9ee710ee3d409676be4145548
MD5 a49bffd824bc571fa1a5dad29134e8fc
BLAKE2b-256 4ad2ed0b684266a2f7add1c46a60126808d259670c8d6d1093e55be7201009c4

See more details on using hashes here.

File details

Details for the file gmail_mcp_server_ansh-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for gmail_mcp_server_ansh-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a967008c271aa535ab63b17b6d35c700fed50a4955e4913d3b6e7063525f61d3
MD5 3151c9e68860e785a714f45daa9caa97
BLAKE2b-256 3ffb6ba2abfbf183c6ba158bc96848e17a850246079e578581c88602d18489bc

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page