Skip to main content

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

  1. Clone the repository:
git clone <repository-url>
cd gmail-mcp
  1. Create and activate a virtual environment:
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate
  1. Install dependencies:
pip install -r requirements.txt

Configuration

Authentication Setup

You have three options for authentication:

  1. OAuth 2.0 (Recommended for user-based access)

    • Download OAuth 2.0 credentials from Google Cloud Console
    • Save as credentials.json in the project root
    • First run will open browser for authentication
  2. Service Account

    • Create a service account in Google Cloud Console
    • Download the service account key
    • Save as service_account.json or set SERVICE_ACCOUNT_PATH
  3. Environment Variables

    export CREDENTIALS_CONFIG="base64_encoded_credentials"
    # or
    export GOOGLE_APPLICATION_CREDENTIALS="/path/to/service-account.json"
    

Usage

  1. Start the server:
python gmail_mcp_server.py
  1. 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 messages
  • from:example@gmail.com - Messages from specific sender
  • subject:meeting - Messages with specific subject
  • has:attachment - Messages with attachments
  • label:important - Messages with specific label
  • after:2024/01/01 - Messages after specific date
  • before: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

  1. Fork the repository
  2. Create a feature branch
  3. Commit your changes
  4. Push to the branch
  5. Create a Pull Request

License

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

Support

For support, please:

  1. Check the documentation
  2. Search existing issues
  3. Create a new issue if needed

Acknowledgments

  • Google Gmail API
  • FastMCP framework
  • Python community

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

mcp_gmail-0.1.0.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

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

mcp_gmail-0.1.0-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

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

Hashes for mcp_gmail-0.1.0.tar.gz
Algorithm Hash digest
SHA256 261016da2ee1d61a493a2ee1bd75677b0d69c832cff1356ec37a621d1a16ac06
MD5 c6aaaab281985fc8f5718f0e2723b475
BLAKE2b-256 1c25df9f868705b825d91262ac055be94c7ae8d732bbef317b575ace98acfd3e

See more details on using hashes here.

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

Hashes for mcp_gmail-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9cdadd06a5f18b245e729671d373c1984a4a752fa171a5c0b2e871449d80d8fa
MD5 67d149b6cbba33b0a1e9b09258c77082
BLAKE2b-256 1a3b19ee57cbc0c839acc25d0a28fceb45a32ed1d9c7815bab063358d0d1c2da

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