code_nav.extract_context
Extract compact code context from repository files by specifying targets and optional queries, using probe or fallback range methods.
Instructions
Extract compact code context with Probe or fallback ranges.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| targets | Yes | ||
| query | No | ||
| max_tokens | No |
Implementation Reference
- src/tools/extract-context.ts:11-23 (handler)Handler function for code_nav.extract_context tool. Orchestrates extraction by getting repo root and calling extractContext from probe-adapter.
export async function extractContextTool( input: ExtractContextInput, cwd = process.cwd(), ) { const repo = await getRepoRoot(cwd); const extracted = await extractContext( repo.repoRoot, input.targets, input.query, clampLimit(input.max_tokens, DEFAULT_CONTEXT_TOKENS, 50_000), ); return extracted.blocks; } - src/core/probe-adapter.ts:17-44 (handler)Core extraction logic that tries Probe first, then falls back to file read for each target, with token limiting.
export async function extractContext( repoRoot: string, targets: string[], query: string | undefined, maxTokens: number, ): Promise<{ blocks: ContextBlock[]; warnings: string[] }> { const warnings: string[] = []; const blocks: ContextBlock[] = []; const hasProbe = await commandExists("probe"); for (const target of targets) { if (approxTokens(blocks.map((block) => block.content).join("\n")) >= maxTokens) break; const parsed = parseTarget(target); if (!parsed) { warnings.push(`invalid target: ${target}`); continue; } if (hasProbe) { const probeBlock = await tryProbe(repoRoot, parsed, query); if (probeBlock) { blocks.push(probeBlock); continue; } warnings.push(`probe failed for ${target}; used fallback`); } blocks.push(await fallbackExtract(repoRoot, parsed)); } return { blocks: trimBlocks(blocks, maxTokens), warnings }; } - src/tools/extract-context.ts:5-9 (schema)TypeScript interface for the tool's input schema: targets (required), query (optional), max_tokens (optional).
export interface ExtractContextInput { targets: string[]; query?: string; max_tokens?: number; } - src/mcp.ts:77-89 (registration)Registration of code_nav.extract_context tool on the MCP server with inputSchema and handler binding.
server.registerTool( "code_nav.extract_context", { description: "Extract compact code context with Probe or fallback ranges.", inputSchema: { targets: z.array(z.string()).min(1), query: z.string().optional(), max_tokens: z.number().int().positive().optional(), }, annotations: { readOnlyHint: true, openWorldHint: false }, }, async (input) => mcpJson(await extractContextTool(input)), ); - src/core/probe-adapter.ts:115-126 (helper)Helper function trimBlocks that enforces max_tokens limit on extracted blocks.
function trimBlocks(blocks: ContextBlock[], maxTokens: number): ContextBlock[] { const kept: ContextBlock[] = []; let used = 0; for (const block of blocks) { const tokens = approxTokens(block.content); if (used + tokens > maxTokens && kept.length > 0) break; kept.push(block); used += tokens; } return kept; }