Gmail MCP服务器(Rust)
 
通过模型上下文协议实现的用于Claude/AI集成的Gmail MCP服务器的高性能Rust实现。
灵感来源: 共和/Gmail MCP服务器 (TypeScript) 这是一个完整的Rust重写,提供了更好的性能、内存安全和本机二进制文件。
特性
- OAuth 2.0身份验证:通过令牌刷新保护Google OAuth流
- 电子邮件操作:发送、起草、阅读、搜索、修改和删除电子邮件
- 附件支持:发送带有文件附件的电子邮件,下载附件
- 标签管理:创建、更新、删除和列出Gmail标签
- 过滤器管理:使用条件和操作创建筛选器,包括模板
- 批量操作:高效批量修改/删除多条消息
- 完整的MCP协议:实现AI工具集成的模型上下文协议
- 综合测试:61个测试(24个单元+37个集成),没有任何刺耳的警告
可用工具(共19个)
| 工具 | 说明 |
|---|---|
send_email | 发送一封新电子邮件(带可选附件) |
draft_email | 创建电子邮件草稿 |
read_email | 按ID阅读特定电子邮件 |
search_emails | 使用Gmail查询语法搜索电子邮件 |
modify_email | 在电子邮件中添加/删除标签 |
delete_email | 将电子邮件移至垃圾箱 |
list_email_labels | 列出所有Gmail标签 |
batch_modify_emails | 修改多封电子邮件上的标签 |
batch_delete_emails | 删除多封电子邮件 |
create_label | 创建新标签 |
update_label | 更新标签的属性 |
delete_label | 删除标签 |
get_or_create_label | 获取现有标签或创建新标签 |
create_filter | 创建新筛选器 |
list_filters | 列出所有筛选器 |
get_filter | 获取特定筛选器 |
delete_filter | 删除筛选器 |
create_filter_from_template | 从预定义模板创建筛选器 |
download_attachment | 下载电子邮件附件 |
先决条件
- Rust 1.70+(通过安装 生锈)
- 启用Gmail API的谷歌云项目
- OAuth 2.0凭据(桌面应用程序类型)
安装
从源代码构建
git clone https://github.com/abhinav-copilot/gmail-mcp-server-rust.git
cd gmail-mcp-server-rust
cargo build --release二进制文件将位于 target/release/gmail-mcp-server.
设置
1.创建Google Cloud OAuth凭据
- 首选 谷歌云控制台
- 创建新项目或选择现有项目
- 启用Gmail API
- 转到凭据→ 创建凭据→ OAuth 2.0客户端ID
- 选择“桌面应用程序”作为应用程序类型
- 下载JSON文件
2.配置凭据
将下载的OAuth文件另存为:
~/.gmail-mcp/gcp-oauth.keys.json(推荐)./gcp-oauth.keys.json(当前目录)
或者设置环境变量:
export GMAIL_OAUTH_PATH=/path/to/gcp-oauth.keys.json3.身份验证
# Run authentication flow
./gmail-mcp-server auth这将打开您的浏览器以获得Google OAuth同意。批准后,凭据将存储在 ~/.gmail-mcp/credentials.json.
用法
独立服务器
# Start the MCP server (communicates via stdio)
./gmail-mcp-server使用克劳德桌面/光标
添加到您的MCP配置中(例如。, ~/.config/claude/claude_desktop_config.json):
{
"mcpServers": {
"gmail": {
"command": "/path/to/gmail-mcp-server",
"args": []
}
}
}发送带有附件的电子邮件
这 send_email 和 draft_email 工具支持文件附件:
{
"name": "send_email",
"arguments": {
"to": ["recipient@example.com"],
"subject": "Document attached",
"body": "Please see the attached file.",
"attachments": ["/path/to/document.pdf", "/path/to/image.png"]
}
}支持的附件类型:PDF、Word、Excel、图像(PNG、JPG、GIF)、文本、CSV、JSON、XML、ZIP。
项目结构
src/
├── lib.rs # Library crate root
├── main.rs # Binary entry point, CLI handling
├── config.rs # Configuration management
├── error.rs # Error types with thiserror
├── gmail/
│ ├── mod.rs # Gmail module exports
│ ├── types.rs # Gmail API types (serde)
│ ├── auth.rs # OAuth 2.0 authentication
│ ├── client.rs # Gmail API client
│ ├── utils.rs # Email utilities, attachment support
│ ├── labels.rs # Label management
│ └── filters.rs # Filter management
└── mcp/
├── mod.rs # MCP module exports
├── types.rs # MCP protocol types
├── server.rs # MCP server (stdio transport)
└── tools.rs # Tool definitions & handlers
tests/
└── integration_tests.rs # Integration tests发展
# Build
cargo build
# Run tests
cargo test
# Lint
cargo clippy
# Format
cargo fmt
# Build optimized release
cargo build --release配置
| 环境变量 | 描述 | 默认值 |
|---|---|---|
GMAIL_OAUTH_PATH | OAuth密钥文件的路径 | ~/.gmail-mcp/gcp-oauth.keys.json |
GMAIL_CREDENTIALS_PATH | 存储令牌的路径 | ~/.gmail-mcp/credentials.json |
GMAIL_OAUTH_PORT | OAuth回调端口 | 3000 |
RUST_LOG | 日志级别(跟踪、调试、信息、警告、错误) | info |
许可证
此项目根据MIT许可证获得许可-请参阅 许可证 文件以获取详细信息。
致谢
- 原始实施: 共和/Gmail MCP服务器 -启发Rust移植的TypeScript实现
- 模型上下文协议: 模型上下文协议.io -协议规范
- Anthropic:创建克劳德和MCP生态系统
