codegraph icon indicating copy to clipboard operation
codegraph copied to clipboard

Allow overriding MCP server instructions (e.g. .codegraph/instructions.md) — built-in text overstates explore/edge reliability on multi-language repos

Open monochrome3694 opened this issue 5 months ago • 5 comments

Context

Since #529 the MCP initialize instructions in mcp/server-instructions.ts are the single source of truth for how agents use the toolset — which is great, but they're a hardcoded string with no override mechanism (no config field, no env var, no per-project file). Whatever they claim, every agent in every workspace receives verbatim.

Problem: the built-in text overclaims on multi-language workspaces

We ran head-to-head comparisons (codegraph vs grep + file reads, checked against ground truth) on a large mixed workspace (~1,100 Swift files + TypeScript/React + Python; ~17k nodes after excluding vendored deps). Results:

What matched the instructions' claims:

  • codegraph_callers / codegraph_callees: excellent — accurate, skip comment mentions, name the enclosing function. Genuinely better than grep.
  • codegraph_explore with a bag of known symbol names: reliable, returned the correct call chain + verbatim source in one call.

What didn't:

  • codegraph_explore with a natural-language question: the right symbol was usually present but buried under keyword-matched noise from unrelated files/languages.
  • Cross-file edges conflate same-named symbols. Examples we hit: a Swift method named matches reported as called from an unrelated package's phonemizer code (different matches); React render/Layout edges surfacing in the relationships section of a Swift-only query. This is expected from tree-sitter name-matching (the Limitations section even hints at it), but…
  • …the instructions actively tell agents the opposite: "Trust codegraph's results — don't re-verify them with grep… They come from a full AST parse", "PRIMARY — call FIRST… most often the ONLY call you need". An agent following that takes name-collision edges at face value and builds conclusions on them. (Similar in spirit to #571 — instruction text shaping agent behavior in the wrong direction.)
  • Minor: the "no covering tests found" annotation produced false negatives for symbols that do have covering tests.

On a single-language repo the built-in text is probably fine — the issue is that one fixed string can't be right for every workspace shape.

Ask

A supported override/extension point, e.g.:

  1. .codegraph/instructions.md — if present, replace (or append to) the built-in instructions for that project. Fits the existing zero-config philosophy (like the .gitignore-based exclusion lever) and .codegraph/ is already per-project.
  2. Or a global equivalent (~/.codegraph/instructions.md), or both with project taking precedence.

Smaller alternative if an override is unwanted: soften the built-in wording — drop "don't re-verify with grep" / "full AST parse" (it's tree-sitter name-matching across files, not type-resolved), and add a caution that cross-file edges between same-named symbols need confirmation in source, especially in multi-language repos.

Workaround we use today

Patching dist/mcp/server-instructions.js in the installed package with calibrated text. Works (verified via the initialize handshake) but silently reverts on every update — exactly the kind of thing a sanctioned override file would fix. Happy to share our calibrated instruction text as a starting point if useful.

🤖 Generated with Claude Code

monochrome3694 avatar Jun 09 '26 23:06 monochrome3694

yes, this would be a great addition @colbymchenry . may i take a look on this?

goutham80808 avatar Jun 13 '26 14:06 goutham80808

Adding a second, independent motivation for an override/opt-out lever: fixed per-session context cost.

On our side (host: OpenCode; several sessions a day across a handful of repos, one of them a mid-sized Rust desktop app) the initialize instructions are injected into the system prompt of every session — including sessions that never touch code. Measured in v1.6.2: SERVER_INSTRUCTIONS is 6,833 B of template text (1,173 B for the NO_ROOT_INDEX variant), and it lands as a ~6 KB block in the host's system prompt (~1.5k tokens, every session, before any work happens).

We just spent a day trimming everything we control (AGENTS.md rules, skill and subagent descriptions) by 30-60%; this block is now the single largest contributor and there is no way to touch it. codegraph serve --help only offers --path, --mcp, --no-watch, and I found no env var either, so the only workaround is patching the versioned install (same as reported above) — which silently reverts on every upgrade.

Ask: in addition to the per-project/global override (#765's main proposal), a shortening/opt-out lever on the server side:

  • codegraph serve --mcp --instructions=full|short|none, or
  • CODEGRAPH_INSTRUCTIONS=short|none — this one also covers hosts that spawn codegraph serve --mcp from a config file with a fixed argv (OpenCode's MCP entry, for example), where adding a flag means editing the host config.

For short, the parts that actually change behaviour fit in one screen: one tool, Read-equivalent, call it before Read/Grep, do not re-verify its results with grep, honour the staleness banners, projectPath for multi-project hosts. The long Anti-patterns/Limitations prose is documentation and could live in the README instead.

Data point for none being a legitimate choice: our host already carries a two-line AGENTS.md rule ("if a .codegraph/ index exists, use codegraph_explore before grep/read"), so the tool does get used — the long text is the expensive part.

Happy to open a separate issue if you would rather keep this one about the override file, and to send the short variant as a PR.

reisi007 avatar Oct 17 '26 14:10 reisi007

+1 with a cost number. In 1.6.2, SERVER_INSTRUCTIONS is 1,071 words (6,718 characters), and Claude Code puts it into the system prompt of every session in an indexed repo. We ship codegraph to a team and already carry our own short usage rule for it (which lookup to use first, plus the blind spots), so the built-in text is mostly duplicated context for us.

The smallest form that would cover our case: an env switch next to CODEGRAPH_MCP_TOOLS, for example CODEGRAPH_MCP_INSTRUCTIONS=short|off, where short keeps a few lines ("use codegraph_explore for structural and flow questions on indexed code; results are ranked, not exhaustive"). A file override as proposed here would work too, and covers the multi-language accuracy point above.

deniskern avatar Oct 19 '26 06:10 deniskern

Another data point for the override lever, this time on routing rather than size.

In 1.6.2 the server instructions and the codegraph_explore description tell the agent to call explore first for almost everything, including "reading or editing a file/symbol you can name". In 150 Claude Code sessions on one ~2,900-file Rust/TS repo, explore ran 85 times and the CLI ran 14 times. Almost every explore query was a bag of known symbol names, and the results clustered at ~26k characters regardless of maxFiles (2–8; only maxFiles: 1 came in lower, at ~16k).

For the same named targets, codegraph node <symbol> returns the symbol's source in 0.7k–12k characters (load_aware_jobs 681 vs 24,938 for explore with maxFiles: 2; fix_markers 5,209 vs 25,067), which is 2–35x less for the same answer.

We now add a one-time hint at the first explore call that steers named lookups to node/callers. A project-level override of the instructions, or instruction text that sends named symbols to node and keeps explore for open flows, would make that workaround unnecessary.

deniskern avatar Nov 01 '26 16:11 deniskern

A narrower case that is an inconsistency rather than a preference: with CODEGRAPH_MCP_TOOLS=explore,node,callers,impact, 1.6.2 exposes four tools, but SERVER_INSTRUCTIONS still says "There is a single tool, codegraph_explore", and the explore description still says "call FIRST for almost any question". The instructions do not read the allowlist that tools.js already parses.

Data from one TypeScript + Dart monorepo (~8,400 indexed files), Claude Code with Opus, deduplicated tool calls:

  • 21 days with the default (explore only): 492 explore calls. The results sat at the output cap whatever maxFiles was (p50 26.3k characters).
  • 9 days after setting the allowlist and adding a project rule that sends named symbols to node: explore 40, node 13, callers 10, impact 0. Explore fell from about 24 calls a day to about 4, and the narrow tools did not make up the difference (about 2.5 a day), while the server instructions still described a single tool.
  • We then replaced the two strings locally. The text is chosen when the server starts: if node is in the allowlist, about 1,600 characters route named symbols to node, callers and impact to their tools, and open flows to explore with maxFiles 2–3; otherwise the upstream text (6,718 characters) is served unchanged. In 3 of 3 headless runs that asked about a named function, the first call was codegraph_node with includeCode: true; a "who calls X" question was answered in that single call. The sample is small, but the change in tool choice was immediate.

Suggested fix: build the instructions and the explore description from the active allowlist. When node/callers/impact are exposed, mention them and send named-symbol lookups to them. Keep the current text for the explore-only default.

petterip avatar Nov 05 '26 22:11 petterip