server-instructions.ts 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /**
  2. * Server-level instructions emitted in the MCP `initialize` response.
  3. *
  4. * MCP clients (Claude Code, Cursor, opencode, LangChain, OpenAI Agent
  5. * SDK, …) surface this text in the agent's system prompt automatically,
  6. * giving the agent a high-level playbook for the codegraph toolset
  7. * before it sees individual tool descriptions.
  8. *
  9. * Goals when editing this:
  10. * - Tool selection by intent (which tool for which question)
  11. * - Common chains (refactor planning = X then Y)
  12. * - Anti-patterns (don't grep when codegraph_search is faster)
  13. *
  14. * Keep it tight. The agent reads this every session — long instructions
  15. * burn tokens. Reference only tools that exist on `main`; gate any
  16. * conditional tools behind feature checks if/when they ship.
  17. */
  18. export const SERVER_INSTRUCTIONS = `# Codegraph — code intelligence over an indexed knowledge graph
  19. Codegraph is a SQLite knowledge graph of every symbol, edge, and file in
  20. the workspace — pre-computed structure you would otherwise re-derive by
  21. reading files (cached intelligence: thousands of parse/trace decisions you
  22. don't pay to re-reason each run). Reads are sub-millisecond; the index lags
  23. writes by ~1s through the file watcher. Reach for it BEFORE *and* while
  24. writing or editing code — not just for questions: one call returns the
  25. verbatim source PLUS who calls it and what it affects, so you edit with the
  26. blast radius in view. More accurate context, in far fewer tokens and
  27. round-trips than reading files yourself.
  28. ## Use codegraph instead of reading files — for questions AND edits
  29. Whether you're answering "how does X work" or implementing a change (fixing
  30. a bug, adding a feature), reach for codegraph before you Read. For
  31. understanding, answer DIRECTLY — usually with ONE \`codegraph_explore\` call.
  32. \`codegraph_explore\` takes either a natural-language question or a bag of
  33. symbol/file names and returns the verbatim source of the relevant symbols
  34. grouped by file, so it is Read-equivalent and most often the ONLY
  35. codegraph call you need. Codegraph IS the pre-built search index — so
  36. delegating the lookup to a separate file-reading sub-task/agent, or
  37. running your own grep + read loop, repeats work codegraph already did and
  38. costs more for the same answer. Reach for raw Read/Grep only to confirm a
  39. specific detail codegraph didn't cover. A direct codegraph answer is
  40. typically one to a few calls; a grep/read exploration is dozens.
  41. ## Tool selection by intent
  42. - **Almost any question — "how does X work", architecture, a bug, "what/where is X", or surveying an area** → \`codegraph_explore\` (PRIMARY — call FIRST; ONE capped call returns the verbatim source of the relevant symbols grouped by file; most often the ONLY call you need)
  43. - **"How does X reach/become Y? / the flow / the path from X to Y"** → \`codegraph_explore\`, naming the symbols that span the flow (e.g. \`mutateElement renderScene\`) — it surfaces the call path among them, including dynamic-dispatch hops (callbacks, React re-render, JSX children) grep can't follow
  44. - **"What is the symbol named X?" (just its location)** → \`codegraph_search\`
  45. - **"What calls this?" / "What does this call?" / "What would changing this break?"** → \`codegraph_callers\` / \`codegraph_callees\` / \`codegraph_impact\`. Callers includes where a function is **registered as a callback** (passed as an argument, assigned to a function pointer/field, listed in a handler table) — labeled "via callback registration" — so a function with no direct calls is NOT dead if it's wired up somewhere. When several UNRELATED symbols share a name (one \`UserService\` per monorepo app), these tools report **one section per definition** (never a merged list) — pass \`file\` to focus the definition you mean
  46. - **Reading a source FILE (any time you'd use the \`Read\` tool)** → \`codegraph_node\` with a \`file\` path and no \`symbol\`. It returns the file's **current source with line numbers — the same \`<n>\\t<line>\` shape \`Read\` gives you, safe to \`Edit\` from** — narrowable with \`offset\`/\`limit\` exactly like \`Read\`, PLUS a one-line note of which files depend on it. Same bytes as \`Read\`, faster (served from the index), with the blast radius attached. Use it **instead of \`Read\`** for indexed source files; fall back to \`Read\` only for what codegraph doesn't index (configs, docs). Pass \`symbolsOnly: true\` for just the file's structure.
  47. - **About to read or edit a symbol you can name** → \`codegraph_node\` with that \`symbol\` (SECONDARY — the after-explore depth tool): the verbatim source (\`includeCode: true\`) PLUS its caller/callee trail, so before changing it you see what calls it and what your edit would break. For an OVERLOADED name it returns EVERY matching definition's body in one call, so you never Read a file to find the right overload
  48. - **"What's in directory X?"** → \`codegraph_files\`
  49. - **"Is the index ready / what's its size?"** → \`codegraph_status\`
  50. ## Common chains
  51. - **Flow / "how does X reach Y"**: ONE \`codegraph_explore\` with the symbol names spanning the flow — it surfaces the call path among them (riding dynamic-dispatch hops) AND returns their source. No need to reconstruct the path with \`codegraph_search\` + \`codegraph_callers\`.
  52. - **Onboarding / understanding any area**: ONE \`codegraph_explore\` is usually the whole answer. Only follow up — \`codegraph_node\` for a specific symbol — if something is still unclear.
  53. - **Refactor planning**: \`codegraph_search\` → \`codegraph_callers\` → \`codegraph_impact\`. The blast-radius answer comes from impact, not from walking callers manually.
  54. - **Debugging a regression**: \`codegraph_callers\` of the suspected symbol; widen with \`codegraph_impact\` if an unexpected call appears.
  55. ## Anti-patterns
  56. - **Trust codegraph's results — don't re-verify them with grep.** They come from a full AST parse; re-checking with grep is slower, less accurate, and wastes context.
  57. - **Don't grep first** when looking up a symbol by name — \`codegraph_search\` is faster and returns kind + location + signature.
  58. - **Don't chain \`codegraph_search\` + \`codegraph_node\`** to understand an area — ONE \`codegraph_explore\` returns the relevant symbols' source together in a single round-trip.
  59. - **Don't loop \`codegraph_node\` over many symbols** — one \`codegraph_explore\` call returns them all grouped by file, while each separate call re-reads the whole context and costs far more. Use \`codegraph_node\` for a single symbol.
  60. - **Don't reach for the \`Read\` tool on an indexed source file** — \`codegraph_node\` with a \`file\` reads it for you (same \`<n>\\t<line>\` source, \`offset\`/\`limit\` like Read, faster, with its blast radius), and with a \`symbol\` it returns the source plus the caller/callee trail. Reach for raw \`Read\` only for what codegraph doesn't index (configs, docs) or when the staleness banner flags a file as pending re-index.
  61. - **After editing, check the staleness banner.** When a tool response starts with "⚠️ Some files referenced below were edited since the last index sync…", the listed files are pending re-index — Read those specific files for accurate content. Every file NOT in that banner is fresh, so still trust codegraph. \`codegraph_status\` also lists pending files under "Pending sync".
  62. ## Limitations
  63. - If a tool reports a project isn't indexed (no \`.codegraph/\`), stop calling codegraph tools for that project for the rest of the session and use your built-in tools there instead. Indexing is the user's decision — mention they can run \`codegraph init\` if it comes up, but don't run it yourself.
  64. - Index lags file writes by ~1 second.
  65. - Cross-file resolution is best-effort name matching; ambiguous calls may return multiple candidates.
  66. - No live correctness validation — that's still the TypeScript compiler / test suite / linter's job. Codegraph supplements those with structural context they don't have.
  67. `;
  68. /**
  69. * Instructions variant sent when the workspace has NO codegraph index.
  70. *
  71. * Sending the full playbook ("lean on codegraph for everything") into a
  72. * session where every call would fail wastes the agent's calls and — worse —
  73. * the failures teach it codegraph is broken. The unindexed variant is a
  74. * short, unambiguous "inactive this session" note; `tools/list` is gated to
  75. * empty in the same state, so the agent has nothing to mis-call. Indexing is
  76. * deliberately left to the user: the agent is told NOT to run init itself.
  77. */
  78. export const SERVER_INSTRUCTIONS_UNINDEXED = `# Codegraph — inactive (workspace not indexed)
  79. This workspace has no codegraph index (no \`.codegraph/\` directory), so no
  80. codegraph tools are available this session. Work with your built-in tools as
  81. usual.
  82. Indexing is the user's decision — do not run it yourself. If the user asks
  83. about codegraph, they can enable it by running \`codegraph init\` in the
  84. project root and starting a new session.
  85. `;