Skip to content

Connecting CodeNavi to OpenCode

OpenCode is an open-source AI coding agent (TUI, desktop app, and IDE extension) with native MCP support. Point it at CodeNavi's HTTP endpoint and the agent gets what OpenCode itself doesn't ship: semantic_search over the semantic index and the call graph (find_callers, impact_analysis, diff_impact). OpenCode's own grep and file reads cover exact strings; CodeNavi's grep_search and read_file are a reveal_tools call away, or listed in protected mode.

Top-level key is mcp, not mcpServers. OpenCode nests servers under "mcp" (OpenCode 2.x: "mcp": {"servers": {…}}), and HTTP servers use "type": "remote". A block copied from a Cursor/Cline config silently does nothing.

Fast path

On a machine that is not the server (a Mac or a laptop that reaches CodeNavi through a tunnel or the LAN), there is no install directory: go to OpenCode 2.x, the opencode mcp add command works anywhere, then set "codemode": false as shown there. On the server itself, from your install directory:

./codenavi connect opencode          # --host <addr> if the server is on another machine

It checks the server and the token, then writes the codenavi entry below into ~/.config/opencode/opencode.json. It writes the 2.x layout with "codemode": false, or the 1.x layout on OpenCode 1.x. The file gets a {env:CODENAVI_AUTH_TOKEN} placeholder, never the token, and a backup is saved next to it. It prints the export line and, on 2.x, reminds you to run opencode service restart. It also writes the CodeNavi usage rules into ~/.config/opencode/AGENTS.md (see Teach the agent; --no-rules skips it), and three slash commands (--no-commands skips them). A config file with comments (JSONC) is left alone: add the entry by hand as described below.

The endpoint

  • URL: http://localhost:8765/mcp (unscoped) or /mcp/<codebase> (scoped)
  • Header: Authorization: Bearer <CODENAVI_AUTH_TOKEN>
  • Get the token from your install's .env: grep '^CODENAVI_AUTH_TOKEN=' .env | cut -d= -f2- (run from your install directory)

Configure it

OpenCode reads opencode.json from the project root (safe to commit; overrides global) or ~/.config/opencode/opencode.json (user-global). JSONC is accepted, and the files merge, later sources win on conflicting keys.

OpenCode 2.x

One command writes the entry (drop --global to write the project file):

export CODENAVI_AUTH_TOKEN=…            # from your install's .env
opencode mcp add codenavi --global --url http://localhost:8765/mcp \
  --header 'Authorization=Bearer {env:CODENAVI_AUTH_TOKEN}'
opencode service restart                # the background service must see the token

Then add "codemode": false to the entry, so the result reads:

{
  "mcp": {
    "servers": {
      "codenavi": {
        "type": "remote",
        "url": "http://localhost:8765/mcp",
        "headers": { "Authorization": "Bearer {env:CODENAVI_AUTH_TOKEN}" },
        "codemode": false
      }
    }
  }
}
  • "codemode": false: by default OpenCode 2.x hides MCP tools behind its execute tool: the model has to write JavaScript that calls tools.codenavi.semantic_search(…). Large hosted models manage that; small local models don't, and never call a CodeNavi tool. With false the tools are offered directly (codenavi_semantic_search, …). The key only works in this mcp.servers layout, the 1.x layout below drops it.
  • {env:CODENAVI_AUTH_TOKEN} is read by OpenCode's background service, not by the shell you type in. A service started before you exported the token sends an empty header and gets 401s. Run opencode service restart from a shell with the token exported (or opencode run --standalone, which starts a private server from your shell). Or paste the token literally in a file you don't commit.

OpenCode 1.x

{
  "$schema": "https://opencode.ai/config.json",
  "mcp": {
    "codenavi": {
      "type": "remote",
      "url": "http://localhost:8765/mcp",
      "enabled": true,
      "headers": { "Authorization": "Bearer <PASTE_TOKEN>" }
    }
  }
}

OpenCode 2.x still reads this layout and connects, but ignores any codemode key in it, move to the 2.x layout when you use a small local model.

If your server runs with auth turned off, drop the headers line, just the url is enough.

Committing the project file? Don't commit the literal token. OpenCode expands {env:VAR} inside header values:

"headers": { "Authorization": "Bearer {env:CODENAVI_AUTH_TOKEN}" }

then export CODENAVI_AUTH_TOKEN=… in your shell before launching opencode (2.x: then opencode service restart).

Teach the agent to prefer CodeNavi

OpenCode reads rules from AGENTS.md (project root, walking up, or global ~/.config/opencode/AGENTS.md). ./codenavi connect opencode writes them for you. By hand: paste the Agent instructions block from the /connect page into it, that block is what makes the agent reach for semantic_search before grep and respect CodeNavi's one-search-at-a-time / isolation rules. Already keeping those rules in CLAUDE.md for Claude Code? OpenCode falls back to ~/.claude/CLAUDE.md automatically, so they get picked up as-is.

Slash commands

./codenavi connect opencode also writes three commands into ~/.config/opencode/commands/codenavi/. They are the OpenCode versions of the Claude Code plugin's commands of the same name:

Command What it does
/codenavi/review [git-diff-args] Blast-radius review of your diff: diff_impact, then reads the callers it flags and reports breakages, duplicates and a merge / hold verdict. No argument = the working tree; --staged or main...HEAD pass through to git diff.
/codenavi/overview [codebase] Structural overview: entry points, key modules, languages.
/codenavi/search <query> [--codebase NAME] semantic_search with limit 5.

They live in their own codenavi/ folder, so they never replace a command of yours. A re-run of connect rewrites them. To remove them, delete that folder.

connect writes them on the machine where it runs. If OpenCode runs on another machine (a laptop that reaches the server over the LAN), run connect on the server and copy ~/.config/opencode/commands/codenavi/ to the laptop's ~/.config/opencode/commands/.

The compliance check (/codenavi:compliance in Claude Code) is not included: it needs the plugin's rule packs.

Verify

From the project directory run:

opencode mcp list

codenavi shows as ✓ connected. On 2.x it shows ○ pending while it connects. Right after opencode service restart, 2.4 shows No MCP servers configured for a few seconds instead: run it again. Inside the agent, the tools appear prefixed with the server name (codenavi_semantic_search, codenavi_list_codebases, …). Then try:

"Use semantic_search to find where payment processing happens."

Headless (opencode run, 2.x). Two things differ from the TUI:

  • Pass --auto. Without it every MCP call waits for an approval nobody can give, and the run hangs until it times out. --auto approves every tool that is not explicitly denied, including shell and edit, run it in a directory you don't mind the agent changing.
  • The first turn can go out before the MCP connection for a new directory is up, so the model sees no CodeNavi tools on turn one. In a script, wait until opencode mcp list shows ✓ codenavi (it also starts the connection), then run:
for i in $(seq 30); do opencode mcp list | grep -q '✓ codenavi' && break; sleep 1; done
opencode run --auto "Use semantic_search to find where routes are registered."

The things that bite people

  1. Top-level key is mcp, not mcpServers, and the server type is "remote", not "streamable-http". Wrong key = silent no-op.
  2. 2.x with a small local model never calls CodeNavi. Code mode is on, set "codemode": false (2.x layout only, see above).
  3. 2.x 401s although the token is exported. The background service started without it, opencode service restart from that shell.
  4. No Authorization header = OAuth attempt. For a remote server with no configured header, OpenCode auto-starts the MCP OAuth flow (Dynamic Client Registration). CodeNavi uses a static bearer token, not OAuth, when auth is on, always include the header. (With auth off, CodeNavi never challenges, so plain url is fine.)
  5. Unscoped vs scoped /mcp. Use unscoped /mcp unless you know the codebase name. /mcp/<name> returns Unknown codebase (404) for any <name> that isn't configured; a single-codebase install is usually named default. Check with list_codebases.
  6. "MCP servers add to your context." OpenCode's own docs warn about context-heavy MCP servers. CodeNavi ships a short default tool list precisely for this, the default surface is small, and you can scope the endpoint per-codebase to keep tool lists tight.
  7. Local thinking models are unreliable in opencode run. On OpenCode 2.0.29 with a local Qwen 27B, the same prompt sometimes worked, sometimes stalled after a step until the timeout, and once the model said it had no CodeNavi tools while opencode mcp list showed ✓ codenavi. The CodeNavi calls that were made all succeeded. Add --format json to see each step and tool call as it happens. Copilot CLI ran the same models without this.

Privacy

OpenCode sends your prompts, and therefore the code chunks CodeNavi returns, to whichever model you've configured. Point it at a local provider (Ollama / LM Studio) for a fully on-box loop; OpenCode is local-model capable, and the CodeNavi server itself never phones home. Scope CodeNavi to codebases you're comfortable sharing with whatever model the agent uses.