Gmail MCP server

Provides direct access to Gmail accounts for reading, searching, sending emails, creating drafts, and managing labels and threads through secure OAuth authentication.
Back to servers
Provider
Redazzo
Release date
Mar 15, 2025
Language
Python

The Gmail MCP Server provides Gmail API integration for AI assistants through the Model Context Protocol. It enables AI systems to read emails, search for specific messages, send emails, create drafts, and manage various aspects of a Gmail account through a standardized interface.

Prerequisites

  • Python 3.6+
  • Gmail API credentials (credentials.json)
  • Required Python packages:
    • google-auth
    • google-auth-oauthlib
    • google-api-python-client
    • mcp (Model Context Protocol SDK)

Installation

  1. Clone the repository:

    git clone https://github.com/yourusername/gmail-mcp.git
    cd gmail-mcp
    
  2. Install dependencies:

    pip install -r requirements.txt
    
  3. Set up Gmail API credentials:

    • Go to the Google Cloud Console
    • Create a new project or select an existing one
    • Enable the Gmail API
    • Create OAuth 2.0 credentials (Desktop application type)
    • Download the credentials JSON file and save it as credentials.json in the project root directory

Running the Server

To start the Gmail MCP server:

python -m gmail.server

Or use the convenience script:

python gmail_server.py

The first time you run the server, it will open a browser window for authentication with your Google account. After successful authentication, a token.json file will be created to store your credentials for future use.

Available Tools

The Gmail MCP server provides the following tools for AI assistants:

Tool Name Description Parameters
get_labels_tool Get all Gmail labels and their IDs None
get_inbox_messages Get recent messages from the Gmail inbox max_results (optional, default: 10)
get_message_content_tool Get the full content of a specific email message_id (required)
send_email Send an email from your Gmail account to, subject, body (all required)
search_emails_tool Search for emails using Gmail search syntax query (required), max_results (optional, default: 10)
create_draft Create a draft email to, subject, body (all required)
add_label_to_message Add a label to a specific email message message_id, label_name (both required)
get_thread Get all messages in an email conversation thread thread_id (required)
mark_as_read Mark an email message as read message_id (required)
mark_as_unread Mark an email message as unread message_id (required)
archive_message Archive an email message message_id (required)
trash_message Move an email message to the Gmail trash message_id (required)

Resources

The server also provides the following MCP resources:

Resource URI Description
gmail://labels List of all Gmail labels
gmail://inbox Recent messages from the inbox
gmail://message/{message_id} Content of a specific email message
gmail://search/{query} Results of a Gmail search query

Using the Command Line Interface

In addition to the MCP server, you can use the included command-line interface:

python gmail_cli.py [command] [options]

Example: Searching Emails via CLI

python gmail_cli.py search "from:[email protected] is:unread" --max 5

This searches for up to 5 unread emails from [email protected].

Authentication and Security

The Gmail MCP server uses OAuth 2.0 for authentication with the Gmail API:

  1. On first run, the server initiates an OAuth flow that opens a browser window
  2. You log in to your Google account and grant permission
  3. Google provides an access token that is stored in token.json
  4. Subsequent runs use the stored token, refreshing it when necessary

Security Notes:

  • The token.json file contains sensitive authentication information and should be kept secure
  • The server requests Gmail API scopes for reading, sending, composing, modifying emails, and managing labels

Example Use Cases

When connected to an AI assistant through MCP, you can ask natural language questions like:

  • "Show me my recent emails"
  • "Search for emails from [email protected]"
  • "Send an email to [email protected] with subject 'Meeting' and body 'Can we meet tomorrow?'"
  • "What's in my latest email?"

Troubleshooting

Authentication Issues

If you encounter authentication problems:

  1. Delete the token.json file
  2. Run the server again to trigger a new authentication flow
  3. Ensure you have the correct permissions on your Google Cloud project

API Quota Limits

The Gmail API has usage quotas. If you hit these limits:

  1. Check your Google Cloud Console for quota information
  2. Consider implementing rate limiting in your application
  3. Request higher quotas if needed for production use

How to add this MCP server to Cursor

There are two ways to add an MCP server to Cursor. The most common way is to add the server globally in the ~/.cursor/mcp.json file so that it is available in all of your projects.

If you only need the server in a single project, you can add it to the project instead by creating or adding it to the .cursor/mcp.json file.

Adding an MCP server to Cursor globally

To add a global MCP server go to Cursor Settings > MCP and click "Add new global MCP server".

When you click that button the ~/.cursor/mcp.json file will be opened and you can add your server like this:

{
    "mcpServers": {
        "cursor-rules-mcp": {
            "command": "npx",
            "args": [
                "-y",
                "cursor-rules-mcp"
            ]
        }
    }
}

Adding an MCP server to a project

To add an MCP server to a project you can create a new .cursor/mcp.json file or add it to the existing one. This will look exactly the same as the global MCP server example above.

How to use the MCP server

Once the server is installed, you might need to head back to Settings > MCP and click the refresh button.

The Cursor agent will then be able to see the available tools the added MCP server has available and will call them when it needs to.

You can also explictly ask the agent to use the tool by mentioning the tool name and describing what the function does.

Want to 10x your AI skills?

Get a free account and learn to code + market your apps using AI (with or without vibes!).

Nah, maybe later