介绍
该项目为使用Python编写的MCP(模型控制协议)服务器提供了一个与FastMCP的Google Chat集成。它允许您通过MCP工具访问Google聊天空间和消息并与之交互。
结构
该项目由两个主要部分组成:
- 带谷歌聊天工具的MCP服务器:提供通过模型控制协议与Google聊天进行交互的工具。
- 由FastMCP编写 - server.py:使用Google聊天工具实现主MCP服务器 - google_chat.py:Google Chat API集成和身份验证处理
- 认证服务器:用于Google帐户身份验证的独立组件
- 由FastAPI编写 - 使用Google处理OAuth2流 - 存储和管理访问令牌 - 可以独立运行,也可以作为MCP服务器的一部分运行 - server_auth.py:身份验证服务器实现
身份验证流程允许您获取和刷新Google API令牌,然后MCP工具使用这些令牌访问Google Chat数据。(您的空间和消息)
特性
- 使用Google Chat API进行OAuth2身份验证
- 列出可用的Google聊天空间
- 使用日期筛选从特定空间检索邮件
- 本地身份验证服务器,便于设置
需求
- Python 3.13+
- 启用了以下API的Google Cloud项目:
- 谷歌聊天API - 人员API(用于用户显示名称)
- 来自Google Cloud控制台的OAuth2凭据
如何使用?
准备Google Oauth登录
- 克隆此项目
git clone https://github.com/chy168/google-chat-mcp-server.git
cd google-chat-mcp-server- 准备一个谷歌云项目(GCP)
- 谷歌云Conolse(https://console.cloud.google.com/auth/overview?project=\)
- 谷歌认证平台>客户端>(+)创建客户端>Web应用程序
参考:https://developers.google.com/identity/protocols/oauth2/?hl=en 授权的JavaScript源代码添加: http://localhost:8000 授权重定向URI: http://localhost:8000/auth/callback
- 创建OAuth 2.0客户端后,将客户端机密下载为
.json文件。另存为credentials.json在项目的顶层。
认证
有两种身份验证模式可供选择:
选项1:CLI模式(建议用于无头/远程环境)
uv run python server.py --auth cli这将:
- 显示授权URL
- 在任何浏览器中打开URL(可以在其他设备上)
- 完成谷歌授权
- 从浏览器复制重定向URL并将其粘贴回终端
- 令牌将另存为
token.json
选项2:Web模式(适用于具有本地浏览器的环境)
uv run python server.py --auth web --port 8000- 打开浏览器http://localhost:8000/auth
- 完成谷歌登录
- 令牌将另存为
token.json
MCP配置(MCP.json)
{
"mcpServers": {
"google_chat": {
"command": "uv",
"args": [
"--directory",
"/google-chat-mcp-server",
"run",
"server.py",
"--token-path",
"/google-chat-mcp-server/token.json"
]
}
}Docker/Podman
运行容器
# Mount your project directory containing token.json
docker run -it --rm \
-v /path/to/your/project:/data \
ghcr.io/chy168/google-chat-mcp-server:latest \
--token-path=/data/token.json
# or with podman
podman run -it --rm \
-v /path/to/your/project:/data \
ghcr.io/chy168/google-chat-mcp-server:latest \
--token-path=/data/token.json在容器中运行身份验证服务器
# Web mode
docker run -it --rm \
-p 8000:8000 \
-v /path/to/your/project:/data \
ghcr.io/chy168/google-chat-mcp-server:latest \
--auth web --host 0.0.0.0 --port 8000 --token-path=/data/token.json
# CLI mode (for headless environments)
docker run -it --rm \
-v /path/to/your/project:/data \
ghcr.io/chy168/google-chat-mcp-server:latest \
--auth cli --token-path=/data/token.json工具
MCP服务器提供以下工具:
谷歌聊天工具
get_chat_spaces()-列出机器人可以访问的所有谷歌聊天空间get_space_messages(space_name: str, start_date: str, end_date: str = None)-列出来自特定谷歌聊天空间的消息,并可选择时间过滤
开发和调试
塑造形象
docker build -t google-chat-mcp-server:latest .
# or
podman build -t google-chat-mcp-server:latest .调试
fastmcp dev server.py --with-editable .