一个提供获取、发送Gmail功能的MCP(模型上下文协议)服务器,无需本地凭证或令牌设置。
<a href="https://glama.ai/mcp/servers/@baryhuang/mcp-headless-gmail"> <img width="380" height="200" src="https://gips3.baidu.com/it/u=959723254,397133773&fm=3081&app=3081&f=PNG?w=760&h=400" alt="无头Gmail服务器 MCP服务器" /> </a># 克隆仓库
git clone https://github.com/baryhuang/mcp-headless-gmail.git
cd mcp-headless-gmail
# 安装依赖项
pip install -e .
# 构建Docker镜像
docker build -t mcp-headless-gmail .
你可以配置Claude Desktop使用Docker镜像,通过在你的Claude配置中添加以下内容:
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
}
响应包括:
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
}
响应包括:
body_size_bytes:邮件正文的总大小chunk_size:返回块的大小contains_full_body:布尔值表示该块是否包含剩余的正文为了获取长邮件的完整正文,可以通过增加每次调用的偏移量直到contains_full_body变为true来连续调用。
{
"google_access_token": "your_access_token",
"to": "recipient@example.com",
"subject": "来自MCP Gmail的问候",
"body": "这是通过MCP Gmail服务器发送的测试邮件",
"html_body": "<p>这是通过MCP Gmail服务器发送的<strong>测试邮件</strong></p>"
}
gmail_refresh_token工具,传递:
gmail_refresh_token工具以获取新令牌。这种方法简化了大多数API调用,无需为每个操作都提供客户端凭证,同时在需要时仍能刷新令牌。
要获取所需的Google API凭证,请按照以下步骤操作:
https://www.googleapis.com/auth/gmail.readonly(用于读取邮件)https://www.googleapis.com/auth/gmail.send(用于发送邮件)此服务器实现了自动令牌刷新。当你的访问令牌过期时,Google API客户端将使用刷新令牌、客户端ID和客户端密钥获取新的访问令牌,无需用户干预。
此服务器需要直接访问你的Google API凭证。始终确保你的令牌和凭证安全,并且不要与不可信方共享。
详情见LICENSE文件。