MCP Headless Gmail Server
MCP 无头 Gmail 服务器
MCP(模型上下文协议)服务器提供获取、发送 Gmail 的功能,无需设置本地凭证或令牌。
为什么选择 MCP Headless Gmail 服务器?
关键优势
无头和远程操作:与其他需要在 docker 之外运行和本地文件访问的 MCP Gmail 解决方案不同,该服务器可以在没有浏览器、没有本地文件访问的远程环境中完全无头运行。
解耦架构:任何客户端都可以独立完成 OAuth 流程,然后将凭证作为上下文传递给此 MCP 服务器,从而在凭证存储和服务器实现之间实现完全分离。
不错,但不是批评
重点功能:在许多用例中,特别是对于营销应用程序,只需要访问 Gmail,而无需日历等额外的 Google 服务,这使得这种重点实现成为理想选择。
Docker-Ready :设计时考虑了容器化,可实现良好隔离、独立于环境的一键式设置。
可靠的依赖关系:建立在维护良好的 google-api-python-client 库上。
Related MCP server: MCP Headless Gmail Server
特征
获取 Gmail 最新电子邮件正文的前 1k 个字符
使用偏移参数以 1k 为单位获取完整的电子邮件正文内容
通过 Gmail 发送电子邮件
单独刷新访问令牌
自动刷新令牌处理
先决条件
Python 3.10 或更高版本
Google API 凭证(客户端 ID、客户端密钥、访问令牌和刷新令牌)
安装
# Clone the repository
git clone https://github.com/baryhuang/mcp-headless-gmail.git
cd mcp-headless-gmail
# Install dependencies
pip install -e .Docker
构建 Docker 镜像
# Build the Docker image
docker build -t mcp-headless-gmail .与 Claude Desktop 一起使用
您可以通过将以下内容添加到 Claude 配置中来配置 Claude Desktop 以使用 Docker 镜像:
码头工人
{
"mcpServers": {
"gmail": {
"command": "docker",
"args": [
"run",
"-i",
"--rm",
"buryhuang/mcp-headless-gmail:latest"
]
}
}
}npm 版本
{
"mcpServers": {
"gmail": {
"command": "npx",
"args": [
"@peakmojo/mcp-server-headless-gmail"
]
}
}
}注意:使用此配置,您需要在工具调用中提供您的 Google API 凭据,如“使用工具”部分所示。Gmail 凭据不会作为环境变量传递,以保持凭据存储和服务器实现之间的分离。
跨平台发布
要为多个平台发布 Docker 镜像,可以使用docker buildx命令。请按以下步骤操作:
创建一个新的构建器实例(如果还没有):
docker buildx create --use为多个平台构建并推送图像:
docker buildx build --platform linux/amd64,linux/arm64,linux/arm/v7 -t buryhuang/mcp-headless-gmail:latest --push .验证该图像是否适用于指定的平台:
docker buildx imagetools inspect buryhuang/mcp-headless-gmail:latest
用法
该服务器通过 MCP 工具提供 Gmail 功能。专用的令牌刷新工具简化了身份验证处理。
启动服务器
mcp-server-headless-gmail使用工具
当使用像 Claude 这样的 MCP 客户端时,您有两种主要方式来处理身份验证:
刷新令牌(第一步或令牌过期时)
如果您同时拥有访问令牌和刷新令牌:
{
"google_access_token": "your_access_token",
"google_refresh_token": "your_refresh_token",
"google_client_id": "your_client_id",
"google_client_secret": "your_client_secret"
}如果您的访问令牌已过期,您可以仅使用刷新令牌进行刷新:
{
"google_refresh_token": "your_refresh_token",
"google_client_id": "your_client_id",
"google_client_secret": "your_client_secret"
}这将返回一个新的访问令牌及其到期时间,您可以在后续调用中使用它。
获取最近的电子邮件
检索最近的电子邮件,包含每封电子邮件正文的前 1k 个字符:
{
"google_access_token": "your_access_token",
"max_results": 5,
"unread_only": false
}响应包括:
电子邮件元数据(id、threadId、发件人、收件人、主题、日期等)
电子邮件正文的前 1000 个字符
body_size_bytes:电子邮件正文的总大小(以字节为单位)contains_full_body:布尔值,指示是否包含整个主体(true)或截断(false)
获取完整的电子邮件正文内容
对于正文大于 1k 个字符的电子邮件,您可以分块检索全部内容:
{
"google_access_token": "your_access_token",
"message_id": "message_id_from_get_recent_emails",
"offset": 0
}您还可以通过线程 ID 获取电子邮件内容:
{
"google_access_token": "your_access_token",
"thread_id": "thread_id_from_get_recent_emails",
"offset": 1000
}响应内容包括:
从指定偏移量开始的 1k 邮件正文块
body_size_bytes:电子邮件正文的总大小chunk_size:返回块的大小contains_full_body:布尔值,指示块是否包含主体的其余部分
要检索长消息的整个电子邮件正文,请进行连续调用,每次将偏移量增加 1000,直到contains_full_body为真。
发送电子邮件
{
"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>"
}令牌刷新工作流程
首先使用以下任一方式调用
gmail_refresh_token工具:您的完整凭证(访问令牌、刷新令牌、客户端 ID 和客户端密钥),或
如果访问令牌已过期,则仅显示刷新令牌、客户端 ID 和客户端密钥
使用返回的新访问令牌进行后续 API 调用。
如果您收到指示令牌过期的响应,请再次调用
gmail_refresh_token工具以获取新令牌。
这种方法不需要每个操作都提供客户端凭据,从而简化了大多数 API 调用,同时仍在需要时启用令牌刷新。
获取 Google API 凭证
要获取所需的 Google API 凭据,请按照以下步骤操作:
创建新项目
启用 Gmail API
配置 OAuth 同意屏幕
创建 OAuth 客户端 ID 凭据(选择“桌面应用程序”作为应用程序类型)
保存客户端 ID 和客户端密钥
使用 OAuth 2.0 获取具有以下范围的访问和刷新令牌:
https://www.googleapis.com/auth/gmail.readonly(用于阅读电子邮件)https://www.googleapis.com/auth/gmail.send(用于发送电子邮件)
令牌刷新
此服务器实现了令牌自动刷新功能。当您的访问令牌过期时,Google API 客户端将使用刷新令牌、客户端 ID 和客户端密钥来获取新的访问令牌,无需用户干预。
安全说明
此服务器需要直接访问您的 Google API 凭据。请始终确保您的令牌和凭据安全无虞,切勿与不受信任的第三方共享。
执照
有关详细信息,请参阅 LICENSE 文件。
Available Tools
4 toolsgmail_get_email_body_chunkB
Get a 1k character chunk of an email body starting from the specified offset
| Name | Required | Description | Default |
|---|---|---|---|
| google_access_token | Yes | Google OAuth2 access token | |
| message_id | No | ID of the message to retrieve | |
| thread_id | No | ID of the thread to retrieve (will get the first message if multiple exist) | |
| offset | No | Offset in characters to start from (default: 0) |
TDQS
Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?
With no annotations provided, the description carries full burden for behavioral disclosure. It mentions the 1k character chunking behavior, which is valuable, but doesn't address authentication needs (though implied by google_access_token parameter), error handling, rate limits, or what happens with invalid offsets/message_ids. For a tool with no annotation coverage, this leaves significant gaps.
Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.
Is the description appropriately sized, front-loaded, and free of redundancy?
The description is a single, efficient sentence that immediately conveys the core functionality without any wasted words. It's appropriately sized and front-loaded with the essential information.
Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.
Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?
Given the tool's moderate complexity (4 parameters, no output schema, no annotations), the description is minimally adequate. It explains the chunking behavior but lacks details about authentication requirements, error conditions, and how this tool relates to siblings. Without annotations or output schema, more behavioral context would be helpful for safe usage.
Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.
Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?
Schema description coverage is 100%, so parameters are well-documented in the schema. The description adds context about the 'offset' parameter (default: 0) and clarifies that thread_id retrieves the first message if multiple exist, providing some value beyond the schema. However, it doesn't explain parameter interactions or provide additional semantic context.
Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.
Does the description clearly state what the tool does and how it differs from similar tools?
The description clearly states the action ('Get'), resource ('email body chunk'), and key constraint ('1k character chunk starting from specified offset'), making the purpose immediately understandable. However, it doesn't explicitly differentiate from sibling tools like gmail_get_recent_emails, which retrieves multiple emails rather than a specific body chunk.
Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.
Does the description explain when to use this tool, when not to, or what alternatives exist?
No guidance is provided about when to use this tool versus alternatives. The description doesn't mention prerequisites (like needing a message_id or thread_id), nor does it explain when this tool is appropriate compared to gmail_get_recent_emails for retrieving email content.
Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.
gmail_get_recent_emailsC
Get the most recent emails from Gmail (returns metadata, snippets, and first 1k chars of body)
| Name | Required | Description | Default |
|---|---|---|---|
| google_access_token | Yes | Google OAuth2 access token | |
| max_results | No | Maximum number of emails to return (default: 10) | |
| unread_only | No | Whether to return only unread emails (default: False) |
TDQS
Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?
With no annotations provided, the description carries full burden for behavioral disclosure. It mentions what data is returned (metadata, snippets, first 1k chars of body) which is helpful, but doesn't cover important behavioral aspects like authentication requirements (beyond the parameter), rate limits, pagination behavior, error conditions, or whether this is a read-only operation. For a tool with no annotations, this leaves significant gaps.
Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.
Is the description appropriately sized, front-loaded, and free of redundancy?
The description is a single, efficient sentence that communicates the core functionality and return format. It's appropriately sized for a straightforward retrieval tool, though it could potentially benefit from slightly more detail given the lack of annotations and output schema.
Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.
Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?
Given the complexity of email retrieval (3 parameters, no annotations, no output schema), the description is insufficiently complete. It doesn't explain the return format in detail, doesn't mention authentication requirements beyond the parameter, and doesn't cover important behavioral aspects. For a tool with no annotations or output schema, the description should provide more context about what to expect from the operation.
Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.
Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?
Schema description coverage is 100%, so the schema already fully documents all three parameters. The description doesn't add any parameter-specific information beyond what's in the schema. The baseline of 3 is appropriate when the schema does all the parameter documentation work.
Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.
Does the description clearly state what the tool does and how it differs from similar tools?
The description clearly states the tool's purpose: 'Get the most recent emails from Gmail' specifies the verb (get) and resource (emails). It distinguishes from sibling 'gmail_get_email_body_chunk' by indicating it returns metadata, snippets, and partial body content, but doesn't explicitly differentiate from other siblings like 'gmail_send_email' or 'gmail_refresh_token' beyond the obvious functional difference.
Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.
Does the description explain when to use this tool, when not to, or what alternatives exist?
No explicit guidance on when to use this tool versus alternatives is provided. The description doesn't mention when to use this versus 'gmail_get_email_body_chunk' for full body retrieval, or when to use 'gmail_refresh_token' for token management. Usage context is implied by the tool name but not explicitly stated.
Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.
gmail_refresh_tokenB
Refresh the access token using the refresh token and client credentials
| Name | Required | Description | Default |
|---|---|---|---|
| google_access_token | No | Google OAuth2 access token (optional if expired) | |
| google_refresh_token | Yes | Google OAuth2 refresh token | |
| google_client_id | Yes | Google OAuth2 client ID for token refresh | |
| google_client_secret | Yes | Google OAuth2 client secret for token refresh |
TDQS
Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?
With no annotations provided, the description carries full burden but only states what the tool does at a high level. It doesn't disclose behavioral traits like whether this invalidates previous tokens, rate limits, error conditions, or what the refreshed token enables. For a security-sensitive operation with zero annotation coverage, this is inadequate.
Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.
Is the description appropriately sized, front-loaded, and free of redundancy?
The description is a single, efficient sentence that directly states the tool's purpose with zero waste. It's appropriately sized and front-loaded, with every word contributing to understanding the core functionality.
Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.
Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?
For a security-critical token refresh operation with no annotations and no output schema, the description is insufficient. It doesn't explain what happens after refresh (e.g., token lifetime, scope preservation), error handling, or integration with sibling tools. Given the complexity and lack of structured data, more context is needed.
Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.
Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?
Schema description coverage is 100%, so the schema already documents all 4 parameters thoroughly. The description adds no parameter-specific semantics beyond what's in the schema (e.g., it doesn't explain relationships between parameters or provide usage examples). Baseline 3 is appropriate when schema does the heavy lifting.
Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.
Does the description clearly state what the tool does and how it differs from similar tools?
The description clearly states the verb ('Refresh') and resource ('access token'), specifying it uses refresh token and client credentials. It distinguishes from sibling tools (email-related operations) by focusing on authentication token management, though it doesn't explicitly name alternatives for token refresh.
Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.
Does the description explain when to use this tool, when not to, or what alternatives exist?
The description implies usage when access tokens expire (via 'optional if expired' in schema), but doesn't explicitly state when to use this tool versus alternatives like initial authentication or other token management methods. No guidance on prerequisites or exclusions is provided.
Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.
gmail_send_emailC
Send an email via Gmail
| Name | Required | Description | Default |
|---|---|---|---|
| google_access_token | Yes | Google OAuth2 access token | |
| to | Yes | Recipient email address | |
| subject | Yes | Email subject | |
| body | Yes | Email body content (plain text) | |
| html_body | No | Email body content in HTML format (optional) |
TDQS
Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?
With no annotations provided, the description carries the full burden of behavioral disclosure. It states the action ('send an email') but lacks critical details: it doesn't mention authentication requirements (implied by the 'google_access_token' parameter but not explicitly stated), potential rate limits, error handling, or what happens upon success (e.g., whether it returns a confirmation). This leaves significant gaps for a mutation tool.
Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.
Is the description appropriately sized, front-loaded, and free of redundancy?
The description is a single, efficient sentence with zero wasted words—'Send an email via Gmail' is front-loaded and directly conveys the core action. It's appropriately sized for the tool's complexity, making it easy to parse quickly.
Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.
Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?
Given the tool's complexity (a mutation tool with 5 parameters, no annotations, and no output schema), the description is incomplete. It fails to address key contextual aspects like authentication needs, behavioral traits (e.g., what 'send' entails operationally), or output expectations, leaving the agent with insufficient information for reliable use.
Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.
Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?
The schema description coverage is 100%, with all parameters well-documented in the schema (e.g., 'to' as recipient email, 'body' as plain text content). The description adds no additional meaning beyond the schema, such as explaining parameter interactions (e.g., 'body' vs. 'html_body') or constraints. This meets the baseline for high schema coverage.
Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.
Does the description clearly state what the tool does and how it differs from similar tools?
The description 'Send an email via Gmail' clearly states the verb ('send') and resource ('email via Gmail'), making the purpose immediately understandable. However, it doesn't differentiate from sibling tools like 'gmail_get_recent_emails' or 'gmail_refresh_token' beyond the obvious action distinction, which keeps it from a perfect score.
Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.
Does the description explain when to use this tool, when not to, or what alternatives exist?
The description provides no guidance on when to use this tool versus alternatives. It doesn't mention prerequisites (e.g., needing authentication via 'google_access_token'), nor does it clarify scenarios where other tools like 'gmail_get_recent_emails' might be more appropriate, leaving usage context entirely implicit.
Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.
Tool Schema Changelog
Recent tool additions, removals, and schema changes observed during successful MCP inspections.
4 tool updates
- First observed
gmail_get_email_body_chunk - First observed
gmail_get_recent_emails - First observed
gmail_refresh_token - First observed
gmail_send_email
TDQS
Scored across 4 tools
Each tool has a clearly distinct purpose: retrieving email body chunks, listing recent emails, refreshing tokens, and sending emails. There is no overlap in functionality that would cause confusion or misselection.
All tools follow a consistent 'gmail_verb_noun' pattern with snake_case, making them predictable and easy to understand. The naming convention is uniform across all four tools.
With 4 tools, the count is reasonable for a Gmail server, though it feels slightly thin for covering all common email operations. It includes core functions but could benefit from additional tools like searching or managing drafts.
The tools cover basic email operations (read, list, send) and authentication, but there are notable gaps such as searching emails, managing labels, or handling attachments. This could limit agents in performing more complex email tasks.
Maintenance
Related MCP Connectors
Manage Gmail end-to-end: search, read, send, draft, label, and organize threads. Automate workflow…
Never-stored live email: read, send, organize, schedule and auto-triage Gmail or any IMAP mailbox.
Permissioned access to Gmail, Drive and Calendar via the user's own Google account
A MCP server for Gmail that lets you search, read, and draft emails and replies.
Related MCP Servers
- AlicenseNot gradedqualityNot gradedmaintenanceA Model Context Protocol server that enables applications to interact with Gmail through a clean API, supporting email searching, sending, reading, and label management.MIT
- AlicenseBqualityDmaintenanceEnables reading and sending Gmail messages in headless/remote environments without local credential storage, using OAuth tokens passed directly in API calls for containerized deployments.362 npmMIT
- AlicenseNot gradedqualityBmaintenanceEnables reading, sending, archiving, and managing Gmail emails and labels through Google OAuth authentication, acting as an OAuth proxy to the Gmail API.150 npm11MIT
- AlicenseNot gradedqualityDmaintenanceA Model Context Protocol server that exposes the Gmail API for integration with LLMs, enabling email management tasks such as reading, labeling, and searching emails.7MIT