MCP HubMCP Hub
GongRzhe

Gmail-MCP-Server

by: GongRzhe

A Model Context Protocol (MCP) server for Gmail integration in Claude Desktop with auto authentication support. This server enables AI assistants to manage Gmail through natural language interactions.

162created 26/12/2024
Visit
Gmail
Claude

📌Overview

Purpose: The framework serves to integrate Gmail functionalities within Claude Desktop, enabling AI assistants to manage emails seamlessly through natural language interactions.

Overview: The Gmail AutoAuth MCP Server is designed for efficient Gmail management. With auto authentication support, it allows users to perform various email operations easily, enhancing interaction through a natural language interface.

Key Features:

  • Email Management: Send and read emails, handle attachments, and organize emails with customizable labels.

  • Advanced Search Capabilities: Utilize Gmail’s search features to find emails based on multiple criteria such as sender, subject, and date range.

  • International Character Support: Full compatibility with non-ASCII characters in email subjects and content, allowing a diverse range of users to interact without language barriers.

  • OAuth2 Authentication: Easy integration with secure authentication processes to manage Gmail access without manual credential handling.

  • Global Credential Storage: Conveniently store authentication credentials locally, facilitating smooth and secure access across multiple sessions.


Gmail AutoAuth MCP Server

A Model Context Protocol (MCP) server for Gmail integration in Claude Desktop with auto authentication support. This server enables AI assistants to manage Gmail through natural language interactions.

Features

  • Send emails with subject, content, attachments, and recipients
  • Full support for international characters in subject lines and email content
  • Read email messages by ID with advanced MIME structure handling
  • View email attachments information (filenames, types, sizes)
  • Search emails with various criteria (subject, sender, date range)
  • Comprehensive label management: create, update, delete, and list labels
  • List all available Gmail labels (system and user-defined)
  • List emails in inbox, sent, or custom labels
  • Mark emails as read/unread
  • Move emails to different labels/folders
  • Delete emails
  • Batch operations for efficiently processing multiple emails at once
  • Full integration with Gmail API
  • Simple OAuth2 authentication flow with auto browser launch
  • Support for both Desktop and Web application credentials
  • Global credential storage for convenience

Installation & Authentication

Installing via Smithery

To install Gmail AutoAuth for Claude Desktop automatically via Smithery:

npx -y @smithery/cli install @gongrzhe/server-gmail-autoauth-mcp --client claude

Installing Manually

  1. Create a Google Cloud Project and obtain credentials:

    a. Create a Google Cloud Project:

    • Go to Google Cloud Console
    • Create or select a project
    • Enable the Gmail API for your project

    b. Create OAuth 2.0 Credentials:

    • Go to "APIs & Services" > "Credentials"
    • Click "Create Credentials" > "OAuth client ID"
    • Choose "Desktop app" or "Web application" as application type
    • For Web application, add http://localhost:3000/oauth2callback to authorized redirect URIs
    • Download the JSON file of your OAuth keys and rename it to gcp-oauth.keys.json
  2. Run Authentication:

    You can authenticate in two ways:

    a. Global Authentication (Recommended):

    mkdir -p ~/.gmail-mcp
    mv gcp-oauth.keys.json ~/.gmail-mcp/
    
    npx @gongrzhe/server-gmail-autoauth-mcp auth
    

    b. Local Authentication:

    # Place gcp-oauth.keys.json in your current directory
    npx @gongrzhe/server-gmail-autoauth-mcp auth
    

    The process looks for gcp-oauth.keys.json in the current directory or ~/.gmail-mcp/, copies it if in the current directory, then opens the browser for Google authentication and saves credentials to ~/.gmail-mcp/credentials.json.

    Note: Supports both Desktop and Web application credentials. For Web app, ensure proper authorized redirect URI.

  3. Configure in Claude Desktop:

{
  "mcpServers": {
    "gmail": {
      "command": "npx",
      "args": [
        "@gongrzhe/server-gmail-autoauth-mcp"
      ]
    }
  }
}

Docker Support

  1. Authentication:
docker run -i --rm \
  --mount type=bind,source=/path/to/gcp-oauth.keys.json,target=/gcp-oauth.keys.json \
  -v mcp-gmail:/gmail-server \
  -e GMAIL_OAUTH_PATH=/gcp-oauth.keys.json \
  -e "GMAIL_CREDENTIALS_PATH=/gmail-server/credentials.json" \
  -p 3000:3000 \
  mcp/gmail auth
  1. Usage:
{
  "mcpServers": {
    "gmail": {
      "command": "docker",
      "args": [
        "run",
        "-i",
        "--rm",
        "-v",
        "mcp-gmail:/gmail-server",
        "-e",
        "GMAIL_CREDENTIALS_PATH=/gmail-server/credentials.json",
        "mcp/gmail"
      ]
    }
  }
}

Cloud Server Authentication

For cloud environments (e.g., n8n), specify a custom callback URL during authentication:

npx @gongrzhe/server-gmail-autoauth-mcp auth https://yourdomain.com/oauth2callback

Setup Instructions for Cloud Environment

  1. Configure Reverse Proxy to forward traffic from your domain to the authentication port.

  2. Add DNS A record pointing your domain to your server’s IP.

  3. Add your custom callback URL (e.g., https://yourdomain.com/oauth2callback) to the authorized redirect URIs in Google Cloud Console.

  4. Run authentication command as above.

  5. Configure your application as in the manual installation step.

Available Tools

The server provides the following tools usable via Claude Desktop:

1. Send Email (send_email)

Sends a new email immediately.

{
  "to": ["recipient@example.com"],
  "subject": "Meeting Tomorrow",
  "body": "Hi,\n\nJust a reminder about our meeting tomorrow at 10 AM.\n\nBest regards",
  "cc": ["cc@example.com"],
  "bcc": ["bcc@example.com"]
}

2. Draft Email (draft_email)

Creates a draft email without sending.

{
  "to": ["recipient@example.com"],
  "subject": "Draft Report",
  "body": "Here's the draft report for your review.",
  "cc": ["manager@example.com"]
}

3. Read Email (read_email)

Retrieves the content of a specific email by ID.

{
  "messageId": "182ab45cd67ef"
}

4. Search Emails (search_emails)

Searches emails using Gmail search syntax.

{
  "query": "from:sender@example.com after:2024/01/01 has:attachment",
  "maxResults": 10
}

5. Modify Email (modify_email)

Adds or removes labels from emails (move folders, archive, etc.).

{
  "messageId": "182ab45cd67ef",
  "addLabelIds": ["IMPORTANT"],
  "removeLabelIds": ["INBOX"]
}

6. Delete Email (delete_email)

Permanently deletes an email.

{
  "messageId": "182ab45cd67ef"
}

7. List Email Labels (list_email_labels)

Retrieves all available Gmail labels.

{}

8. Create Label (create_label)

Creates a new Gmail label.

{
  "name": "Important Projects",
  "messageListVisibility": "show",
  "labelListVisibility": "labelShow"
}

9. Update Label (update_label)

Updates an existing Gmail label.

{
  "id": "Label_1234567890",
  "name": "Urgent Projects",
  "messageListVisibility": "show",
  "labelListVisibility": "labelShow"
}

10. Delete Label (delete_label)

Deletes a Gmail label.

{
  "id": "Label_1234567890"
}

11. Get or Create Label (get_or_create_label)

Gets an existing label by name or creates it if missing.

{
  "name": "Project XYZ",
  "messageListVisibility": "show",
  "labelListVisibility": "labelShow"
}

12. Batch Modify Emails (batch_modify_emails)

Modifies labels for multiple emails efficiently.

{
  "messageIds": ["182ab45cd67ef", "182ab45cd67eg", "182ab45cd67eh"],
  "addLabelIds": ["IMPORTANT"],
  "removeLabelIds": ["INBOX"],
  "batchSize": 50
}

13. Batch Delete Emails (batch_delete_emails)

Permanently deletes multiple emails efficiently.

{
  "messageIds": ["182ab45cd67ef", "182ab45cd67eg", "182ab45cd67eh"],
  "batchSize": 50
}

Advanced Search Syntax

The search_emails tool supports Gmail search operators:

OperatorExampleDescription
from:from:john@example.comEmails from a sender
to:to:mary@example.comEmails sent to a recipient
subject:subject:"meeting notes"Emails with text in subject
has:attachmenthas:attachmentEmails with attachments
after:after:2024/01/01Emails received after a date
before:before:2024/02/01Emails received before a date
is:is:unreadEmails with specific state
label:label:workEmails with specific label

Combine multiple operators, e.g., from:john@example.com after:2024/01/01 has:attachment

Advanced Features

Email Content Extraction

  • Prioritizes plain text content, falls back to HTML if needed
  • Handles multi-part MIME messages with nested parts
  • Extracts attachments info (filename, type, size)
  • Preserves original email headers (From, To, Subject, Date)

International Character Support

Supports non-ASCII characters including Turkish, Chinese, Japanese, Korean, and others, ensuring correct encoding and display.

Comprehensive Label Management

  • Create, update, delete labels (system labels protected)
  • Get or create labels by name
  • List all system and user labels with details
  • Control label visibility in message and label lists

Label visibility options:

  • messageListVisibility: show or hide
  • labelListVisibility: labelShow, labelShowIfUnread, or labelHide

Enables sophisticated email organization directly through Claude.

Batch Operations

  • Process up to 50 emails at once (configurable)
  • Automatic chunking to avoid API limits
  • Detailed reporting of successes and failures
  • Error handling with retries
  • Ideal for bulk inbox management

Security Notes

  • OAuth credentials stored securely in ~/.gmail-mcp/
  • Uses offline access for persistent authentication
  • Never share or commit credentials
  • Regularly review and revoke access in Google Account settings
  • Credentials accessible only by the current user

Troubleshooting

  1. OAuth Keys Not Found
    Ensure gcp-oauth.keys.json is in current directory or ~/.gmail-mcp/ with correct permissions.

  2. Invalid Credentials Format
    Verify OAuth keys contain web or installed credentials; confirm redirect URI correctness.

  3. Port Already in Use
    Free port 3000 if in use before running authentication.

  4. Batch Operation Failures
    Batch failures cause retries on individual items; check errors and consider lowering batch size.

Contributing

Contributions are welcome! Please submit a Pull Request.

License

MIT

Support

For issues or questions, please file an issue on the GitHub repository.