Headless Gmail MCP server

Enables headless Gmail access by separating credential storage from implementation, allowing clients to complete OAuth flows independently and pass credentials as context for email operations in containerized environments.
Back to servers
Provider
Bary Huang
Release date
Mar 20, 2025
Language
Python
Stats
36 stars

The MCP Headless Gmail Server enables Gmail operations (reading and sending emails) in remote environments without requiring local credential setup or browser access. It operates as a Model Context Protocol server that can run headless in containerized environments.

Installation Options

Using NPM

Install the server globally using NPM:

npm install -g @peakmojo/mcp-server-headless-gmail

Or run it directly with npx:

npx @peakmojo/mcp-server-headless-gmail

Using Docker

Pull and run the Docker image:

docker pull buryhuang/mcp-headless-gmail
docker run -i --rm buryhuang/mcp-headless-gmail:latest

Configuration with Claude Desktop

Docker Configuration

Add this to your Claude configuration:

{
  "mcpServers": {
    "gmail": {
      "command": "docker",
      "args": [
        "run",
        "-i",
        "--rm",
        "buryhuang/mcp-headless-gmail:latest"
      ]
    }
  }
}

NPM Configuration

For the NPM version:

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

Using the MCP Gmail Tools

Authentication and Token Refresh

When your access token expires or for initial setup, use the token refresh tool:

{
  "google_refresh_token": "your_refresh_token",
  "google_client_id": "your_client_id",
  "google_client_secret": "your_client_secret"
}

Or with an existing access token:

{
  "google_access_token": "your_access_token",
  "google_refresh_token": "your_refresh_token",
  "google_client_id": "your_client_id",
  "google_client_secret": "your_client_secret"
}

Retrieving Recent Emails

Get the most recent emails (first 1k characters of each body):

{
  "google_access_token": "your_access_token",
  "max_results": 5,
  "unread_only": false
}

Getting Full Email Content

For emails with bodies larger than 1k characters, retrieve content in chunks:

{
  "google_access_token": "your_access_token",
  "message_id": "message_id_from_get_recent_emails",
  "offset": 0
}

Or by thread ID:

{
  "google_access_token": "your_access_token",
  "thread_id": "thread_id_from_get_recent_emails",
  "offset": 1000
}

For long emails, make sequential calls increasing the offset by 1000 each time until contains_full_body is true.

Sending Emails

Send an email through Gmail:

{
  "google_access_token": "your_access_token",
  "to": "[email protected]",
  "subject": "Hello from MCP Gmail",
  "body": "This is a test email sent via MCP Gmail server",
  "html_body": "<p>This is a <strong>test email</strong> sent via MCP Gmail server</p>"
}

Obtaining Google API Credentials

To use this server, you'll need to obtain Google API credentials:

  1. Go to the Google Cloud Console
  2. Create a new project
  3. Enable the Gmail API
  4. Configure OAuth consent screen
  5. Create OAuth client ID credentials (select "Desktop app" as application type)
  6. Save the client ID and client secret
  7. Use OAuth 2.0 to obtain access and refresh tokens with these scopes:
    • https://www.googleapis.com/auth/gmail.readonly (for reading emails)
    • https://www.googleapis.com/auth/gmail.send (for sending emails)

Security Considerations

This server requires direct access to your Google API credentials. Always keep your tokens and credentials secure and never share them with untrusted parties.

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