Skip to content

feat(codex): Codex uses mgrep for conceptual questions and keeps rg for exact lookups - #153

Open
F0rce wants to merge 1 commit into
mixedbread-ai:mainfrom
F0rce:feat/codex-mcp-integration
Open

F0rce wants to merge 1 commit into
mixedbread-ai:mainfrom
F0rce:feat/codex-mcp-integration

Conversation

@F0rce

@F0rce F0rce commented Oct 6, 2026

Copy link
Copy Markdown

Problem

mgrep install-codex appends a full skill file ("MANDATORY ... NEVER use grep") to the global ~/.codex/AGENTS.md. With it, Codex stops using any other way of searching. Without it, Codex ignores mgrep. The install also registers codex mcp add mgrep mgrep mcp, a server that exposes no tools, returns an invalid CallTool result and reports version 0.1.3; it only exists to start the watcher. Searches go through the shell CLI inside Codex's network sandbox, so each mgrep search asks for approval.

Expected: Codex uses mgrep where semantic search helps, keeps rg/grep for exact lookups, and searches without approval prompts.

Change

The Codex integration becomes a native Codex plugin, published from the same repository marketplace as the Claude plugin (.agents/plugins/marketplace.json, plugins/mgrep/.codex-plugin/plugin.json):

  • MCP server mgrep mcp --expose-tools with one read-only search tool. Codex starts it in the session directory and stops it at session end. It syncs that directory in the background, so no watcher outlives the session. The first search waits up to 20 s for the initial sync, then searches the existing index and says so.
  • SessionStart hook that only adds guidance: mgrep for conceptual or cross-file questions, rg/grep for exact strings, symbols and regexes. Codex asks the user to review it once.
  • Skill with a short neutral description of the tool and the CLI.

mgrep install-codex adds the marketplace and installs mgrep@Mixedbread-Grep; mgrep uninstall-codex reverses it. Both remove the legacy AGENTS.md block and MCP registration.

Changes outside src/install/codex.ts that the MCP tool needs: search logic moves to src/lib/search.ts so the CLI and the tool share it; createStore({ interactive: false }) so the server never prompts; the version comes from package.json; startWatch no longer calls process.exit and reports whether watching started, so the server can wait for it. The Claude plugin is unchanged.

Scope and approval

There is no prior issue or discussion. The direction matches the unmerged maintainer branch chore/use-mcp-for-code (MCP tool instead of shell search). Happy to open a discussion first or split differently if you prefer.

Verification

Selective use, measured with codex exec (Codex CLI 0.160.1, gpt-6.1-sol, reasoning high) in fresh CODEX_HOMEs on a private 853-file TypeScript monorepo. 10 questions: 6 conceptual (no identifiers named) and 4 exact (a symbol, a literal error string, a config key, an env var). 5 runs per question and variant, 100 runs, none failed. Correct = the answer cites at least one expected file.

Plugin without hook Plugin with hook (this PR)
mgrep used, conceptual 8/30 30/30
mgrep used, exact 0/20 0/20
Correct 50/50 50/50
Mean cost index, conceptual (median) 68.7k (60.9k) 71.2k (61.7k)
Mean cost index, exact 10.1k 9.7k

Cost index = uncached input + 0.1 × cached input + 8 × output tokens. With the hook Codex makes about one mgrep call per conceptual question, reads the returned ranges and confirms with rg; it never uses mgrep for exact lookups. Cost and correctness stay within noise. This PR does not claim mgrep makes Codex cheaper on this repository.

A single-run screen of stronger wordings: an explicit decision rule led to up to 17 mgrep calls on one question; "always start with mgrep" also used mgrep on exact lookups and doubled their cost, which is the current AGENTS.md behaviour.

Other checks:

  • test/codex.bats covers the exact codex invocations of install and uninstall, legacy AGENTS.md cleanup with surrounding content kept, idempotent reinstall, and the hook output. test/mcp.bats covers the tool schema, root resolution, waiting for the watcher, the 20 s bound and the fallback sync. 92/92 pass.
  • Installed from a local checkout into a fresh CODEX_HOME: codex mcp list shows the server and app-server hooks/list shows the hook.
  • In real codex exec sessions the MCP server starts in the session directory, mgrep.search runs without an approval prompt, and nothing keeps running after the session ends.

Not checked: interactive Codex TUI on a machine without a previous mgrep install, and Windows.

Model and harness: Claude Opus 5.5 (1M context) in Claude Code for design, review and benchmarking; GPT-6.1-Sol in Codex for implementation.

…or exact lookups

Replace the Codex integration, which appended a "MANDATORY / NEVER use
grep" skill to ~/.codex/AGENTS.md and registered an MCP server without
tools. Codex then stopped searching any other way.

The integration is now a native Codex plugin from the same repository
marketplace as the Claude plugin:

- MCP server (`mgrep mcp --expose-tools`) with one read-only `search`
  tool. It runs outside Codex's network sandbox, so searches need no
  approval prompt, and it syncs the session directory in the background
  for the lifetime of the session.
- SessionStart hook that only adds neutral guidance: mgrep for
  conceptual questions, rg/grep for exact strings and symbols.
- Short neutral skill.
- `install-codex` adds the marketplace and installs the plugin;
  `uninstall-codex` reverses it. Both clean up the legacy AGENTS.md
  block and MCP registration.

Supporting changes the MCP tool needs: search logic extracted to
src/lib/search.ts and shared by the CLI and the tool, non-interactive
store creation for the MCP server, version read from package.json, and
startWatch no longer calls process.exit and reports whether watching
started.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant