Skip to main content
Glama
gemini-web-cdp
by hxl117

gemini-web-cdp

Zero-prompt local bridge and stdio MCP server for Gemini Web (gemini.google.com), combining a lightweight Chrome MV3 extension (port 9335) with an automatic isolated CDP browser fallback (port 9333) and lossless DOM-to-Markdown conversion.

SKILL.md 提供可直接加载到 Agent 的技能规范;plugin-template/ 提供 MCP 插件配置模板。

解决什么问题

在本地 Agent / IDE 工作流中调用网页端 Gemini(如 Carbon / High Thinking / 网页历史多轮对话 / 文件上传)时,常规方案常遇到三类阻塞:

  1. 远程调试弹窗阻塞:直接连接日常 Chrome DevToolsActivePort 会反复触发「Allow remote debugging?」人工授权弹窗。

  2. 后台标签冻结:Chrome 对非激活隐藏标签页会节流 Angular 变更检测与输入管线,导致点击发送后不响应。

  3. 长任务断流与重复发题:深度思考任务常耗时 5–10 分钟,普通一次性等待容易超时或盲目重发产生重复对话。

gemini-web-cdp 通过「本地扩展网关 + 独立自动化副窗口 + 20 秒启动确认兜底 + 状态机监控恢复」一次解决上述问题。

Related MCP server: Chrome Ops MCP

架构概览

Agent / CLI / MCP Client
        │  (stdio JSON-RPC 2.0 / CLI)
        ▼
gemini_mcp_server.mjs  ──托管──▶  127.0.0.1:9335 Local Hub (Host/Origin/MIME 校验)
        │                                   │
        ├─ Channel 1 (首选, 0 弹窗) ─────────┼── WebSocket (/gemini-ext) ──▶ Chrome MV3 扩展 (v1.3.1)
        │                                   │                                  └─ 独立自动化副窗口 (focused: true)
        │                                   │                                     20s 启动确认 + 结束后自动清理标签
        └─ Channel 2 (自动兜底) ─────────────┴──▶ 127.0.0.1:9333 独立 Chrome Profile (--headless=new / 离屏)
                                                                               └─ 20s 未启动 / 扩展未连时无感接管

核心特性

  • 零额外第三方 Node 依赖:仅使用 Node.js 内置标准库(http / net / crypto / child_process / readline)。

  • 双通道自动路由(--mode auto):

    • 优先复用日常已登录 Chrome 的 MV3 扩展(9335),不依赖 chrome.debugger,无顶部黄条。

    • 若扩展未连线,自动无感使用独立 Profile(9333)继续执行;点击发送后若 20 秒内未确认生成启动,则返回 SUBMISSION_UNKNOWN 并保留原页面供核对,绝不跨通道重复发题;仅在确认属于本工具管理的独立实例时才允许 close 关闭。

  • 默认 Carbon + High 与严格校验:默认切换 carbon 模型与 high(高 / 超级详尽)思考等级(传 keep 可跳过切换);模型未匹配时直接报错并返回页面可用模型列表。

  • 4 级附件上传回退:依次尝试 existing_input -> drop_xap(拖拽事件)-> plus_menu_input(点击 + 菜单主/次输入框)-> paste(剪贴板粘贴事件),单附件限制 20 MiB,适配 Gemini DOM 变动。

  • 60 秒心跳监测与防盲目重发状态机:

    • 尚未生成 /app/<id> 会话 ID 且 120 秒零输出时,停止并限量重发最多 1 次;

    • 一旦生成 /app/<id>,遇报错横幅或 240 秒停滞时优先刷新同一会话恢复(最多 2 次)或点击“重新生成”,绝不重复刷屏提问。

  • 高保真 DOM -> Markdown:html_to_md.py 自动剥离思考折叠块、按钮与图标,提取代码块语言标签、保护行内/块级 LaTeX 公式($...$ / $$...$$)、保留围栏代码块内部空行与 GFM 表格。

  • 本地网关双向密码学认证与安全边界:

    • 127.0.0.1:9335 在 HTTP 侧强制校验回环 Host、拒绝任意跨站 Origin、POST /ext-rpc 精确限制 application/json(并发上限 8、50 MB 请求体 / 20 MiB 单附件、30s 读超时);客户端与网关通过 0600/当前用户 ACL 保护的 extension_hub_auth.json 执行 HMAC-SHA256 双向挑战-响应与单次 Nonce 防重放(绝不向未验证监听者发送提示词或附件)。

    • WebSocket /gemini-ext 启用 5 秒握手超时、最大帧/分片上限与 Chrome 扩展 ID 固定(GEMINI_WEB_EXTENSION_ID 或首次连接自动 Pin),并通过一次性配对密钥(node scripts/gemini_cdp.mjs pair-key 写入扩展选项页)做 WebCrypto HMAC-SHA256 双向握手验证后才受理 RPC。

    • 独立 Chrome close 命令必须同时核对端口真实监听 PID、规范化独立 Profile 路径与状态文件归属;目标 URL 与页面 DOM 执行强制校验 location.origin === "https://gemini.google.com",并内置 busyTabIds 防止同标签并发提交冲突。

环境要求

  • Node.js >= 20.0.0(已通过 v24.16.0 验证)

  • Python >= 3.10 + 依赖库:

    pip install -r requirements.txt
  • Google Chrome >= 116(Windows 已实测;macOS / Linux 支持通过 CHROME_EXE 与 PYTHON_EXE / GEMINI_CDP_PROFILE_DIR 环境变量适配)

快速上手

1. 安装 Python 转换依赖与静态自检

pip install -r requirements.txt
npm run check

2. 在日常 Chrome 加载并一次性配对扩展

  1. 生成本机一次配对密钥:

    node scripts/gemini_cdp.mjs pair-key
  2. 打开 chrome://extensions,开启右上角 开发者模式,点击 加载已解压的扩展程序,选择本仓库的 extension/ 目录。

  3. 点击该扩展卡片的 扩展程序选项(Details -> Extension options),粘贴第 1 步输出的 pairKey 并点击 Save & Connect(密钥存于扩展本地存储,仅用于验证本地 127.0.0.1:9335 网关身份)。

  4. 后续运行 mcp 或 hub 时扩展每 20 秒自动重连本地 127.0.0.1:9335;需要热更新代码时可执行 node scripts/gemini_cdp.mjs reload-ext。

3. 配置原生 Stdio MCP Server(推荐)

在任意支持 MCP 的客户端(Antigravity / Claude Code / Codex / Cursor 等)配置中使用 plugin-template/mcp_config.example.json:

{
  "mcpServers": {
    "gemini-web-cdp": {
      "command": "node",
      "args": [
        "<PATH_TO_REPO>/scripts/gemini_mcp_server.mjs"
      ]
    }
  }
}

可用 MCP 工具包括:gemini_web_status、gemini_web_list、gemini_web_ask(支持 async: true 后台提交)、gemini_web_job_status、gemini_web_export、gemini_web_close。

4. CLI 直接调用示例

# 检查连接与登录状态
node scripts/gemini_cdp.mjs status --mode auto

# 列出最近会话
node scripts/gemini_cdp.mjs list --limit 10

# 发送提示词 + 附件,默认 Carbon + 高思考,结果写入 Markdown
node scripts/gemini_cdp.mjs ask --new-chat --attach ./context.md --prompt "请结合附件输出关键风险对比表。" --out-md ./reply.md

# 指定会话 URL 多轮追问(传入 --keep-tab 可保留自动化标签页)
node scripts/gemini_cdp.mjs ask --url https://gemini.google.com/app/<conversation_id> --prompt "请补充验证清单。"

# 导出整段会话为 Markdown
node scripts/gemini_cdp.mjs export --url https://gemini.google.com/app/<conversation_id> --out-md ./full_thread.md

# 若使用 9333 独立隔离兜底窗口:Windows 可运行 scripts/Start-GeminiChrome.cmd 手动登录,用完可执行
node scripts/gemini_cdp.mjs close --port 9333

环境变量说明

变量名

用途

默认行为

CHROME_EXE

自定义 Chrome 可执行文件路径

自动探测系统常见 Chrome 安装路径

GEMINI_CDP_PROFILE_DIR

自定义 9333 独立 Chrome Profile 目录

系统应用数据目录下的 Chrome_Gemini_CDP_Profile

PYTHON_EXE

自定义 Python 解释器路径

Windows 用 python,POSIX 用 python3

安全边界与免责声明

  • 本项目仅在本地回环地址(127.0.0.1)运行,不读取、导出或上传任何 Cookie、localStorage 或账号密码;遇到 accounts.google.com 登录页或验证码时直接返回 LOGIN_REQUIRED 由人工处理。

  • 严禁在自动化提示词或附件中上传受控机密文件、API Key 或个人隐私信息;请自行遵守目标网站的服务条款。

License

MIT

Tool Schema Changelog

Recent tool additions, removals, and schema changes observed during successful MCP inspections.

Loading tool schema changelog…

    Related MCP Connectors

    Related MCP Servers

    • A
      license
      B
      quality
      B
      maintenance
      Enables AI coding agents to inspect and operate Chrome as a developer, including reading DevTools console/network data, evaluating JavaScript, and managing unpacked extensions during an edit-test-debug loop.
      24
      MIT
    • A
      license
      Not graded
      quality
      C
      maintenance
      Enables local agents to send conversations and image-generation requests through user-authorized web chat and Flow Chrome tabs, poll or cancel jobs, and download generated images without API keys or remote debugging. Runs locally over stdio with explicit per-request confirmation and token-paired extension controls.
      MIT
    • A
      license
      Not graded
      quality
      B
      maintenance
      Enables AI agents to use selected signed-in Chrome tabs as MCP tools for reading pages, performing trusted interactions, inspecting DevTools errors, capturing network APIs, and taking screenshots, while also providing local OpenAI- and Anthropic-compatible chat endpoints backed by signed-in web chat tabs.
      Apache 2.0