Skip to main content
Glama

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
NameRequiredDescriptionDefault
target_pathNoSpecific directory or file to analyze (relative to project root). Defaults to root.
depth_limitNoHow many folder levels deep to scan. Use 1-2 for large projects.
include_symbolsNoInclude function/class/enum names in the tree. Defaults to true.
max_tokensNoMaximum tokens for output. Auto-prunes if exceeded. Default: 20000.

Implementation Reference

  • 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}`;
    }
  • 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",

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/ForLoopCodes/contextplus'

If you have feedback or need assistance with the MCP directory API, please join our Discord server