article
README
🚀 Gmail MCP 服务器
Gmail MCP 服务器是一个基于 Model Context Protocol (MCP) 的工具,它提供了与 Gmail API 交互的能力。借助 MCP 接口,用户可以对电子邮件进行编程管理,极大地提升了邮件管理的效率和灵活性。
🚀 快速开始
Gmail MCP 服务器为用户提供了便捷的方式来管理 Gmail 邮件。通过 MCP 接口,用户可以轻松地列出、搜索、发送和修改邮件。
✨ 主要特性
Gmail 工具
list_emails:可列出收件箱中的近期邮件,并且支持可选过滤功能,方便用户快速定位所需邮件。search_emails:支持使用 Gmail 查询语法进行高级邮件搜索,满足用户多样化的搜索需求。send_email:能够发送新邮件,同时支持 CC 和 BCC 功能,方便进行多人邮件沟通。modify_email:可以修改邮件标签,如将邮件存档、移动到垃圾邮件箱、标记为已读或未读等。
📦 安装指南
先决条件
- Node.js:需要安装 Node.js 版本 14 或更高。
- Google Cloud Console 设置:
- 访问 Google Cloud Console。
- 创建新项目或选择现有项目。
- 启用 Gmail API:
- 转到 "APIs & Services" > "Library"。
- 搜索并启用 "Gmail API"。
- 设置 OAuth 2.0 凭证:
- 转到 "APIs & Services" > "Credentials"。
- 点击 "Create Credentials" > "OAuth client ID"。
- 选择 "Web 应用程序"。
- 设置 "Authorized redirect URIs" 包括:
http://localhost:3001/code。 - 记下 Client ID 和 Client Secret。
安装步骤
-
克隆和安装:
git clone https://github.com/yourusername/google-gmail-mcp-server.git cd google-gmail-mcp-server npm install -
创建 OAuth 凭证: 在根目录下创建一个
credentials.json文件:{ "web": { "client_id": "YOUR_CLIENT_ID", "client_secret": "YOUR_CLIENT_SECRET", "redirect_uris": ["http://localhost:3001/code"], "auth_uri": "https://accounts.google.com/o/oauth2/auth", "token_uri": "https://oauth2.googleapis.com/token" } } -
获取 Refresh Token:
node get-refresh-token.js这将:
- 打开浏览器进行 Google OAuth 认证。
- 请求以下权限:
https://www.googleapis.com/auth/gmail.modifyhttps://www.googleapis.com/auth/gmail.send
- 将凭证保存到
token.json。 - 在控制台显示 refresh token。
-
配置 MCP 设置: 将服务器配置添加到你的 MCP 设置文件中:
- 对于 VSCode Claude 扩展:
~/Library/Application Support/Code/User/globalStorage/saoudrizwan.claude-dev/settings/cline_mcp_settings.json - 对于 Claude 桌面应用:
~/Library/Application Support/Claude/claude_desktop_config.json
在
mcpServers对象中添加:{ "mcpServers": { "google-gmail": { "command": "node", "args": ["/path/to/google-gmail-mcp-server/index.js"], "env": {} } } } - 对于 VSCode Claude 扩展:
-
运行服务器:
npm start
💻 使用示例
基础用法
列出邮件
{
"command": "_gmail.list_emails",
"args": {
"userId": "me"
},
"response": {
"data": {
"messages": []
}
}
}
搜索邮件
{
"command": "_gmail.search_emails",
"args": {
"userId": "me",
"query": "inbox is:unread label:important"
},
"response": {
"data": {
"messages": []
}
}
}
发送邮件
{
"command": "_gmail.send_email",
"args": {
"userId": "me",
"email": {
"to": "recipient@example.com",
"subject": "Test Email",
"bodyText": "This is a test email."
}
},
"response": {
"data": {
"id": "string"
}
}
}
修改邮件
{
"command": "_gmail.modify_email",
"args": {
"userId": "me",
// 原文档此处未完整,保持原样
}
}
扫码联系在线客服