get_context_tree
Analyze project structure to extract file headers, functions, classes, and enums with dynamic pruning based on project size for efficient codebase navigation.
Instructions
Get the structural tree of the project with file headers, function names, classes, enums, and line ranges. Automatically reads 2-line headers for file purpose. Dynamic token-aware pruning: Level 2 (deep symbols) -> Level 1 (headers only) -> Level 0 (file names only) based on project size.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| target_path | No | Specific directory or file to analyze (relative to project root). Defaults to root. | |
| depth_limit | No | How many folder levels deep to scan. Use 1-2 for large projects. | |
| include_symbols | No | Include function/class/enum names in the tree. Defaults to true. | |
| max_tokens | No | Maximum tokens for output. Auto-prunes if exceeded. Default: 20000. |
Implementation Reference
- src/tools/context-tree.ts:107-129 (handler)The main handler function for the `get_context_tree` tool, which orchestrates directory walking, building the tree structure, and pruning content to fit within token limits.
export async function getContextTree(options: ContextTreeOptions): Promise<string> { const entries = await walkDirectory({ rootDir: options.rootDir, targetPath: options.targetPath, depthLimit: options.depthLimit, }); const includeSymbols = options.includeSymbols !== false; const tree = await buildTree(entries, options.rootDir, includeSymbols); const maxTokens = options.maxTokens ?? 20000; let rendered = renderTree(tree); if (estimateTokens(rendered) <= maxTokens) return rendered; pruneSymbols(tree); rendered = renderTree(tree); if (estimateTokens(rendered) <= maxTokens) return `[Level 1: Headers only, symbols pruned to fit ${maxTokens} tokens]\n\n${rendered}`; pruneHeaders(tree); rendered = renderTree(tree); return `[Level 0: File names only, project too large for ${maxTokens} tokens]\n\n${rendered}`; } - src/tools/context-tree.ts:7-13 (schema)Input options type definition for `getContextTree`.
export interface ContextTreeOptions { rootDir: string; targetPath?: string; depthLimit?: number; includeSymbols?: boolean; maxTokens?: number; } - src/index.ts:169-169 (registration)Tool registration for `get_context_tree` within the MCP tool definitions.
"get_context_tree",