MCP Headless Gmail Server

MCP Headless Gmail Server

Enables reading and sending Gmail messages in headless/remote environments without local credential storage, using OAuth tokens passed directly in API calls for containerized deployments.

Category
访问服务器

README

MCP Headless Gmail Server (NPM & Docker)

npm version Docker Pulls License: MIT

A MCP (Model Context Protocol) server that provides get, send Gmails without local credential or token setup.

<a href="https://glama.ai/mcp/servers/@baryhuang/mcp-headless-gmail"> <img width="380" height="200" src="https://glama.ai/mcp/servers/@baryhuang/mcp-headless-gmail/badge" alt="Headless Gmail Server MCP server" /> </a>

Why MCP Headless Gmail Server?

Critical Advantages

  • Headless & Remote Operation: Unlike other MCP Gmail solutions that require running outside of docker and local file access, this server can run completely headless in remote environments with no browser no local file access.
  • Decoupled Architecture: Any client can complete the OAuth flow independently, then pass credentials as context to this MCP server, creating a complete separation between credential storage and server implementation.

Nice but not critical

  • Focused Functionality: In many use cases, especially for marketing applications, only Gmail access is needed without additional Google services like Calendar, making this focused implementation ideal.
  • Docker-Ready: Designed with containerization in mind for a well-isolated, environment-independent, one-click setup.
  • Reliable Dependencies: Built on the well-maintained google-api-python-client library.

Features

  • Get most recent emails from Gmail with the first 1k characters of the body
  • Get full email body content in 1k chunks using offset parameter
  • Send emails through Gmail
  • Refresh access tokens separately
  • Automatic refresh token handling

Prerequisites

  • Python 3.10 or higher
  • Google API credentials (client ID, client secret, access token, and refresh token)

Installation

Installing via Smithery

To install mcp-headless-gmail for Claude Desktop automatically via Smithery:

npx -y @smithery/cli install @AbhinavBansal17/mcp-headless-gmail --client claude

Manual Installation

# Clone the repository
git clone https://github.com/baryhuang/mcp-headless-gmail.git
cd mcp-headless-gmail

# Install dependencies
pip install -e .

Docker

Building the Docker Image

# Build the Docker image
docker build -t mcp-headless-gmail .

Usage with Claude Desktop

You can configure Claude Desktop to use the Docker image by adding the following to your Claude configuration:

docker

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

npm version

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

Note: With this configuration, you'll need to provide your Google API credentials in the tool calls as shown in the Using the Tools section. Gmail credentials are not passed as environment variables to maintain separation between credential storage and server implementation.

Cross-Platform Publishing

To publish the Docker image for multiple platforms, you can use the docker buildx command. Follow these steps:

  1. Create a new builder instance (if you haven't already):

    docker buildx create --use
    
  2. Build and push the image for multiple platforms:

    docker buildx build --platform linux/amd64,linux/arm64,linux/arm/v7 -t buryhuang/mcp-headless-gmail:latest --push .
    
  3. Verify the image is available for the specified platforms:

    docker buildx imagetools inspect buryhuang/mcp-headless-gmail:latest
    

Usage

The server provides Gmail functionality through MCP tools. Authentication handling is simplified with a dedicated token refresh tool.

Starting the Server

mcp-server-headless-gmail

Using the Tools

When using an MCP client like Claude, you have two main ways to handle authentication:

Refreshing Tokens (First Step or When Tokens Expire)

If you have both access and refresh tokens:

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

If your access token has expired, you can refresh with just the refresh token:

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

This will return a new access token and its expiration time, which you can use for subsequent calls.

Getting Recent Emails

Retrieves recent emails with the first 1k characters of each email body:

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

Response includes:

  • Email metadata (id, threadId, from, to, subject, date, etc.)
  • First 1000 characters of the email body
  • body_size_bytes: Total size of the email body in bytes
  • contains_full_body: Boolean indicating if the entire body is included (true) or truncated (false)

Getting Full Email Body Content

For emails with bodies larger than 1k characters, you can retrieve the full content in chunks:

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

You can also get email content by thread ID:

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

The response includes:

  • A 1k chunk of the email body starting from the specified offset
  • body_size_bytes: Total size of the email body
  • chunk_size: Size of the returned chunk
  • contains_full_body: Boolean indicating if the chunk contains the remainder of the body

To retrieve the entire email body of a long message, make sequential calls increasing the offset by 1000 each time until contains_full_body is true.

Sending an Email

{
  "google_access_token": "your_access_token",
  "to": "recipient@example.com",
  "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>"
}

Token Refresh Workflow

  1. Start by calling the gmail_refresh_token tool with either:
    • Your full credentials (access token, refresh token, client ID, and client secret), or
    • Just your refresh token, client ID, and client secret if the access token has expired
  2. Use the returned new access token for subsequent API calls.
  3. If you get a response indicating token expiration, call the gmail_refresh_token tool again to get a new token.

This approach simplifies most API calls by not requiring client credentials for every operation, while still enabling token refresh when needed.

Obtaining Google API Credentials

To obtain the required Google API credentials, follow these steps:

  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 the application type)
  6. Save the client ID and client secret
  7. Use OAuth 2.0 to obtain access and refresh tokens with the following scopes:
    • https://www.googleapis.com/auth/gmail.readonly (for reading emails)
    • https://www.googleapis.com/auth/gmail.send (for sending emails)

Token Refreshing

This server implements automatic token refreshing. When your access token expires, the Google API client will use the refresh token, client ID, and client secret to obtain a new access token without requiring user intervention.

Security Note

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

License

See the LICENSE file for details.

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选