gemini-local-bridge-mcp
by 37chengshan
README.md
# gemini-local-bridge-mcp
> **中文**:零依赖、跨平台(macOS / Windows / Linux)本地文件与搜索 **MCP** 服务,面向 **Gemini App**、Claude Desktop 及各类 AI IDE。仅通过 `stdio`(NDJSON + `Content-Length` 双帧)说标准 MCP(`protocolVersion: 2025-06-18`),**不**引入任何 npm 外部依赖,**只**用 Node.js ≥ 18 内置模块。带 **4KB 级响应帧预算** 防客户端卡顿、跨平台路径适配(`darwin` / `win32` / `linux`)、`--read-only`、`--root` 目录隔离与敏感凭据路径拦截。
>
> **English**: Zero-dependency, cross-platform (macOS / Windows / Linux) local file & search **MCP** server for **Gemini App**, Claude Desktop, and AI IDEs. Speaks standard MCP (`protocolVersion: 2025-06-18`) over `stdio` (NDJSON + `Content-Length` dual framing), pulls in **no external npm packages**, and uses only **Node.js ≥ 18** built-ins. Features a **4KB-class response-frame budget** to prevent client stalls, cross-platform path handling (`darwin` / `win32` / `linux`), `--read-only`, `--root` scoping, and sensitive-credential path guardrails.
License: **MIT** (see `LICENSE`). Package: `gemini-local-bridge-mcp` `0.1.0`, ESM (`"type": "module"`), `engines.node >= 18`. No `postinstall`, no native build, no telemetry.
---
## 1. 核心特性 / Key features
### 零外部依赖 / Zero external dependencies
- **零外部依赖 / Zero external dependencies.** Runtime needs **only Node.js ≥ 18**. No `npm install`, no `node_modules`, no native addons, no bundled `grep`/`ripgrep`, no `find`. Just `node src/server.mjs`.
- **零外部依赖**:运行只需 **Node.js ≥ 18**,**无需 `npm install`**,无 `node_modules`、无原生模块、不依赖外部 `grep`/`ripgrep`/`find`。直接 `node src/server.mjs`。
- Everything uses `node:fs`, `node:os`, `node:path`, `node:url`, `node:assert`. The installer (`install.mjs`) is likewise dependency-free. **全部 `node:*` 内置;安装器同样零依赖。**
- The test suite runs with `node tests/test-all.mjs` — no dev dependencies either. **测试也无任何开发依赖。**
### 跨平台路径适配 / Cross-platform path handling (`darwin` / `win32` / `linux`)
- **OS detection** via `process.platform`; supported **`darwin`**, **`win32`**, **`linux`** (Linux/POSIX fallthrough). **`process.platform` 检测;`darwin`/`win32`/`linux`。**
- **Correct path semantics per OS**: `path.win32` on Windows vs `path.posix` on macOS/Linux (`\` vs `/`, drive letters); Windows root-scope matching **case-insensitive**. **`path.win32`/`path.posix`;Windows 大小写不敏感。**
- **Real home resolution** (`detectRealHome`): win32 `%USERPROFILE%`→`%HOMEDRIVE%`+`%HOMEPATH%`; `os.userInfo().homedir`; filter `/.gemini/`; `$HOME`/`os.homedir()`. **真实家目录。**
- **Path expansion** (`expandUserPath`): `~`, `~/`, `~\`, **Windows `%VAR%`** (`%USERPROFILE%`→home; unknown left intact), **`$HOME`/`${HOME}`**, `resolve()`. **`~`/`%VAR%`/`$HOME`。**
- **OS-aware registry targets** + **Node binary resolution**: macOS `/opt/homebrew/bin/node`→`/usr/local/bin/node`→`process.execPath`; Windows `C:\Program Files[ (x86)]\nodejs\node.exe`. **平台化注册表与 Node 二进制。**
- **No external tools**; line-ending/shell agnostic (`\n`/`\r\n`; `zsh`/`bash`; **Windows PowerShell**/`cmd`/`pwsh`); UTF-8. **不调用外部命令;换行/终端无关。**
### 4KB 响应帧预算防客户端卡顿 / 4KB-class response-frame budget
Keeps every tool result small so a local client never freezes on a huge frame. **保持每条结果很小,避免本地客户端在超大帧上卡死。**
- **`--max-chars <num>`**: per-call cap on returned content, **default `4000` (≈4KB)**, **min `500`, max `16000`**. **默认 4000(约 4KB)。**
- `fs_read` `content` capped at `maxChars`; **middle-truncation `…[truncated N chars; pass startLine/endLine…]…`** head+tail; **1-indexed `startLine`/`endLine`**; `totalChars`/`totalLines`. **头尾保留+中间截断+行切片。**
- **Independent hard caps**: `fs_list` `limit`≤200(def)/500; `fs_search`≤60,≤2000 files,depth≤8,skip>2MB,skip binary,`preview`≤160; `fs_tree`≤250,depth≤6,prune heavy/sensitive dirs; `SKIP_DIRS`. **额外硬上限。**
- **Binary protection**: NUL probe first 4096 bytes; binary `fs_read`→`{isBinary:true,…}` no raw bytes. **二进制保护。**
- **Honest scope note**: 4KB default applies to returned **text content** + per-result caps; this package does **not** currently re-serialize/truncate the **entire** JSON-RPC frame to a strict 4096 bytes for `fs_list`/`fs_search`/`fs_tree`. Pass `--max-chars 4000` for tightest default. **诚实说明:4KB 针对返回文本 + 条数上限;非整帧 4096 字节总顶。**
- Valid JSON-RPC; oversized fields truncated with explicit markers, never silently.
### stdio 模式 vs 本机独立 HTTP MCP 模式的权限差异 / `stdio` vs standalone local HTTP MCP (desktop clients)
**本仓 `gemini-local-bridge-mcp` 只发 `stdio`**;下面是**桌面客户端下两种模式的权限模型**(独立 HTTP MCP 由你用外层守护进程包装同一 `src/server.mjs`,或参考同作者 `mcp-4f3a2b` / `omni-mac --http`,见 §6)。This package ships **stdio only**; comparison is the **permission model**.
| | **stdio 模式 / stdio** | **本机独立 HTTP MCP / standalone local HTTP MCP** |
|---|---|---|
| 谁启动 / who spawns | **客户端** spawn (`command`+`args`). | **独立进程**(macOS **user LaunchAgent** `~/Library/LaunchAgents` `RunAtLoad/KeepAlive`;Windows **Task Scheduler/service**). Client doesn't spawn. |
| 继承沙盒 / inherits sandbox | **继承宿主客户端沙盒**. On macOS, Gemini App / Claude built-in `run_command` under **Seatbelt**; stdio child isn't automatically unsandboxed. Reading `~/.config/gh`, `~/video`, `~/Library/…`, writing `~/Library/Application Support` can hit **`EPERM`**. | **不继承客户端沙盒**. Own process + OS identity; client only HTTP to `127.0.0.1`. No `EPERM` from *client* sandbox. (Your own OS ACLs/TCC still apply.) |
| 继承环境 / inherits env | Inherits client `PATH`/`HOME`/env/cwd. GUI apps minimal `PATH`→`node` `127`/`126`. | **自管环境**; absolute `node`; `launchd`/PowerShell env; `127.0.0.1`. |
| 权限来源 / source | client sandbox + user + app TCC + spawned-env. | daemon user token + own env + **app-level `--root`/guardrails still apply** + bind `127.0.0.1`. |
| 典型故障 / typical | `EPERM`, `exit 126/127`, relative `command`, client-only folders. | port conflict; needs `launchctl`/scheduled task; **bind localhost + DNS-rebinding**; no app sandbox but no extra auth by default. |
| 配置 / config | `mcpServers{command,args}`. | register **URL** `http://127.0.0.1:PORT/mcp`; daemon separate. |
| 生命周期 / lifecycle | starts/stops with client. | independent; `KeepAlive`; survives client restart. |
**关键结论 / Bottom line.** On **sandboxed desktop clients** (e.g. **macOS Gemini App**, built-in command under **Seatbelt**; sandboxed `run_command` can't read `~/.config/gh`, `~/video`, `~/Library/...` or write `~/Library/Application Support` → **`EPERM`**), **stdio children inherit that sandbox**, but a **standalone local HTTP MCP started outside the app** runs **unsandboxed as your user** (`mcp-4f3a2b` `127.0.0.1:3846` PPID 1; `omni-mac --http 3847` + `~/Library/LaunchAgents/cc.omni-mac.http.plist` `RunAtLoad`/`KeepAlive` no Seatbelt → `http://127.0.0.1:3847/mcp`, **no `EPERM`**). **沙盒客户端下 stdio 继承沙盒;独立本机 HTTP MCP 无沙盒。**
- **But** `gemini-local-bridge-mcp` **itself only implements `stdio`** and already enforces **app-level safety** regardless of transport: `--read-only`, `--root`, sensitive-path blocks, `isWithinAllowedRoots`. **OS-sandbox differences don't remove these checks.** **本仓只 stdio;自带安全两种模式都生效。**
- **Windows PowerShell**: stdio child inherits current PowerShell **logon token**, `%PATH%`, `%USERPROFILE%`/`%APPDATA%`, ACLs; `command`=absolute `C:\Program Files\nodejs\node.exe`. Standalone HTTP = own scheduled-task/service identity (or `Start-Job`), `127.0.0.1`, own env; PowerShell transient `$env` doesn't leak in; `resolveNodeBinary` checks `C:\Program Files[ (x86)]\nodejs`. **Windows 子进程继承令牌/环境/ACL;独立 HTTP 自身身份 + 127.0.0.1。**
- **Only bind `127.0.0.1`** + validate `Host` (DNS-rebinding); never `0.0.0.0`. **仅 127.0.0.1 + Host 校验。**
---
## 2. 快速开始 / Quick start
### 2.1 macOS
```bash
node --version # v18+ (Homebrew: /opt/homebrew/bin/node); no `npm install`
git clone https://github.com/37chengshan/gemini-local-bridge-mcp.git
cd gemini-local-bridge-mcp
node /Users/cc/code/gemini-local-bridge-mcp/src/server.mjs # read/write stdio
node /Users/cc/code/gemini-local-bridge-mcp/src/server.mjs --read-only # read-only
node /Users/cc/code/gemini-local-bridge-mcp/src/server.mjs --read-only --root ~/Projects --max-chars 4000
```
### 2.2 Windows PowerShell
```powershell
node --version # v18+; no `npm install`
Get-Command node | Select-Object -ExpandProperty Source # e.g. C:\Program Files\nodejs\node.exe
git clone https://github.com/37chengshan/gemini-local-bridge-mcp.git
Set-Location .\gemini-local-bridge-mcp
& "C:\Program Files\nodejs\node.exe" "C:\Users\$env:USERNAME\code\gemini-local-bridge-mcp\src\server.mjs" # read/write
node .\src\server.mjs # if `node` on PATH
node .\src\server.mjs --read-only --root "$env:USERPROFILE\Projects" --max-chars 4000 # read-only + scoped + 4KB
```
- **macOS + Windows PowerShell**; **Linux** too. No `.sh`. Windows `%USERPROFILE%`/`%APPDATA%`, `~\`, `%VAR%`; **absolute `node.exe`**; JSON `\\`. `node install.mjs …` in PowerShell too. **双平台。**
---
## 3. CLI
```text
node src/server.mjs [--read-only] [--root <dir>]... [--max-chars <num>]
```
- `--read-only`; `--root` (repeatable; `~`/`%VAR%`/`$HOME`; outside→`isError`); `--max-chars` (default **4000 ~4KB**, min500,max16000).
- **stdio dual framing** NDJSON + `Content-Length` auto; `initialize`→`protocolVersion params.protocolVersion || 2025-06-18`, `capabilities{tools:{}}`, `serverInfo gemini-local-bridge-mcp/0.1.0`; `ping`,`tools/list`,`tools/call`,`resources/list`,`prompts/list`; notifications no reply; tool errors→`isError:true`. **stdio 双帧;标准 MCP。**
---
## 4. 工具 / Tools
Read tools `readOnlyHint:true,destructiveHint:false,idempotentHint:true,openWorldHint:false`; write `readOnlyHint:false,destructiveHint:false,idempotentHint:false,openWorldHint:false`. `--read-only` removes write. `isError` not throw. **只读/写注解。**
| Tool | 说明 / description | key params |
|---|---|---|
| `fs_read` | UTF-8 read; line slice; `maxChars`; binary; ~4KB. | `filePath`,`startLine`,`endLine`,`maxChars` |
| `fs_list` | dir list (name/type/size/mtime; sensitive filtered). | `dirPath`,`limit`≤500 |
| `fs_tree` | compact recursive tree; skip heavy/sensitive; caps. | `dirPath`,`maxDepth`≤6 |
| `fs_search` | pure-Node recursive substring/regex; caps. | `dirPath`,`query`,`isRegex`,`caseSensitive`,`filePattern` |
| `fs_stat` | exists/type/bytes/lineCount/mtime + host `{platform,arch,homeDir,readOnlyMode}`. | `filePath` |
| `fs_write` *(write)* | create/overwrite UTF-8; `mkdir -p`. | `filePath`,`content` |
| `fs_replace` *(write)* | replace exact substring. | `filePath`,`target`,`replacement` |
Paths `~`/`~/`/`~\`/`%VAR%`/`$HOME`/`${HOME}` per-platform; `darwin`/`win32`/`linux`.
---
## 5. 客户端配置 / Client `mcpServers`
### macOS
```json
{ "mcpServers": { "local-bridge": { "command": "/opt/homebrew/bin/node",
"args": ["/Users/cc/code/gemini-local-bridge-mcp/src/server.mjs","--read-only","--max-chars","4000"] } } }
```
- Gemini `~/.gemini/settings.json`,`~/.gemini/config/mcp_config.json`; Claude `~/Library/Application Support/Claude/claude_desktop_config.json`,`~/.claude.json`; Cursor `~/.cursor/mcp.json`; Windsurf `~/.codeium/windsurf/mcp_config.json`. **绝对 command。**
### Windows PowerShell
```json
{ "mcpServers": { "local-bridge": { "command": "C:\\Program Files\\nodejs\\node.exe",
"args": ["C:\\Users\\cc\\code\\gemini-local-bridge-mcp\\src\\server.mjs","--read-only","--max-chars","4000"] } } }
```
- `%USERPROFILE%\.gemini\settings.json`,`%APPDATA%\Claude\claude_desktop_config.json`,`%USERPROFILE%\.claude.json`,`%APPDATA%\Gemini\mcp_config.json`. JSON `\\`. **绝对 node.exe。**
### stdio vs standalone HTTP
This package registers **stdio**. Standalone local HTTP would register **URL** `http://127.0.0.1:PORT/mcp` + independent daemon — **permission differences in §1**. `gemini-local-bridge-mcp` does **not** open HTTP itself. **本仓 stdio;独立 HTTP 外层守护,权限见 §1。**
---
## 6. 安装器 / Installer + doctor
```bash
node install.mjs --doctor
node install.mjs --dry-run [--read-only] [--root ~/Projects]
node install.mjs --install [--host gemini|claude|cursor|windsurf|all] [--read-only] [--root …]
```
```powershell
node install.mjs --doctor
node install.mjs --dry-run --read-only --root "$env:USERPROFILE\Projects"
node install.mjs --install --host gemini --read-only
```
Registers `mcpServers["local-bridge"]={command:<absolute node>,args:[src/server.mjs,…]}`; backup `.bak-localbridge-<ts>`; `--dry-run` no-write; `npm run doctor`/`npm run install:dry`; `npm test`. **注册+备份+体检。**
### 对照独立本机 HTTP MCP(权限)/ contrast
`mcp-4f3a2b`=standalone `node server.mjs` `127.0.0.1:3846`, no sandbox, `{url:http://127.0.0.1:3846/mcp}`; **`omni-mac --http 3847`** + `~/Library/LaunchAgents/cc.omni-mac.http.plist`(`RunAtLoad`/`KeepAlive`,no Seatbelt)→`http://127.0.0.1:3847/mcp` full-host **no `EPERM`**. Windows=scheduled task/service `127.0.0.1`. **不改 CoreData `.store`;不动 `mcp-4f3a2b`。** See §1.
---
## 7. 安全 / Security
`--read-only`; `--root` (`isWithinAllowedRoots`,win32 case-insensitive); per-target; applied to read/write/list/tree/search. **Sensitive-path guardrails** (`isSensitivePath`,case-insensitive; `\`→`/`; children too): `.ssh`,`.aws`,`.gnupg`,`.kube/config`,`.npmrc`,`.pypirc`,`.netrc`; **macOS** `~/Library/Keychains`,Chrome `Default/Cookies`; **Windows** `AppData\Roaming\Microsoft\Credentials`,`AppData\Local\Microsoft\Credentials`,`…\Chrome\User Data\Default\Network\Cookies`,`C:\Windows\System32\config\SAM`,`NTUSER.DAT`. Binary protection; no secret output; no network. **App-level independent of OS sandbox; HTTP→127.0.0.1 + DNS-rebinding + own auth.** **按平台灵敏度拦截;应用层独立安全。**
---
## 8. 测试 / Tests
```bash
/opt/homebrew/bin/node tests/test-all.mjs # user-specified
node tests/test-all.mjs # node>=18 on PATH
npm test # == node ./tests/test-all.mjs
```
```powershell
node tests\test-all.mjs
npm test
```
Repo root; zero dev deps. 6 suites→`ALL 6 CROSS-PLATFORM TEST SUITES PASSED.` **测试全过。** Verified: `/opt/homebrew/bin/node tests/test-all.mjs` → 6 PASS.
---
## 9. 范围 / Scope
Local file+search **stdio** MCP; app-level safety; ~4KB. **No OS sandbox; no HTTP; no network; no `npm install`; no secrets.** OS sandboxing = host concern; standalone HTTP changes spawn/sandbox inheritance (§1) not internal guardrails. **不提供 OS 沙盒/自带 HTTP/联网;不改变内部安全。**
---
MIT License © 2026 Contributors.
This server cannot be deployed
Maintenance
ActivityMaintained
ResponsivenessOutdated data