Skip to content

Connect Gemini Web to Local Codebase via Serena MCP

Connect Gemini Web to your local filesystem and LSP via Serena MCP, Streamable HTTP, and Cloudflare Tunnel without extra API costs.

Many developers subscribe to consumer AI plans (such as Google AI Pro/Ultra) that include generous usage limits in Gemini Web, yet those quotas often go unused before each reset cycle. Meanwhile, running terminal-based coding agents (such as Claude Code or OpenCode) requires either a separate subscription or pay-as-you-go API billing that escalates rapidly on medium-to-large codebases.

The solution is not installing unofficial browser plugins to hijack web sessions, but rather leveraging Gemini Web’s native Custom Connectors (Model Context Protocol / MCP) support to communicate directly with your local machine.

Before diving into setup, one architectural misconception needs to be cleared up: you do not connect Gemini Web to your code editor (VS Code, Zed, or Neovim). A code editor acts as an MCP Client, not an MCP Server—two clients cannot connect to each other directly. Instead, you connect Gemini Web to your local filesystem and Language Server Protocol (LSP) through an MCP Server. Your code editor simply opens the same project directory and relies on file auto-reload whenever files change on disk.

For this MCP Server role, Serena (oraios/serena) outperforms brute-force text-reading MCP servers for three fundamental reasons:

  1. Up to 10x Context Efficiency via LSP/AST: Web chat interfaces lack the aggressive automatic context compaction found in CLI agents. Repeatedly reading entire files fills up the context window quickly and degrades model reasoning. Serena exposes AST/LSP symbol tools (get_symbols_overview, find_symbol, replace_symbol_body), allowing Gemini to inspect and edit only the specific classes or functions needed.
  2. Native Streamable HTTP Support: Serena supports --transport streamable-http out of the box. No external stdio-to-HTTP wrapper or proxy is required.
  3. Persistent Project Memories (.serena/memories/): Architecture summaries, coding conventions, and technical decisions are stored locally inside the project folder. When you start a fresh Gemini Web chat session, Gemini reads those memories immediately without needing repeated explanations.

System Architecture

Traffic flows from Google’s servers to your local loopback interface through an encrypted Cloudflare tunnel, without opening any public ports on your router or firewall:

flowchart TD subgraph Cloud["Cloud & Edge"] GW["Gemini Web<br/>(Custom Connectors)"] CF["Cloudflare Edge<br/>(~100s HTTP timeout limit)"] end subgraph Local["Local Machine (Host)"] CFD["cloudflared (Container)<br/>(network_mode: host)"] SRN["Serena MCP Server (Python)<br/>(Bind: 127.0.0.1:8001)"] PROJ["Local Project Folder<br/>(/absolute/path/to/project)"] IDE["Code Editor<br/>(VS Code / Zed / Nvim)"] end GW -->|"HTTPS (Streamable HTTP / POST & SSE)"| CF CF -->|"Encrypted Tunnel"| CFD CFD -->|"HTTP -> Host: localhost:8001"| SRN SRN -->|"AST / LSP Queries & File I/O"| PROJ PROJ <==>|"Auto-Reload<br/>(Disk File Sync)"| IDE

Using network_mode: host on the cloudflared container lets the tunnel daemon reach 127.0.0.1:8001 directly on the host, while the Serena process remains isolated strictly on the local loopback interface.


System Prerequisites & LSP Installation

This runbook uses Fedora Linux as the primary reference environment, with notes for other Linux distributions.

1. Install and Verify uv

Serena is managed and executed using Astral’s uv. Install uv via Fedora’s official repositories or the universal installer script:

# Fedora Linux
sudo dnf install uv

# Other Linux distributions (Ubuntu/Debian/Arch/etc.)
curl -LsSf https://astral.sh/uv/install.sh | sh

# Verify installation
uv --version

2. Install Language Server (LSP) Binaries

Serena’s symbolic navigation depends on the LSP binaries for each programming language being available in your system $PATH. Install the language servers matching your project stack:

# Go (gopls)
sudo dnf install golang-x-tools-gopls
# Or via Go toolchain (all Linux distros):
go install golang.org/x/tools/gopls@latest

# TypeScript / JavaScript (typescript-language-server)
npm install -g typescript typescript-language-server

# Python (pyright)
npm install -g pyright

# PHP (intelephense)
npm install -g intelephense

Ensure binary directories such as ~/.local/bin, ~/go/bin, and your global npm bin folder are present in your shell’s $PATH.

3. Configure Your Code Editor (Preventing Dirty Buffer Conflicts)

To ensure edits written by Serena appear immediately in your editor without overwrite prompts:

  • Enable Auto-Reload: Make sure your editor automatically reloads external disk changes (enabled by default in VS Code and Zed; in Neovim enable set autoread with FocusGained / BufEnter autocmds).
  • Avoid Dirty Buffers: Save manual edits (Ctrl+S) before asking Gemini to modify the same file. If a tab has unsaved changes (a dirty buffer), editors pause auto-reload to prevent data loss.

Cloudflare Tunnel Setup (One-Time)

1. Run the cloudflared Container (Docker Compose)

Create a dedicated directory for the tunnel service (for example ~/app-service/cloudflare-tunnel) and prepare two files so the container can reach 127.0.0.1:8001 on the host via network_mode: host:

compose.yml

services:
  cloudflared:
    image: cloudflare/cloudflared:latest
    network_mode: host
    container_name: cloudflared
    restart: unless-stopped
    command: tunnel --no-autoupdate run
    environment:
      - TUNNEL_TOKEN=${TUNNEL_TOKEN}

.env (populate with your tunnel token from the Cloudflare dashboard):

TUNNEL_TOKEN=eyJhIjoi...<your-cloudflare-tunnel-token>

Start the container in detached mode:

docker compose up -d

(Remember to add .env to .gitignore if this directory is tracked in Git).

2. Configure Public Hostname & Host Header

  1. Open Cloudflare Zero Trust Dashboard -> Networks -> Tunnels.
  2. Select your active tunnel, click Configure, open the Public Hostname tab, and click Add a public hostname.
  3. Fill in the routing fields:
    • Subdomain: serena-mcp
    • Domain: yourdomain .com
    • Service Type: HTTP
    • URL: localhost:8001
  4. Expand Additional application settings -> HTTP Settings, and set:
    • HTTP Host Header: localhost:8001

Note also that Cloudflare Edge enforces an ~100-second HTTP timeout on standard plans. Any MCP tool call that blocks longer than 100 seconds will be terminated by Cloudflare.


Installing & Running Serena MCP

1. Persistent Installation via uv

Install serena-agent globally as an isolated CLI tool:

uv tool install --from git+https://github.com/oraios/serena serena-agent

Verify that serena --help runs cleanly from your terminal.

2. Initialize Your Project

Navigate to your target project directory and generate Serena’s project configuration:

cd /absolute/path/to/project
serena project create

This creates a .serena/ folder containing language settings and project memories. Add .serena/ to .gitignore so local session metadata stays out of version control:

echo ".serena/" >> .gitignore

3. Start the MCP Server

Launch Serena in Streamable HTTP mode bound strictly to localhost port 8001:

serena start-mcp-server   --transport streamable-http   --host 127.0.0.1   --port 8001   --project /absolute/path/to/project

Endpoint Verification & Connecting Gemini Web

1. Verify the Public Endpoint

Before opening Gemini Web, verify external connectivity with curl:

curl -i https://serena-mcp.yourdomain.com/mcp

When the tunnel and Host Header are properly configured, the server responds with:

HTTP/2 406 Not Acceptable
content-type: application/json
...

{"jsonrpc":"2.0","error":{"code":-32000,"message":"Not Acceptable: Client must accept text/event-stream"},"id":null}

An HTTP/2 406 Not Acceptable response mentioning Client must accept text/event-stream confirms that the Streamable HTTP endpoint is active and reachable through Cloudflare Edge.

2. Register in Gemini Web

  1. Open Gemini Web’s connector settings at https://gemini.google.com/customize/connectors.
  2. Add a custom connector pointing to: https://serena-mcp.yourdomain.com/mcp
  3. Start a new Gemini Web chat session, click the + (Tools) icon in the prompt bar, and toggle on your registered Serena connector.
  4. On the first tool invocation, Gemini Web prompts for permission. Choose Always allow for the session—waiting manually on permission prompts across multiple tool calls risks exceeding Cloudflare’s 100-second HTTP timeout.

Security Hygiene & Vibe Coding Optimization

Security Hygiene

Exposing an MCP Server capable of filesystem reads/writes and shell execution to a public URL—even on an obscure subdomain—expands your attack surface if left running indefinitely:

  • Keep It Ephemeral (On-Demand): Never run Serena MCP as a permanent background systemd service. Start it only during active coding sessions.
  • Always Bind to 127.0.0.1: Always pass --host 127.0.0.1 so the server is unreachable over your local LAN/Wi-Fi.
  • Tear Down When Finished: Once your session ends, stop Serena with Ctrl+C and disable or remove the Public Hostname in Cloudflare Tunnel.

Starter Guardrail Prompt

Paste the following prompt at the start of every new Gemini Web session to keep context lean and prevent network timeouts:

You are connected to my local codebase via the Serena MCP Server. Follow these operational rules strictly during this session:

1. **Directory Exclusions:** Completely ignore `node_modules/`, `.git/`, `vendor/`, `dist/`, `build/`, and `.serena/` unless explicitly requested. Never scan or read files inside them.
2. **Prioritize Symbolic Navigation (Save Context):** Do not read entire large files (`read_file`). Use `get_symbols_overview` and `find_symbol` first to inspect class/function structure, then read only the specific symbol bodies needed. Use `replace_symbol_body` for surgical edits.
3. **Avoid Cloudflare's 100s Timeout:** For long-running shell commands (dev servers, large test suites, full builds), run them in the background or redirect output to a log file so the command returns control well under 100 seconds.
4. **Initial Orientation:** Check for existing project memories (`list_memories`) or review the root directory layout before proposing architectural changes.

Quick Troubleshooting

Symptom / Error Root Cause Fix
HTTP/2 421 Invalid Host header on curl or Gemini connect MCP SDK rejects non-localhost Host headers due to DNS rebinding protection. In Cloudflare Tunnel Public Hostname -> Additional application settings -> HTTP Settings, set HTTP Host Header to localhost:8001.
Infinite Connecting spinner (>100s) followed by failure A tool call or manual permission prompt exceeded Cloudflare Edge’s ~100s timeout. Select Always allow on the first tool prompt; run heavy shell tasks in the background; ensure the LSP is not hanging.
Slow or missing symbol resolution Language LSP binary is missing from $PATH, or initial LSP indexing is still running. Verify which gopls / which pyright / which typescript-language-server in the same shell; wait 10–30s after startup for indexing.
Files update in Serena logs, but code editor view stays stale The file has unsaved manual edits (dirty buffer) inside your editor. Save or revert the file (Ctrl+S or Reload from Disk) and enable external file auto-reload.
502 Bad Gateway from Cloudflare cloudflared is running, but Serena is stopped or cloudflared lacks network_mode: host. Verify serena start-mcp-server is listening on 127.0.0.1:8001 and cloudflared uses network_mode: host.

With this setup, you gain full LSP-backed agentic coding directly over your local machine while utilizing the Gemini Web quota you already pay for—without extra infrastructure complexity or per-token API costs.

Quick Access & Navigation
Type any keyword to filter in real-time.