Skip to content

Commit 3e24361

Browse files
andreinknvclaude
andcommitted
feat(mcp): emit server-level instructions in initialize response
The MCP `initialize` response can include an `instructions` field that clients (Claude Code, Cursor, opencode, LangChain, OpenAI Agent SDK, etc.) surface in the agent's system prompt automatically. Today codegraph emits an empty initialize response — agents only see individual tool descriptions, no overall guidance on how to compose them. This adds the missing playbook: - **Tool selection by intent** — quick map from "what is X" / "how does X work" / "what would changing X break" to the right tool. - **Common chains** — onboarding (context first), PR review (review_context), refactor planning (search → callers → impact), debugging a regression. - **Tier discipline** — start at the cheap deterministic tier (search, context, callers, callees, impact, node, explore, files, status), escalate to conditional tools only when their data exists, and only reach for LLM-mediated tools when the cheap path doesn't suffice. - **Agent-bridge tier** — explicit recipe for projects without a local LLM where the agent itself summarizes via codegraph_pending_summaries + codegraph_save_summaries. - **Anti-patterns** — don't grep when search exists, don't chain search+node when context covers it, don't query the index immediately after a write. Lives in src/mcp/server-instructions.ts so it's easy to update without touching the JSON-RPC dispatch in src/mcp/index.ts. Single-file, no schema changes, no migrations, no test changes needed. References tools that exist on `main` today; doesn't presume any of the in-flight feature PRs (colbymchenry#110, colbymchenry#112-115, colbymchenry#111) have landed. After those merge, the relevant sections of this guidance start applying without needing a follow-up edit. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 7a9b997 commit 3e24361

2 files changed

Lines changed: 83 additions & 3 deletions

File tree

src/mcp/index.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import CodeGraph, { findNearestCodeGraphRoot } from '../index';
2020
import { StdioTransport, JsonRpcRequest, JsonRpcNotification, ErrorCodes } from './transport';
2121
import { ToolHandler } from './tools';
2222
import { getToolModule } from './tools/registry';
23+
import { SERVER_INSTRUCTIONS } from './server-instructions';
2324

2425
/**
2526
* Convert a file:// URI to a filesystem path.
@@ -35,8 +36,10 @@ function fileUriToPath(uri: string): string {
3536
}
3637
return path.resolve(filePath);
3738
} catch {
38-
// Fallback for non-standard URIs
39-
return uri.replace(/^file:\/\/\/?/, '');
39+
// Fallback for non-standard URIs — still resolve through path.resolve
40+
// so a malformed `file:///../etc/passwd` is normalized rather than
41+
// returned raw to downstream filesystem code.
42+
return path.resolve(uri.replace(/^file:\/\/\/?/, ''));
4043
}
4144
}
4245

@@ -269,13 +272,18 @@ export class MCPServer {
269272
// Try to initialize the default project (non-fatal if it fails)
270273
await this.tryInitializeDefault(projectPath);
271274

272-
// We accept the client's protocol version but respond with our supported version
275+
// We accept the client's protocol version but respond with our supported version.
276+
// `instructions` is a protocol-level field that MCP clients surface in the
277+
// agent's system prompt, giving the agent a high-level playbook for the
278+
// toolset before it sees individual tool descriptions. See
279+
// ./server-instructions.ts.
273280
this.transport.sendResult(request.id, {
274281
protocolVersion: PROTOCOL_VERSION,
275282
capabilities: {
276283
tools: {},
277284
},
278285
serverInfo: SERVER_INFO,
286+
instructions: SERVER_INSTRUCTIONS,
279287
});
280288
}
281289

src/mcp/server-instructions.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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 (PR review = X then Y; refactor planning = A then B)
12+
* - Anti-patterns (don't grep when codegraph_search is faster)
13+
* - Tier discipline (cheap deterministic → conditional → LLM-mediated)
14+
*
15+
* Keep it tight. The agent reads this every session — long instructions
16+
* burn tokens. Aim for under ~80 lines of useful guidance.
17+
*/
18+
export const SERVER_INSTRUCTIONS = `# Codegraph — code intelligence over an indexed knowledge graph
19+
20+
Codegraph builds a SQLite knowledge graph of every symbol, edge, and
21+
file in the workspace. It is a structural reference manual the agent
22+
consults BEFORE writing or editing code, not a live linter that runs
23+
during generation. Reads are sub-millisecond; the index lags writes by
24+
about a second through the file watcher.
25+
26+
## When to use which tool
27+
28+
- **"What is the symbol named X?"** → \`codegraph_search\` (fast lookup)
29+
- **"What's the deal with this task / feature / bug?"** → \`codegraph_context\` (PRIMARY tool — composes 5+ smaller queries into one answer)
30+
- **"What calls this function?"** → \`codegraph_callers\`
31+
- **"What does this function call?"** → \`codegraph_callees\`
32+
- **"What would changing this break?"** → \`codegraph_impact\`
33+
- **"Show me this symbol's source / signature / docstring."** → \`codegraph_node\`
34+
- **"Survey an unfamiliar topic / pattern / module."** → \`codegraph_explore\` (heavier; best when budget allows)
35+
- **"What's in directory X?"** → \`codegraph_files\`
36+
- **"Is the index ready / what's its size?"** → \`codegraph_status\`
37+
38+
## Common chains (run tools in sequence)
39+
40+
- **Onboarding to a topic**: \`codegraph_context\` first. If still unclear, \`codegraph_explore\` for breadth, then \`codegraph_node\` on specific symbols you want code for.
41+
- **PR review**: if \`codegraph_review_context\` is available (PR #110), pass the unified diff to it — returns affected symbols + their callers + impact + co-change warnings in one call.
42+
- **Refactor planning**: \`codegraph_search\` to find the symbol, \`codegraph_callers\` to see what depends on it, \`codegraph_impact\` to see the blast radius.
43+
- **Debugging a regression**: \`codegraph_callers\` of the suspected symbol. If recent changes are in scope, look for hotspot tools (\`codegraph_hotspots\` if available) to identify churn × centrality risk.
44+
45+
## Tool tiers (start cheap, escalate when needed)
46+
47+
1. **Always available, deterministic, sub-millisecond**: search / context / callers / callees / impact / node / explore / files / status. Most tasks can be answered entirely at this tier.
48+
2. **Conditional on data availability**: \`codegraph_review_context\` needs a diff. \`codegraph_hotspots\`, \`codegraph_config\`, \`codegraph_sql\` need their respective indexed signals (git history, env-var read sites, SQL string-literals). All return clearly when data isn't present.
49+
3. **LLM-mediated, opt-in**: \`codegraph_ask\` (RAG Q&A), \`codegraph_similar\` (semantic search), \`codegraph_dead_code\` (graph + LLM judge), \`codegraph_role\` / \`codegraph_module\` (LLM classifications). These require a configured local LLM endpoint or the agent-bridge tier.
50+
51+
## Agent-bridge tier (when no local LLM is configured)
52+
53+
When LLM-mediated tools aren't available but the user wants summaries:
54+
1. Call \`codegraph_pending_summaries\` to pull a batch of symbols needing summaries (returns each symbol's body + content_hash).
55+
2. The agent (you) generate one-line summaries for each — action-verb leading, no "This function..." preamble, ≤200 chars.
56+
3. Call \`codegraph_save_summaries\` echoing each item's contentHash unchanged. Codegraph re-validates against current disk before persisting.
57+
58+
This lets agents do LLM work themselves when no separate LLM endpoint exists.
59+
60+
## Anti-patterns
61+
62+
- **Don't grep first** when looking up a symbol by name — \`codegraph_search\` is faster and returns kind + location + signature.
63+
- **Don't call \`codegraph_search\` then \`codegraph_node\`** when you just want context — \`codegraph_context\` is one round-trip.
64+
- **Don't use \`codegraph_explore\` for narrow questions** — it's a multi-call deep dive, expensive in tokens. Save it for genuine "I'm new here" surveys.
65+
- **Don't query the index immediately after editing a file** — the watcher needs ~500ms to debounce + sync. Wait for the next turn.
66+
67+
## Limitations
68+
69+
- Index lags file writes by ~1 second (watcher debounce + sync).
70+
- Cross-file resolution is a best-effort name match; ambiguous calls return multiple candidates.
71+
- 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.
72+
`;

0 commit comments

Comments
 (0)