Google MCP
# Google MCP(google-mcp)
一个叫 **Google MCP** 的自研 stdio 服务器,三块能力合一体:
1. **GitHub 只读桥接**:把原 github-mcp 的仓库/Issue/PR/提交/代码搜索等只读能力直接融进来(`github_*` 前缀,19 个工具),需 `GITHUB_TOKEN`。
2. **Google Workspace**:Gmail + Drive + Calendar,OAuth2 个人账号授权,零第三方依赖(只用 `@modelcontextprotocol/sdk` + Node 内置模块)。
3. **浏览器式工具(对标 Edge MCP)**:内置同源 CDP 引擎(`vendor/edge/`,随仓库自带,单独 clone 即可跑),开标签/导航/读页/查找/点击/输入/截图/eval/网络/存储,**独立浏览器实例(端口 9333)**,与 Edge MCP 的 9222 互不干扰。
> **浏览器选择器(重要)**:Edge MCP 和 Google MCP 都能做浏览器操作。两者的 `browser_pick` 工具会返回 `question + options`,模型必须原样用 `ask_user_question` 弹给用户选「Edge MCP」还是「Google MCP」,按选择调用 `edge_*` 或 `google_*`,不要替用户选。
## 能力
| 类别 | 工具 | 说明 |
|---|---|---|
| GitHub | `github_status` / `github_whoami` / `github_search_repositories` / `github_get_repository` | 状态/账号/仓库搜索与读取,只读 |
| GitHub | `github_list_branches` / `github_list_commits` / `github_get_commit` / `github_list_tags` / `github_list_releases` | 分支/提交/Tag/Release,只读 |
| GitHub | `github_get_file` / `github_list_directory` / `github_get_readme` | 文件/目录/README 内容,只读 |
| GitHub | `github_list_issues` / `github_get_issue` / `github_list_pulls` / `github_get_pull` / `github_get_pull_diff` | Issue 与 PR(含 diff),只读 |
| GitHub | `github_search_code` / `github_search_issues` | 代码/Issue 跨仓搜索,只读 |
| 认证 | `google_status` / `google_auth` / `google_revoke` | 状态/浏览器授权/撤销。**token 值永不返回或打印** |
| Gmail | `gmail_search` / `gmail_read` | 搜索(Gmail 语法)与读正文,只读 |
| Gmail | `gmail_send_draft` → `gmail_send_commit` | 发信两步确认 |
| Drive | `drive_search` / `drive_read` | 文件元数据搜索;Docs→txt、Sheets→csv 导出,只读 |
| Calendar | `calendar_list` / `calendar_events` | 列日历/事件,只读 |
| Calendar | `calendar_insert_draft` → `calendar_insert_commit` | 新建日程两步确认 |
| Calendar | `calendar_patch_draft` → `calendar_patch_commit` | 修改日程两步确认 |
| Calendar | `calendar_delete_draft` → `calendar_delete_commit` | 删除日程两步确认(不可恢复) |
| 选择器 | `browser_pick` | ★ 浏览器操作前必调,弹选项让用户选 Edge / Google MCP |
| 浏览器 | `google_browser`(connect/launch/status/quit/login)/`google_tabs`/`google_goto` | 连接/启动独立浏览器、标签管理、导航 |
| 浏览器 | `google_read` / `google_find` / `google_click` / `google_type` / `google_key` / `google_scroll` | 读页/找元素/点击/输入/按键/滚动 |
| 浏览器 | `google_screenshot` / `google_eval` / `google_wait` / `google_dialog` | 截图/执行 JS/等待/弹窗 |
| 浏览器 | `google_storage` / `google_console` / `google_network` / `google_mock` / `google_cdp` | 存储 cookie/控制台/网络流量/伪造响应/原始 CDP |
读工具可直接调用;所有写工具必须 **draft → 展示预览给用户 → 用户明确同意 → commit**。commit 必须带用户对预览说的确认原话(`consent`)。没有直接删邮件的工具。
浏览器工具跑在独立 profile(默认端口 `9333`,可用 `GOOGLE_BROWSER_PORT` 改)。`google_browser(op="login")` 打开 Google 登录页,Workspace API 授权仍走 `google_auth`(loopback + PKCE,token 只存本地)。浏览器操作前记得先 `browser_pick` 让用户选。
## 配置(一次性)
### 1. 建 OAuth 客户端
1. 打开 [Google Cloud Console → 凭据](https://console.cloud.google.com/apis/credentials),建一个项目(或选已有的)。
2. **API 和服务 → 库**,分别启用 **Gmail API**、**Google Drive API**、**Google Calendar API**。
3. 凭据 → 创建凭据 → **OAuth 客户端 ID** → 应用类型选 **桌面应用**(desktop app)。
4. 拿到 **客户端 ID** 和 **客户端密钥**。桌面应用允许任意 loopback 重定向端口,不用手动填 redirect URI(服务器每次随机取 `127.0.0.1:<随机端口>/callback`)。
5. 把你的账号加为**测试用户**(OAuth 同意屏幕 → 测试用户),否则授权时会报 "unverified app"。
> OOB(`urn:ietf:wg:oauth:2.0:oob`)已被 Google 废弃,所以这里用 loopback + PKCE,不用复制粘贴验证码的旧流程。万一自动回调收不到,也支持把浏览器地址栏的完整 URL 用 `google_auth` 的 `manualCode` 参数粘进来。
### 2. 给凭据
支持两种(环境变量优先):
- 环境变量:`GOOGLE_CLIENT_ID`、`GOOGLE_CLIENT_SECRET`、`GITHUB_TOKEN`(GitHub 只读桥接;等价于旧 github-mcp 的 `GITHUB_PERSONAL_ACCESS_TOKEN`)
- 配置文件 `<包目录>/config.json`(已 gitignore):
```json
{
"clientId": "xxx.apps.googleusercontent.com",
"clientSecret": "GOCSPX-xxx",
"scopes": ["https://www.googleapis.com/auth/gmail.readonly", "..."],
"githubToken": "ghp_xxx"
}
```
> GitHub 只读桥接零额外依赖:用 Node 内置 `fetch` 直连 `https://api.github.com`(Bearer token),token 只从配置/环境变量读取,绝不回传或打印。`github_status` 可查看是否就绪;`GITHUB_API_BASE` 可改 API 端点(企业版)。GitHub 与 Google 两套凭据互不干扰,可以只配其中一套。
### 3. 授权
让模型调 `google_auth`:会打开浏览器 → Google 登录/同意 → 本地回调收到 code → 换 token 落到 `<包目录>/.state/token.json`(权限 0600)。token 过期自动用 refresh_token 刷新,不用重复登录。
其他环境变量:`GOOGLE_MCP_CONFIG`(配置文件路径)、`GOOGLE_MCP_TOKEN_FILE`(token 路径)、`GOOGLE_SCOPES`(空格分隔覆盖默认 scope)、`GOOGLE_BROWSER_HOST` / `GOOGLE_BROWSER_PORT`(浏览器调试端点,默认 127.0.0.1:9333)、`GOOGLE_SHOT_DIR`(截图目录)。默认 scope = Gmail 只读+发信、Drive 只读、Calendar 读写、openid、userinfo.email。要加能力就加 scope 后重新 `google_auth`。
## 在 DSH 里用
dsh-mcp 管理器的 `src/lib/catalog.js` 里已有 `google` 模板。用 manager 装:
```bash
node bin/dsh-mcp.js add google \
--param clientId=xxx.apps.googleusercontent.com \
--param clientSecret=GOCSPX-xxx
```
然后让 DSH 重载补丁(DSH 是 live reload),调 `google_auth` 完成一次登录即可。
> 凭据用 `--param` 传进去会写进 `cordis.patch.yml`。模板对 `GOOGLE_CLIENT_ID` / `GOOGLE_CLIENT_SECRET` / `GITHUB_TOKEN` 都用 DSH 的 `!!js` 表达式从**主进程环境变量**取值,写法必须是带 `${}` 的 ``!!js "`${process.env.X}`"``,不能写成 ``!!js "`process.env.X`"``(后者没有 `${}`,求值结果是字面量字符串 `process.env.X`,会导致「看起来配了、实际是假值」)。
>
> **重要**:DSH 主进程只在**启动时**继承环境变量。如果你在 DSH 运行期间才设置 `GITHUB_TOKEN`(例如 `[Environment]::SetEnvironmentVariable(...,"User")`),即使重启 MCP 子进程也读不到,GitHub 工具会一直 401。这种情况要么**完全退出并重启 DSH 主进程**,要么把 token 直接写进 `cordis.patch.yml` 的 `env.GITHUB_TOKEN`,或在包目录的 `config.json` 里配 `githubToken`(该文件已在 .gitignore,不会提交)。`github_status` 只检查「有没有 token 字符串」,不验证有效性;用 `github_whoami` 才能确认 token 真的生效(成功返回 `login`)。
## 开发 / 验证
```bash
node verify.js # 离线自检(不联网、不碰真 token)
node --test --test-isolation=none test/unit.test.js # 单元测试
node server.js # 直接跑 stdio 服务器(需先配凭据)
```
仓库沙箱里 `node --test` 默认会 spawn 子进程(被 EPERM 拦),所以测试命令带 `--test-isolation=none` 在进程内跑。
## 安全说明
- token 文件 0600,且从不进入日志或工具返回值;`google_status` 只显示 scope、过期时间、是否有 refresh_token。
- `confirmId` 是一次性的、10 分钟过期、hash 防篡改;确认单只存在内存里,重启即失效。
- 写操作(发信/日程增删改)在没有用户对预览点头前不会发出。
- revoke 需要用户明确说「确认撤销 Google 授权」。
TDQS
Scored across 55 tools
Most tools have clearly distinct purposes, and the draft/commit pairs (gmail_send, calendar_insert/patch/delete) are explicitly labeled as two-step, which removes ambiguity. Minor overlaps exist between escape-hatch tools (google_eval vs google_cdp, google_read outline vs aria) and the two browser entry points (browser_pick vs google_browser), but descriptions explain the difference well enough.
There is a good domain-prefix scheme (google_, gmail_, drive_, calendar_, github_) that makes grouping predictable, but within prefixes the convention is mixed: some tools are verb_noun (github_get_issue, gmail_search), others bare verbs (google_goto, google_click), and others bare nouns (google_tabs, google_console). browser_pick breaks the pattern entirely and the Google prefix covers both Workspace and browser tools.
55 tools is far beyond the 25+ threshold and the server bundles four separate domains: browser automation (~20 tools), Gmail, Drive, Calendar, and a GitHub read-only bridge. Each area individually is reasonably scoped, but the combined surface is heavy and hard for an agent to navigate without misfires.
Browser automation and Calendar CRUD are thorough (with safe draft/commit confirmation), and the GitHub bridge is a complete read-only surface. However, Gmail lacks thread/label/modify/delete operations and Drive is read-only (no upload/create/write), leaving notable gaps in two of the four advertised domains.