server-instructions.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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
  20. in the workspace. Reads are sub-millisecond; the index lags writes by
  21. about a second through the file watcher. Consult it BEFORE writing or
  22. editing code, not during.
  23. ## Answer directly — don't delegate exploration
  24. For "how does X work", architecture, trace, or where-is-X questions,
  25. answer DIRECTLY using 2-3 codegraph calls: \`codegraph_context\` first,
  26. then ONE \`codegraph_explore\` for the source of the symbols it surfaces.
  27. Codegraph IS the pre-built search index — so delegating the lookup to a
  28. separate file-reading sub-task/agent, or running your own grep + read
  29. loop, repeats work codegraph already did and costs more for the same
  30. answer. Reach for raw Read/Grep only to confirm a specific detail
  31. codegraph didn't cover. A direct codegraph answer is typically a handful
  32. of calls; a grep/read exploration is dozens.
  33. ## Tool selection by intent
  34. - **"What is the symbol named X?"** → \`codegraph_search\`
  35. - **"What's the deal with this task / feature / area?"** → \`codegraph_context\` (PRIMARY — composes search + node + callers + callees in one call)
  36. - **"How does X reach/become Y? / trace the flow / the path from X to Y"** → \`codegraph_trace\` (ONE call returns the whole call path, including dynamic-dispatch hops — callbacks, React re-render, JSX children — that grep can't follow)
  37. - **"What calls this?"** → \`codegraph_callers\`
  38. - **"What does this call?"** → \`codegraph_callees\`
  39. - **"What would changing this break?"** → \`codegraph_impact\`
  40. - **"Show me this symbol's source / signature / docstring."** → \`codegraph_node\`
  41. - **"Show me several related symbols' source / survey an area."** → \`codegraph_explore\` (ONE capped call; prefer over many codegraph_node/Read)
  42. - **"What's in directory X?"** → \`codegraph_files\`
  43. - **"Is the index ready / what's its size?"** → \`codegraph_status\`
  44. ## Common chains
  45. - **Flow / "how does X reach Y"**: \`codegraph_trace\` from→to FIRST — one call returns the entire path with dynamic-dispatch hops bridged. Then ONE \`codegraph_explore\` for the hop bodies if you need them. Do NOT reconstruct the path with \`codegraph_search\` + \`codegraph_callers\` — that's exactly what trace does in a single call.
  46. - **Onboarding**: \`codegraph_context\` first. If still unclear, \`codegraph_explore\` for breadth, then \`codegraph_node\` on specific symbols.
  47. - **Refactor planning**: \`codegraph_search\` → \`codegraph_callers\` → \`codegraph_impact\`. The blast-radius answer comes from impact, not from walking callers manually.
  48. - **Debugging a regression**: \`codegraph_callers\` of the suspected symbol; widen with \`codegraph_impact\` if an unexpected call appears.
  49. ## Anti-patterns
  50. - **Don't grep first** when looking up a symbol by name — \`codegraph_search\` is faster and returns kind + location + signature.
  51. - **Don't chain \`codegraph_search\` + \`codegraph_node\`** when you just want context — \`codegraph_context\` is one round-trip.
  52. - **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.
  53. - **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".
  54. ## Limitations
  55. - Index lags file writes by ~1 second.
  56. - Cross-file resolution is best-effort name matching; ambiguous calls may return multiple candidates.
  57. - 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.
  58. `;