Skip to main content
Glama
Gemini Local Agent MCP

Gemini Local Agent MCP

by SumitaRinku

Gemini Local Agent MCP

English | 简体中文

把你本地电脑变成一个安全可控的 MCP Agent 服务,通过 Cloudflare Tunnel 加密透传公网 HTTPS,并利用内置自托管 OAuth 2.1 (PKCE + DCR) 与 Gemini Web (gemini.google.com) 的自定义应用连接器对接——让你直接用 Gemini 网页版 / 手机端额度读写本地代码、执行命令和调试项目。


架构概览

Gemini Web / Mobile (gemini.google.com / devmode)
   │
   │  HTTPS POST /mcp (Streamable HTTP + OAuth 2.1 Bearer Token)
   ▼
Cloudflare Edge (*.trycloudflare.com 或自定义域名)
   ▲
   │  出站加密隧道 (cloudflared --protocol http2,无需公网 IP / 端口映射)
   │
本机 cloudflared 进程
   │
   │  http://127.0.0.1:8787
   ▼
gemini-local-agent-mcp
   ├── OAuth 2.1 授权服务器 (PKCE S256 + RFC 7591 DCR + RFC 9728 Metadata)
   ├── 权限控制层 (PERMISSION_MODE: sandbox / readonly / developer)
   ├── 弹窗策略层 (TOOL_APPROVAL_MODE: auto_except_delete / auto_all / strict)
   ├── 14 个跨平台开发工具 (文件读写/原子多处编辑/搜索/Shell/后台任务)
   └── Codex / Claude Code 风格实时终端 UI (同步输出无闪烁 + 纯文本日志落盘)

Related MCP server: AKIMCP

核心特性

  • 开箱即用,零第三方鉴权依赖:内置完整 OAuth 2.1 + PKCE (S256) + 动态客户端注册 DCR (RFC 7591) + 资源元数据发现 (RFC 9728),无需注册 Auth0 或配置 Cloudflare Access,绑定时在浏览器输入本地口令即可。

  • 3 种权限隔离模式 (PERMISSION_MODE):

    • sandbox(默认推荐):通过 realpath 校验把读写与 cwd 锁定在 ALLOWED_ROOTS 白名单内,防 ../软链接越狱,并拦截高危破坏性系统命令。

    • readonly:仅向 Gemini 注册 5 个只读/搜索工具,彻底不暴露文件写入与终端执行。

    • developer:全盘文件与完整本地终端权限(适合个人完全信任的主力开发机)。

  • 解决 Gemini Web「始终允许」反复弹窗卡死:支持 TOOL_APPROVAL_MODE=auto_except_delete(默认),让日常改代码与跑命令零弹窗顺畅连续执行,仅对 delete_path 保留高危二次确认。

  • Codex / Claude Code 风格实时终端界面:启动后展示圆角状态面板、实时工具执行进度动画(正在读取文件… / 正在执行命令…)、树形历史记录(● Edit(...) / ⎿ ...),同时把完整纯文本日志写入 mcp-server.stdout.log。

  • 支持 3 种隧道运行模式:Cloudflare Quick Tunnel(免账号临时域名)、Cloudflare Named Tunnel(固定自定义域名)、纯本地服务(配合外部反代或 Gemini CLI / MCP Inspector)。


环境要求

  • Node.js >= 20.0.0

  • Cloudflare Tunnel CLI (cloudflared):


3 分钟快速开始

1. 安装依赖

git clone https://github.com/SumitaRinku/gemini-local-agent-mcp.git
cd gemini-local-agent-mcp
npm install

(可选)复制配置模板并按需修改工作目录与权限模式:

cp .env.example .env

如果跳过这一步,首次启动时程序会自动生成一份安全的默认 .env(默认 PERMISSION_MODE=sandbox,并自动生成高强度 ADMIN_PASSWORD)。

2. 启动本地 MCP + Cloudflare Quick Tunnel

Windows (PowerShell):

powershell -ExecutionPolicy Bypass -File .\scripts\start-quick-tunnel.ps1

macOS / Linux(或任意终端):

npm run tunnel

启动后终端会展示一个圆角卡片,包含本次生成的:

  • MCP URL:https://xxxx-xxxx.trycloudflare.com/mcp

  • OAuth 口令 (ADMIN_PASSWORD):首次绑定时在浏览器授权页输入

3. 在 Gemini Web 绑定

  1. 打开 https://gemini.google.com(或开发者调试台 https://gemini.google.com/devmode)。

  2. 进入 Settings → Connected Apps → Custom apps → Add a custom app(DevMode 下点击 + Create new)。

  3. 填入终端显示的 MCP URL(注意末尾必须带 /mcp),Client ID / Secret 留空,直接点下一步。

  4. 浏览器弹出本机授权页后,输入终端里的 OAuth 口令 并点击「确认授权」即可开始使用!

详细图文与 Gem 系统提示词模板见:docs/GEMINI_SETUP.md


终端实时界面预览

╭──────────────────────────────────────────────────────────────────────────────────────╮
│ gemini-local-agent-mcp 已就绪 (Ready)                                                │
├──────────────────────────────────────────────────────────────────────────────────────┤
│ MCP URL   : https://xxxx-xxxx.trycloudflare.com/mcp                                  │
│ OAuth 口令: <auto-generated-password>                                                │
│ 权限/确认 : Quick Tunnel · sandbox · auto_except_delete · 14 tools                   │
╰──────────────────────────────────────────────────────────────────────────────────────╯

◆ OAuth 已自动注册客户端 (Google)  [20:05:12]
◆ OAuth 授权完成 · Gemini 已连接就绪  [20:05:18]
◆ Gemini 已同步工具列表,待命中  [20:05:19]
● Read(src/app.js · lines 1-120)  [20:05:24]
  ⎿  已读取 120 行 (共 240 行) · 3ms
● MultiEdit(src/app.js · 2 edits)  [20:05:29]
  ⎿  已更新 2 处 · 2ms

⠹ 正在执行命令 npm test… (1.4s)
  隧道: https://xxxx-xxxx.trycloudflare.com/mcp  │  权限: sandbox  │  已调用: 2  │  后台任务: 0  │  Ctrl+C 退出

模式矩阵速览

完整的配置说明与 4 套开箱即用 .env 预设请查阅:docs/MODES.md

维度

可选值

默认值

说明

权限模式 (PERMISSION_MODE)

sandbox / readonly / developer

sandbox

限制沙盒目录 / 仅暴露 5 个只读工具 / 全盘完整开发权限

弹窗策略 (TOOL_APPROVAL_MODE)

auto_except_delete / auto_all / strict

auto_except_delete

日常编辑与命令免弹窗 / 全部免弹窗 / 写与执行逐次弹窗

隧道模式 (启动方式)

Quick Tunnel / Named Tunnel / Local

Quick Tunnel

免账号随机域名 / 自有固定域名 / 纯本地 127.0.0.1:8787

鉴权模式 (AUTH_MODE)

oauth / none

oauth

OAuth 2.1 + PKCE + DCR / 无鉴权(公网隧道默认禁止启用)


内置工具清单(共 14 个)

在 PERMISSION_MODE=readonly 下仅注册前 5 个只读工具。

类别

工具名

功能描述

系统信息

get_system_info

查看系统版本、CPU/内存、当前权限模式、白名单目录、磁盘及 CLI 版本

目录浏览

list_directory

列出目录内容(支持递归深度与条目限制,自动跳过 .git / node_modules)

文件读取

read_file

按 1-based 行号区间读取 UTF-8 文本文件(自动检测并拦截二进制文件)

全文搜索

search_files

递归搜索文件内容(支持纯文本 / 正则 / 文件名通配过滤)

文件查找

find_files

按文件名通配符或子串递归查找文件与目录

文件写入

write_file

创建、覆盖或追加写入 UTF-8 文本文件(自动创建父目录)

单处编辑

edit_file

精确匹配替换文本(自动兼容 CRLF/LF,防误替换)

原子多处编辑

multi_edit_file

单次请求对同一文件顺序做多处精确替换,任意一处失败则原子回滚

删除路径

delete_path

删除文件或目录(内置保护 OS 根目录防误删)

同步命令

run_command

执行本地 Shell 命令(Windows PowerShell/CMD,Unix bash/sh),支持超时杀进程树

启动后台任务

start_background_task

启动 dev server、长编译等后台任务并立即返回 taskId

查询任务输出

get_task_output

获取后台任务实时日志尾部,支持最长 25 秒等待

任务列表

list_tasks

查看当前会话启动的所有后台任务状态

停止后台任务

stop_task

强制结束指定后台任务及其完整子进程树


常见问题排查 (FAQ)

1. 开了代理/VPN(如 Clash / FlClash)后 Cloudflare Tunnel 连不上或超时?

很多代理工具会拦截或丢弃 QUIC(UDP 7844)流量。本项目已默认使用最稳定的 CLOUDFLARED_PROTOCOL=http2。如需手动切换,可在 .env 修改或启动时传入 --protocol quic / --protocol http2。

2. Gemini Web 每次调用工具都弹窗点「始终允许」,甚至卡死转圈?

这是 Gemini Web 对标有 destructiveHint: true 的自定义 MCP 工具的已知前端交互问题。请确保 .env 中 TOOL_APPROVAL_MODE=auto_except_delete(默认)或 auto_all,重启服务后在 Gemini 的 Connected Apps 中移除旧应用重新添加一次(或在 DevMode 点 Sync schema)即可。

3. Quick Tunnel 重启后域名变了怎么办?

Quick Tunnel 每次重启都会分配新的 *.trycloudflare.com 地址。如果希望长期使用固定域名(例如 https://mcp.yourdomain.com/mcp),请按 docs/MODES.md 中的 Named Tunnel 步骤绑定自己的 Cloudflare 域名,以后重启无需在 Gemini 里重新配置。

4. 如何运行自动化验证测试?

# 运行多权限模式、弹窗策略与 TUI 渲染测试
npm run test:modes

# 运行 12 步 OAuth 2.1 + Streamable HTTP 端到端冒烟测试(需先启动服务或传入隧道 URL)
npm run test:smoke -- http://127.0.0.1:8787

安全与文档导航


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
      Not graded
      quality
      B
      maintenance
      Enables web-based AI agents such as claude.ai, ChatGPT and Grok to read and edit files and run allowlisted shell commands on your own machine, plus control Chromium, DevTools, ports, git, SQLite and background tasks. Access is exposed over HTTPS with OAuth 2.1 through a swappable public edge (Tailscale Funnel or a Cloudflare tunnel), with a loopback-only control panel for configuration.
      MIT
    • F
      license
      Not graded
      quality
      A
      maintenance
      Connects web-based AI chat clients such as Claude, ChatGPT, and Grok to a local machine through a single OAuth-gated MCP endpoint so they can browse, read, write, and edit files, run whitelisted shell commands, search content, use Git, query SQLite, drive browsers and DevTools, and manage background tasks, ports, and clipboard. It also exposes localhost and LAN services via a protected HTTP client, letting remote or mobile clients debug and operate local projects without granting unrestricted shell access.
      -
    • A
      license
      Not graded
      quality
      A
      maintenance
      Exposes an OAuth-gated MCP endpoint that lets web-based AI clients work directly on a local machine through 38 governed tools covering files, shell commands, search, Git, SQLite, browser automation, DevTools, background tasks, localhost services, clipboard, notifications, ports, and Postman control. Local tools can connect straight to the loopback address for offline, low-latency use, while remote clients reach the same machine over an optional public ingress.
      MIT
    • A
      license
      Not graded
      quality
      C
      maintenance
      Enables ChatGPT Web and Google Gemini to securely connect to a Windows PC over OAuth 2.1-authenticated HTTPS, exposing sandboxed filesystem, Git, shared Chrome, and PowerShell command tools through Cloudflare Tunnel or a self-hosted Linux VPS.
      16
      MIT