py-mcp-google-工具箱



MCP 服务器为 AI 助手提供了与 Google 服务交互的强大工具,包括 Gmail、Google 日历、Google Drive 和 Google 搜索。
概述
py-mcp-google-toolbox 提供以下与 Google 相关的功能:
- Gmail操作(阅读、搜索、发送、修改)
- Google 日历管理(事件创建、列出、更新、删除)
- Google Drive 交互(搜索、读取文件)
- Google 搜索集成(搜索网页)
目录
先决条件
- Python :安装 Python 3.12 或更高版本
- Google Cloud 控制台设置:
- 前往Google Cloud Console
- 创建新项目或选择现有项目
- 启用服务 API:
- 前往“API 和服务”>“库”
- 搜索并启用“Gmail API”
- 搜索并启用“Google 日历 API”
- 搜索并启用“Google Drive API”
- 搜索表单并启用“自定义搜索 API”
- 从 GCP 设置 OAuth 2.0 凭据:
- 前往“API 和服务”>“凭证”
- 点击“创建凭证”>“OAuth 客户端 ID”
- 选择“Web应用程序”
- 记下客户端 ID 和客户端密钥
- 下载 secret json 并重命名为 credentials.json
- 生成 API 密钥
- 转到自定义搜索引擎并获取其 ID
安装
Git 克隆
git clone https://github.com/jikime/py-mcp-google-toolbox.git
cd py-mcp-google-toolbox
配置
- 安装 UV 包管理器:
curl -LsSf https://astral.sh/uv/install.sh | sh
- 创建并激活虚拟环境:
uv venv -p 3.12
source .venv/bin/activate # On MacOS/Linux
# or
.venv\Scripts\activate # On Windows
- 安装依赖项:
uv pip install -r requirements.txt
- 获取刷新令牌(如果令牌已过期,则可以运行此操作)
uv run get_refresh_token.py
这将:
- 打开浏览器进行 Google OAuth 身份验证
- 请求以下权限:
https://www.googleapis.com/auth/gmail.modify
https://www.googleapis.com/auth/calendar
https://www.googleapis.com/auth/gmail.send
https://www.googleapis.com/auth/gmail.readonly
https://www.googleapis.com/auth/drive
https://www.googleapis.com/auth/drive.file
https://www.googleapis.com/auth/drive.readonly
- 将凭证保存到
token.json
- 在控制台中显示刷新令牌
- 环境变量:
cp env.example .env
vi .env
# change with your key
GOOGLE_API_KEY=your_google_api_key
GOOGLE_CSE_ID=your_custom_search_engine_id
GOOGLE_CLIENT_ID=your_google_client_id
GOOGLE_CLIENT_SECRET=your_google_client_secret
GOOGLE_REFRESH_TOKEN=your_google_refresh_token
- 将 credentials.json 复制到项目根文件夹 (py-mcp-google-toolbox)
使用 Docker
- 构建 Docker 镜像:
docker build -t py-mcp-google-toolbox .
- 运行容器:
docker run py-mcp-google-toolbox
使用本地
- 运行服务器:
配置 MCP 设置
将服务器配置添加到您的 MCP 设置文件:
克劳德桌面应用程序
- 要通过Smithery自动安装:
npx -y @smithery/cli install @jikime/py-mcp-google-toolbox --client claude
- 要手动安装,请打开
~/Library/Application Support/Claude/claude_desktop_config.json
将其添加到mcpServers
对象:
{
"mcpServers": {
"Google Toolbox": {
"command": "/path/to/bin/uv",
"args": [
"--directory",
"/path/to/py-mcp-google-toolbox",
"run",
"server.py"
]
}
}
}
游标 IDE
打开~/.cursor/mcp.json
将其添加到mcpServers
对象:
{
"mcpServers": {
"Google Toolbox": {
"command": "/path/to/bin/uv",
"args": [
"--directory",
"/path/to/py-mcp-google-toolbox",
"run",
"server.py"
]
}
}
}
对于 Docker
{
"mcpServers": {
"Google Toolbox": {
"command": "docker",
"args": [
"run",
"-i",
"--rm",
"py-mcp-google-toolbox"
]
}
}
}
工具文档
Gmail 工具
list_emails
:列出 Gmail 收件箱中的最近电子邮件,并提供过滤选项search_emails
:执行高级 Gmail 搜索并检索详细的电子邮件内容send_email
:撰写并发送电子邮件,支持抄送、密送收件人modify_email
:通过修改标签来更改电子邮件状态(已读/未读、已存档、已删除)
日历工具
list_events
:检索指定时间范围内即将发生的日历事件create_event
:创建带有参与者、地点和描述的新日历事件update_event
:通过灵活的参数更新修改现有日历事件delete_event
:根据事件 ID 删除日历事件
驱动工具
read_gdrive_file
:从 Google Drive 文件中读取和检索内容search_gdrive
:使用可自定义的查询在 Google Drive 中搜索文件
搜索工具
search_google
:执行 Google 搜索并返回格式化的结果
发展
对于本地测试,您可以使用附带的客户端脚本:
# Example: List emails
uv run client.py list_emails max_results=5 query="is:unread"
# Example: Search emails
uv run client.py search_emails query="from:test@example.com"
# Example: Send email
uv run client.py send_email to="test@example.com" subject="test mail" body="Hello"
# Example: Modify email
uv run client.py modify_email id=MESSAGE_ID remove_labels=INBOX add_labels=ARCHIVED
# Example: List events
uv run client.py list_events time_min=2025-05-01T00:00:00+09:00 time_max=2025-05-02T23:59:59+09:00 max_results=5
# Example: Create event
uv run client.py create_event summary="new event" start=2025-05-02T10:00:00+09:00 end=2025-05-02T11:00:00+09:00 attendees="user1@example.com,user2@example.com"
# Example: Update event
uv run client.py update_event event_id=EVENT_ID summary="update event" start=2025-05-02T10:00:00+09:00 end=2025-05-02T11:00:00+09:00 attendees="user1@example.com,user2@example.com"
# Example Delete event
uv run client.py delete_event event_id=EVENT_ID
# Example: Search Google
uv run client.py search_google query="what is the MCP?"
# Example: Search Google Drive
uv run client.py search_gdrive query=mcp
# Example: Read file
uv run client.py read_gdrive_file file_id=1234567890
执照
MIT 许可证