Gmail MCP server

Integrates Gmail functionality to enable secure access, search, and analysis of email contents using OAuth2 authentication and the Gmail API.
Back to servers
Setup instructions
Provider
AJ Brown
Release date
Dec 01, 2024
Language
Python
Stats
7 stars

This MCP server for Gmail allows Claude Desktop users to access and search their Gmail inbox through a secure OAuth2 connection. It provides a bridge between Claude and the Gmail API, enabling email browsing capabilities directly within Claude.

Setup and Installation

Google Cloud Project Setup

  1. Create a Google Cloud Project at https://console.cloud.google.com/
  2. Enable the Gmail API
  3. Create OAuth 2.0 credentials:
    • Application type: Desktop application
    • Download the credentials as credentials.json

Installation

# Create a conda environment
conda create -n mcp-gmail python=3.12
conda activate mcp-gmail

# Install the package
cd gmail-mcp-server
pip install -e .

Configuration for Claude Desktop

Add the following to your claude_desktop_config.json:

{
  "mcpServers": {
    "gmail": {
      "command": "/path/to/conda/envs/mcp-gmail/bin/python",
      "args": ["-m", "gmail_mcp_server.server"],
      "env": {
        "PYTHONPATH": "/path/to/gmail-mcp-server/src",
        "GMAIL_CREDENTIALS_FILE": "/path/to/credentials.json",
        "GMAIL_TOKEN_FILE": "/path/to/token.json"
      }
    }
  }
}

Note: Replace /path/to/ with your actual file paths. The token file will be created automatically during first authentication.

First Run

When you first try to access Gmail through Claude, you'll be prompted to authorize the application in your browser. After authorization, your credentials will be saved to the token file for future use.

Using the Gmail MCP Server

Available Resources

  • gmail://inbox/recent - Returns your 10 most recent emails

Using the Search Tool

The server provides a search_emails tool with the following parameters:

  • query (required): Gmail search query using Gmail's standard search syntax
  • max_results (optional): Maximum number of results to return (default: 10)

Example search query in Claude:

Could you show me any emails from sanrio in the last two days?

Environment Variables

The server requires two environment variables:

  • GMAIL_CREDENTIALS_FILE: Path to your Google OAuth credentials file
  • GMAIL_TOKEN_FILE: Path where the authentication token will be saved

Running the Server Directly

For testing purposes, you can run the server directly:

GMAIL_CREDENTIALS_FILE="/path/to/credentials.json" \
GMAIL_TOKEN_FILE="/path/to/token.json" \
python -m gmail_mcp_server.server

Security Information

The Gmail MCP server implements these security features:

  • OAuth2 authentication with Gmail
  • Browser-based authorization on first use
  • Local storage of credentials in the specified token file
  • Read-only access to Gmail (no write permissions)
  • Option to revoke access anytime through your Google Account settings

How to install this MCP server

For Claude Code

To add this MCP server to Claude Code, run this command in your terminal:

claude mcp add-json "gmail" '{"command":"/path/to/conda/envs/mcp-gmail/bin/python","args":["-m","gmail_mcp_server.server"],"env":{"PYTHONPATH":"/path/to/gmail-mcp-server/src","GMAIL_CREDENTIALS_FILE":"/path/to/credentials.json","GMAIL_TOKEN_FILE":"/path/to/token.json"}}'

See the official Claude Code MCP documentation for more details.

For 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 > Tools & Integrations and click "New MCP Server".

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

{
    "mcpServers": {
        "gmail": {
            "command": "/path/to/conda/envs/mcp-gmail/bin/python",
            "args": [
                "-m",
                "gmail_mcp_server.server"
            ],
            "env": {
                "PYTHONPATH": "/path/to/gmail-mcp-server/src",
                "GMAIL_CREDENTIALS_FILE": "/path/to/credentials.json",
                "GMAIL_TOKEN_FILE": "/path/to/token.json"
            }
        }
    }
}

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 explicitly ask the agent to use the tool by mentioning the tool name and describing what the function does.

For Claude Desktop

To add this MCP server to Claude Desktop:

1. Find your configuration file:

  • macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
  • Windows: %APPDATA%\Claude\claude_desktop_config.json
  • Linux: ~/.config/Claude/claude_desktop_config.json

2. Add this to your configuration file:

{
    "mcpServers": {
        "gmail": {
            "command": "/path/to/conda/envs/mcp-gmail/bin/python",
            "args": [
                "-m",
                "gmail_mcp_server.server"
            ],
            "env": {
                "PYTHONPATH": "/path/to/gmail-mcp-server/src",
                "GMAIL_CREDENTIALS_FILE": "/path/to/credentials.json",
                "GMAIL_TOKEN_FILE": "/path/to/token.json"
            }
        }
    }
}

3. Restart Claude Desktop for the changes to take effect

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