这是一个用于Claude Desktop中Gmail集成的Model Context Protocol (MCP)服务器,支持自动身份验证。此服务器使AI助手能够通过自然语言交互管理Gmail。
要通过Smithery自动安装Gmail AutoAuth for Claude Desktop:
npx -y @smithery/cli install @gongrzhe/server-gmail-autoauth-mcp --client claude
创建一个Google Cloud项目并获取凭证:
a. 创建一个Google Cloud项目:
b. 创建OAuth 2.0凭证:
http://localhost:3000/oauth2callbackgcp-oauth.keys.json运行认证:
您可以通过两种方式认证:
a. 全局认证(推荐):
# 第一次:将gcp-oauth.keys.json放置在您的家目录下的.gmail-mcp文件夹中
mkdir -p ~/.gmail-mcp
mv gcp-oauth.keys.json ~/.gmail-mcp/
# 从任何地方运行认证
npx @gongrzhe/server-gmail-autoauth-mcp auth
b. 局部认证:
# 将gcp-oauth.keys.json放置在当前目录下
# 文件将被自动复制到全局配置
npx @gongrzhe/server-gmail-autoauth-mcp auth
认证过程会:
~/.gmail-mcp/中的gcp-oauth.keys.json~/.gmail-mcp/~/.gmail-mcp/credentials.json注意:
- 成功认证后,凭证将全局存储在
~/.gmail-mcp/中,并可以从任何目录使用- 支持桌面应用和Web应用凭证
- 对于Web应用凭证,请确保在授权重定向URI中添加
http://localhost:3000/oauth2callback
在Claude Desktop中配置:
{
"mcpServers": {
"gmail": {
"command": "npx",
"args": [
"@gongrzhe/server-gmail-autoauth-mcp"
]
}
}
}
如果您更喜欢使用Docker:
docker run -i --rm \
--mount type=bind,source=/path/to/gcp-oauth.keys.json,target=/gcp-oauth.keys.json \
-v mcp-gmail:/gmail-server \
-e GMAIL_OAUTH_PATH=/gcp-oauth.keys.json \
-e "GMAIL_CREDENTIALS_PATH=/gmail-server/credentials.json" \
-p 3000:3000 \
mcp/gmail auth
{
"mcpServers": {
"gmail": {
"command": "docker",
"args": [
"run",
"-i",
"--rm",
"-v",
"mcp-gmail:/gmail-server",
"-e",
"GMAIL_CREDENTIALS_PATH=/gmail-server/credentials.json",
"mcp/gmail"
]
}
}
}
对于云服务器环境(如n8n),您可以在认证期间指定自定义回调URL:
npx @gongrzhe/server-gmail-autoauth-mcp auth https://gmail.gongrzhe.com/oauth2callback
配置反向代理:
gmail.gongrzhe.com)转发到该端口DNS配置:
Google Cloud平台设置:
https://gmail.gongrzhe.com/oauth2callback)添加到授权重定向URI列表中运行认证:
npx @gongrzhe/server-gmail-autoauth-mcp auth https://gmail.gongrzhe.com/oauth2callback
在应用程序中配置:
{
"mcpServers": {
"gmail": {
"command": "npx",
"args": [
"@gongrzhe/server-gmail-autoauth-mcp"
]
}
}
}
这种方法允许认证流在无法访问localhost的环境中正常工作,例如容器化应用程序或云服务器。
服务器提供了以下工具,可通过Claude Desktop使用:
send_email)立即发送新邮件。支持纯文本、HTML或多部分邮件可选带附件。
基本邮件:
{
"to": ["recipient@example.com"],
"subject": "明天会议",
"body": "您好,\n\n提醒一下我们明天上午10点的会议。\n\n祝好",
"cc": ["cc@example.com"],
"bcc": ["bcc@example.com"],
"mimeType": "text/plain"
}
带附件的邮件:
{
"to": ["recipient@example.com"],
"subject": "项目文件",
"body": "您好,\n\n请查收附上的项目文件。\n\n祝好",
"attachments": [
"/path/to/document.pdf",
"/path/to/spreadsheet.xlsx",
"/path/to/presentation.pptx"
]
}
HTML邮件示例:
{
"to": ["recipient@example.com"],
"subject": "明天会议",
"mimeType": "text/html",
"body": "<html><body><h1>会议提醒</h1><p>提醒一下我们<b>明天的会议</b>在上午10点。</p><p>祝好</p></body></html>"
}
多部分邮件示例(HTML+纯文本):
{
"to": ["recipient@example.com"],
"subject": "明天会议",
"mimeType": "multipart/alternative",
"body": "您好,\n\n提醒一下我们明天上午10点的会议。\n\n祝好",
"htmlBody": "<html><body><h1>会议提醒</h1><p>提醒一下我们<b>明天的会议</b>在上午10点。</p><p>祝好</p></body></html>"
}
draft_email)创建草稿邮件而不发送。也支持附件。
{
"to": ["recipient@example.com"],
"subject": "草案报告",
"body": "这是供您审阅的草案报告。",
"cc": ["manager@example.com"],
"attachments": ["/path/to/draft_report.docx"]
}
read_email)根据其ID检索特定邮件的内容。现在显示增强的附件信息。
{
"messageId": "182ab45cd67ef"
}
增强响应包括附件详细信息:
主题:项目文件
来自:sender@example.com
收件人:recipient@example.com
日期:2025年6月19日 10:30:00 -0400
邮件正文内容在这里...
附件(2):
- document.pdf (application/pdf, 245 KB, ID: ANGjdJ9fkTs-i3GCQo5o97f_itG...)
- spreadsheet.xlsx (application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, 89 KB, ID: BWHkeL8gkUt-j4HDRp6o98g_juI...)
download_attachment)新功能:将电子邮件附件下载到本地文件系统。
{
"messageId": "182ab45cd67ef",
"attachmentId": "ANGjdJ9fkTs-i3GCQo5o97f_itG...",
"savePath": "/path/to/downloads",
"filename": "downloaded_document.pdf"
}
参数:
messageId:包含附件的邮件IDattachmentId:附件ID(在增强邮件显示中显示)savePath:保存文件的目录(可选,默认为当前目录)filename:自定义文件名(可选,如果没有提供则使用原始文件名)search_emails)使用Gmail搜索语法搜索邮件。
{
"query": "from:sender@example.com after:2024/01/01 has:attachment",
"maxResults": 10
}
modify_email)向邮件添加或移除标签(移动到不同文件夹、归档等)。
{
"messageId": "182ab45cd67ef",
"addLabelIds": ["IMPORTANT"],
"removeLabelIds": ["INBOX"]
}
delete_email)永久删除邮件。
{
"messageId": "182ab45cd67ef"
}
list_email_labels)检索所有可用的Gmail标签。
{}
create_label)创建新的Gmail标签。
{
"name": "重要项目",
"messageListVisibility": "show",
"labelListVisibility": "labelShow"
}
update_label)更新现有Gmail标签。
{
"id": "Label_1234567890",
"name": "紧急项目",
"messageListVisibility": "show",
"labelListVisibility": "labelShow"
}
delete_label)删除Gmail标签。
{
"id": "Label_1234567890"
}
get_or_create_label)通过名称获取现有标签或如果不存在则创建它。
{
"name": "项目XYZ",
"messageListVisibility": "show",
"labelListVisibility": "labelShow"
}
batch_modify_emails)批量高效地修改多个邮件的标签。
{
"messageIds": ["182ab45cd67ef", "182ab45cd67eg", "182ab45cd67eh"],
"addLabelIds": ["IMPORTANT"],
"removeLabelIds": ["INBOX"],
"batchSize": 50
}
batch_delete_emails)批量高效地永久删除多个邮件。
{
"messageIds": ["182ab45cd67ef", "182ab45cd67eg", "182ab45cd67eh"],
"batchSize": 50
}
create_filter)创建具有自定义标准和操作的新Gmail过滤器。
{
"criteria": {
"from": "newsletter@company.com",
"hasAttachment": false
},
"action": {
"addLabelIds": ["Label_Newsletter"],
"removeLabelIds": ["INBOX"]
}
}
list_filters)检索所有Gmail过滤器。
{}
get_filter)获取特定Gmail过滤器的详细信息。
{
"filterId": "ANe1Bmj1234567890"
}
delete_filter)删除Gmail过滤器。
{
"filterId": "ANe1Bmj1234567890"
}
create_filter_from_template)使用预定义模板为常见场景创建过滤器。
{
"template": "fromSender",
"parameters": {
"senderEmail": "notifications@github.com",
"labelIds": ["Label_GitHub"],
"archive": true
}
}
您可以基于各种标准创建过滤器:
| 标准 | 示例 | 描述 |
|---|---|---|
from | "sender@example.com" | 来自特定发件人的邮件 |
to | "recipient@example.com" | 发送给特定收件人的邮件 |
subject | "会议" | 主题中包含特定文本的邮件 |
query | "has:attachment" | Gmail搜索查询语法 |
negatedQuery | "垃圾邮件" | 必须不包含的文本 |
hasAttachment | true | 带有附件的邮件 |
size | 10485760 | 邮件大小(字节) |
sizeComparison | "larger" | 大小比较(larger,smaller) |
过滤器可以执行以下操作:
| 操作 | 示例 | 描述 |
|---|---|---|
addLabelIds | ["IMPORTANT", "Label_Work"] | 将标签添加到匹配的邮件 |
removeLabelIds | ["INBOX", "UNREAD"] | 从匹配的邮件中移除标签 |
forward | "backup@example.com" | 将邮件转发到另一个地址 |
服务器包括用于常见过滤场景的预构建模板:
fromSender)过滤来自特定发件人的邮件,并可选归档它们。
{
"template": "fromSender",
"parameters": {
"senderEmail": "newsletter@company.com",
"labelIds": ["Label_Newsletter"],
"archive": true
}
}
withSubject)过滤包含特定主题文本的邮件,并可选标记为已读。
{
"template": "withSubject",
"parameters": {
"subjectText": "[紧急]",
"labelIds": ["Label_Urgent"],
"markAsRead": false
}
}
withAttachments)过滤所有带有附件的邮件。
{
"template": "withAttachments",
"parameters": {
"labelIds": ["Label_Attachments"]
}
}
largeEmails)过滤大于指定大小的邮件。
{
"template": "largeEmails",
"parameters": {
"sizeInBytes": 10485760,
"labelIds": ["Label_Large"]
}
}
containingText)过滤包含特定文本的邮件,并可选标记为重要。
{
"template": "containingText",
"parameters": {
"searchText": "发票",
"labelIds": ["Label_Finance"],
"markImportant": true
}
}
mailingList)过滤邮件列表邮件,并可选归档它们。
{
"template": "mailingList",
"parameters": {
"listIdentifier": "dev-team",
"labelIds": ["Label_DevTeam"],
"archive": true
}
}
这里是一