一个使用 MCP(消息控制协议)框架构建的强大且灵活的 Gmail 集成服务器。此服务器提供了一个强大的接口来与 Gmail API 进行交互,提供了读取、发送和管理电子邮件的程序化功能。
在运行 Gmail MCP 服务器之前,请确保您拥有以下内容:
要通过 Smithery 自动安装 Gmail 集成服务器:
npx -y @smithery/cli install @Quantum-369/Gmail-mcp-server --client claude
git clone <your-repository-url>
cd gmail-mcp-server
python -m venv venv
# 在 Windows 上
venv\Scripts\activate
# 在 Unix/MacOS 上
source venv/bin/activate
pip install .
sudo apt install python3.12 python3.12-venv -y
python3.12 -m venv venv
source venv/bin/activate
client_secret.json 文件放置在项目根目录中。python gmail_token_creator.py
python gmail_server.py
client_secret.json 并放置在项目根目录中gmail_token_creator.py 中设置电子邮件标识符:email_identifier = 'your.email@gmail.com' # 对于每个账户更改此值
python gmail_token_creator.py
脚本会打印一个授权 URL。将该 URL 复制到您的网络浏览器中,完成 Google 同意流程,并在提示时将验证码复制回终端。将在 token_files/ 目录中创建一个用于未来使用的令牌文件。
gmail_server.py: 主 MCP 服务器实现gmail_api.py: Gmail API 交互函数google_apis.py: Google API 认证工具read_emails.py: 邮件读取功能search_emails.py: 邮件搜索功能send_emails.py: 邮件发送功能python gmail_server.py
您还可以使用提供的 Dockerfile 构建容器镜像:
docker build -t gmail-mcp-server .
docker run -v $(pwd)/client_secret.json:/app/client_secret.json \
-v $(pwd)/token_files:/app/token_files gmail-mcp-server
容器运行相同的服务器,并将认证令牌存储在主机上的 token_files 目录中,以便它们在运行之间持久存在。
await send_gmail(
email_identifier="your.email@gmail.com",
to="recipient@example.com",
subject="测试主题",
body="邮件正文内容",
attachment_paths=["附件路径"]
)
await search_email_tool(
email_identifier="your.email@gmail.com",
query="from:someone@example.com",
max_results=3
0,
include_conversations=True
)
await read_latest_emails(
email_identifier="your.email@gmail.com",
max_results=5,
download_attachments=False
)
await download_email_attachments(
email_identifier="your.email@gmail.com",
msg_id="message_id",
download_all_in_thread=False
)
client_secret.json 并且永远不要将其提交到版本控制系统.gitignore服务器包括全面的错误处理和日志记录:
gmail_mcp.logApache License 2.0
对于问题和功能请求,请使用 GitHub 问题跟踪器。