Skip to content

Commit 804ab67

Browse files
colbymchenryclaude
andauthored
feat(mcp): emit server-level instructions in initialize response (colbymchenry#143)
Adds a universal tool-selection playbook surfaced by MCP clients (Claude Code, Cursor, opencode, LangChain, OpenAI Agent SDK) in the agent's system prompt automatically. Without this, agents have to infer tool composition from individual tool descriptions and tend to walk callers manually instead of reaching for codegraph_impact, etc. Scoped tight: only the 9 tools that exist on main today (search/context/callers/callees/impact/node/explore/files/status), no "(when present)" references to unmerged tools, no per-language guidance. ~40 lines of useful guidance. Salvaged from colbymchenry#121, which bundled the instructions with colbymchenry#117's MCP tool-registry refactor and referenced many tools that don't exist on main. Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent a460b85 commit 804ab67

2 files changed

Lines changed: 61 additions & 1 deletion

File tree

src/mcp/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import * as path from 'path';
1919
import CodeGraph, { findNearestCodeGraphRoot } from '../index';
2020
import { StdioTransport, JsonRpcRequest, JsonRpcNotification, ErrorCodes } from './transport';
2121
import { tools, ToolHandler } from './tools';
22+
import { SERVER_INSTRUCTIONS } from './server-instructions';
2223

2324
/**
2425
* Convert a file:// URI to a filesystem path.
@@ -268,13 +269,17 @@ export class MCPServer {
268269
// Try to initialize the default project (non-fatal if it fails)
269270
await this.tryInitializeDefault(projectPath);
270271

271-
// We accept the client's protocol version but respond with our supported version
272+
// We accept the client's protocol version but respond with our supported version.
273+
// The `instructions` field is surfaced by MCP clients in the agent's system
274+
// prompt automatically — it's the right place for the universal tool-selection
275+
// playbook, ahead of individual tool descriptions.
272276
this.transport.sendResult(request.id, {
273277
protocolVersion: PROTOCOL_VERSION,
274278
capabilities: {
275279
tools: {},
276280
},
277281
serverInfo: SERVER_INFO,
282+
instructions: SERVER_INSTRUCTIONS,
278283
});
279284
}
280285

src/mcp/server-instructions.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
20+
Codegraph is a SQLite knowledge graph of every symbol, edge, and file
21+
in the workspace. Reads are sub-millisecond; the index lags writes by
22+
about a second through the file watcher. Consult it BEFORE writing or
23+
editing code, not during.
24+
25+
## Tool selection by intent
26+
27+
- **"What is the symbol named X?"** → \`codegraph_search\`
28+
- **"What's the deal with this task / feature / area?"** → \`codegraph_context\` (PRIMARY — composes search + node + callers + callees in one call)
29+
- **"What calls this?"** → \`codegraph_callers\`
30+
- **"What does this call?"** → \`codegraph_callees\`
31+
- **"What would changing this break?"** → \`codegraph_impact\`
32+
- **"Show me this symbol's source / signature / docstring."** → \`codegraph_node\`
33+
- **"Survey an unfamiliar topic / pattern / module."** → \`codegraph_explore\` (heavier; deep dive)
34+
- **"What's in directory X?"** → \`codegraph_files\`
35+
- **"Is the index ready / what's its size?"** → \`codegraph_status\`
36+
37+
## Common chains
38+
39+
- **Onboarding**: \`codegraph_context\` first. If still unclear, \`codegraph_explore\` for breadth, then \`codegraph_node\` on specific symbols.
40+
- **Refactor planning**: \`codegraph_search\` → \`codegraph_callers\` → \`codegraph_impact\`. The blast-radius answer comes from impact, not from walking callers manually.
41+
- **Debugging a regression**: \`codegraph_callers\` of the suspected symbol; widen with \`codegraph_impact\` if an unexpected call appears.
42+
43+
## Anti-patterns
44+
45+
- **Don't grep first** when looking up a symbol by name — \`codegraph_search\` is faster and returns kind + location + signature.
46+
- **Don't chain \`codegraph_search\` + \`codegraph_node\`** when you just want context — \`codegraph_context\` is one round-trip.
47+
- **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.
48+
- **Don't query the index immediately after editing a file** — the watcher needs ~500ms to debounce + sync. Wait for the next turn.
49+
50+
## Limitations
51+
52+
- Index lags file writes by ~1 second.
53+
- Cross-file resolution is best-effort name matching; ambiguous calls may return multiple candidates.
54+
- 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.
55+
`;

0 commit comments

Comments
 (0)