Codex Gemini Delegator V2
An MCP server that lets OpenAI Codex delegate tasks to Google Gemini agents as isolated background workers, with Git worktree isolation, loop detection, and security boundary enforcement.
README
Codex Gemini Delegator V2
A robust, production-ready MCP (Model Context Protocol) server that allows OpenAI Codex to supervise and delegate tasks to Google Gemini (Antigravity) workers as subordinate agents.
Unlike other bridges, this delegator treats Gemini workers as resilient background jobs. It orchestrates isolated Git worktrees, enforces strict security boundaries, monitors stream-json for infinite loop failures, and auto-nudges stuck workers to unblock them.
Key Features
- True Git Worktree Isolation: When Codex delegates a task, the delegator automatically spawns an invisible Git worktree (
.worktrees/wt-<uuid>). Gemini executes its code changes strictly inside this isolated environment. Your main branch remains completely untouched until Codex explicitly applies the run. - First-Party API Alignment: This MCP server exposes the exact tool names and signatures expected by mainstream Codex delegators (
delegate_to_agent,apply_agent_run,get_agent_run_report, etc.). - Loop Detection & Auto-Nudging: Built-in state heuristics actively monitor the
stream-jsonoutput of the Gemini worker. If the worker gets stuck in an infinite tool failure loop or tries the exact same broken command 3 times in a row, the Supervisor forcefully restarts it with a recursive--session-idand a strict system nudge to change strategies. - Symlink-Resistant Security Boundaries: Subordinate workers are hardened against path traversal and symlink vulnerabilities. A custom hook (
enforce_boundaries.py) prevents them from executing destructive commands (rm -rf,git reset) and strictly jails their write access to their delegated scope. - SQLite WAL Persistence: All runs, PIDs, and logs are tracked in a lightning-fast SQLite WAL ledger. If you close your Codex session, the Gemini workers keep running in the background and their state is fully recoverable.
Prerequisites
- Google Antigravity (
geminiCLI) installed globally. - Python 3.10+
- Codex CLI (
codex)
Installation & Bootstrapping
-
Clone the repository:
git clone https://github.com/psychofanplays/gemini4codex-mcp.git cd gemini4codex -
Register the MCP Server in Codex using
uv: Add the following to your~/.codex/config.toml(or equivalent Codex MCP configuration). We highly recommend usinguvto automatically manage the environment and dependencies:[mcp.servers.gemini-delegator] command = "uv" args = [ "--directory", "/ABSOLUTE/PATH/TO/gemini4codex", "run", "-m", "server.gemini_delegator" ] -
Provide Codex with the Bootstrapping Instructions: Once connected, you must instruct Codex to read the
BOOTSTRAP.mdfile located in this repository. This file provides Codex with the explicit instructions on how to structure the Multi-Agent Hierarchy and how to leveragegemini-3.1-proandgemini-3.6-flashas subordinate agents.Example Prompt for Codex:
"I have just connected the gemini-delegator MCP server. Please read the
BOOTSTRAP.mdfile in the gemini4codex repository and apply the agent synergy patterns to your global memory."
How It Works
When Codex is connected to the MCP server, it gains access to the following tools:
list_agent_backends: Verifies the Gemini CLI and DB are healthy.list_agent_runs: Displays all active, idle, and failed background workers.delegate_to_agent: Spawns a new Gemini worker inside a new UUID Git Worktree.continue_agent_run: Sends a follow-up instruction to an existing worker.get_agent_run_report: Returns the worker's JSON logs, execution status, and importantly, the raw Git diff of what the worker changed inside its worktree.apply_agent_run: Performs a squash merge of the worker's temporary worktree into your main branch.cleanup_agent_run: Cancels a running worker and deletes its isolated worktree.
Example Workflow (From Codex's Perspective)
- Codex: "I need to refactor the database schema, but I don't want to break the app. I'll delegate this to a Gemini worker."
- Codex calls
delegate_to_agent(worker_id="db-refactor", workspace_path=".") - Delegator Server creates
.worktrees/wt-5f8a9b2cand runs thegoogle.antigravitySDK in-process inside it. - Gemini works in the background, modifying files. If it loops or gets stuck, the Supervisor auto-nudges it.
- Codex periodically calls
get_agent_run_report("db-refactor"). - Codex reads the
git diffreturned by the report. "Looks good, the tests passed." - Codex calls
apply_agent_run("db-refactor"). - Delegator Server squash merges the changes cleanly into the main branch but explicitly leaves them staged (no auto-commit), allowing Codex or the human user to review and finalize the commit.
Security
This plugin aggressively restricts the Gemini subordinate worker:
- True OS Sandboxing: The Delegator explicitly prevents subordinate workers from bypassing the Antigravity OS sandbox, meaning they are natively isolated from the filesystem at the kernel level and cannot modify files outside the delegated scope.
- Git Sandboxing: Workers are forbidden from running structural git commands (
git commit,git push,git merge) or destructive operations (rm -rf). - Profile Segregation: You can delegate a
scoutorreviewerprofile, which disables all write tools completely.
Architecture Map
The project is structured into three main layers:
gemini4codex/
├── pyproject.toml # Python packaging and dependency config
├── BOOTSTRAP.md # Essential instructions for Codex on how to use these agents
└── server/
├── gemini_delegator.py # The FastMCP Server exposing endpoints to Codex
├── supervisor.py # Async execution engine; parses stream-json & detects loops
├── worktree.py # Git worktree isolation and squash-merge orchestrator
├── security.py # Dynamic SDK hooks to jail the worker
└── ledger.py # SQLite WAL state manager for robust persistence
- Layer 1: The Codex Interface (
gemini_delegator.py) handles all incoming MCP requests. - Layer 2: The Orchestrator (
worktree.py&supervisor.py) spins up the isolated environment and runs the process asynchronously via the Antigravity SDK. - Layer 3: The Jailed Subordinate (
security.py) restricts the worker's context natively so it cannot harm the host system or primary branch.
Recommended Servers
playwright-mcp
A Model Context Protocol server that enables LLMs to interact with web pages through structured accessibility snapshots without requiring vision models or screenshots.
Magic Component Platform (MCP)
An AI-powered tool that generates modern UI components from natural language descriptions, integrating with popular IDEs to streamline UI development workflow.
Audiense Insights MCP Server
Enables interaction with Audiense Insights accounts via the Model Context Protocol, facilitating the extraction and analysis of marketing insights and audience data including demographics, behavior, and influencer engagement.
VeyraX MCP
Single MCP tool to connect all your favorite tools: Gmail, Calendar and 40 more.
graphlit-mcp-server
The Model Context Protocol (MCP) Server enables integration between MCP clients and the Graphlit service. Ingest anything from Slack to Gmail to podcast feeds, in addition to web crawling, into a Graphlit project - and then retrieve relevant contents from the MCP client.
Kagi MCP Server
An MCP server that integrates Kagi search capabilities with Claude AI, enabling Claude to perform real-time web searches when answering questions that require up-to-date information.
Neon Database
MCP server for interacting with Neon Management API and databases
E2B
Using MCP to run code via e2b.
Exa Search
A Model Context Protocol (MCP) server lets AI assistants like Claude use the Exa AI Search API for web searches. This setup allows AI models to get real-time web information in a safe and controlled way.
Qdrant Server
This repository is an example of how to create a MCP server for Qdrant, a vector search engine.