Skip to main content
Glama

get_deep_directory_tree

Generate a detailed directory tree from a specified path, customizing depth and excluding specific patterns for efficient file structure visualization and analysis.

Instructions

Get deep directory tree

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
optionsNoTree generation options
pathYesPath to get the directory tree (preferably absolute path)

Implementation Reference

  • src/index.ts:70-105 (registration)
    Registration of the 'get_deep_directory_tree' tool with the MCP server using mcp.tool(), specifying name, description, input schema, and handler function.
    mcp.tool( "get_deep_directory_tree", "Get deep directory tree", { path: z .string() .describe("Path to get the directory tree (preferably absolute path)"), options: z .object({ depth: z.number().default(3).describe("Depth of the directory tree"), excludePatterns: z .array(z.string()) .default(["node_modules", ".git", ".turbo", "dist", ".next"]) .describe( "Patterns to exclude from the tree (e.g., ['node_modules', '*.log'])" ), }) .default({ depth: 3, excludePatterns: ["node_modules", ".git", ".turbo", "dist", ".next"], }) .describe("Tree generation options"), }, async ({ path, options }) => { const directoryStructure = getDirectoryTree(path, options); return { content: [ { type: "text", text: directoryStructure, }, ], }; } );
  • Zod schema definition for the tool inputs: 'path' (string) and 'options' (object with depth and excludePatterns).
    { path: z .string() .describe("Path to get the directory tree (preferably absolute path)"), options: z .object({ depth: z.number().default(3).describe("Depth of the directory tree"), excludePatterns: z .array(z.string()) .default(["node_modules", ".git", ".turbo", "dist", ".next"]) .describe( "Patterns to exclude from the tree (e.g., ['node_modules', '*.log'])" ), }) .default({ depth: 3, excludePatterns: ["node_modules", ".git", ".turbo", "dist", ".next"], }) .describe("Tree generation options"), },
  • Async handler function that invokes getDirectoryTree with provided path and options, then returns the directory structure as text content in MCP format.
    async ({ path, options }) => { const directoryStructure = getDirectoryTree(path, options); return { content: [ { type: "text", text: directoryStructure, }, ], }; } );
  • Core helper function getDirectoryTree that recursively traverses the directory up to specified depth, excludes patterns, and builds a tree string representation.
    function getDirectoryTree( rootPath: string, options: TreeOptions = { depth: 3, excludePatterns: ["node_modules", ".git", ".turbo", "dist", ".next"], } ) { const tree: string[] = []; function shouldExclude(file: string): boolean { return options.excludePatterns.some((pattern) => { if (pattern.includes("*")) { const regexPattern = pattern.replace(/\*/g, ".*"); return new RegExp(regexPattern).test(file); } return file === pattern; }); } function traverse(dir: string, currentDepth: number = 0) { if (currentDepth >= options.depth) return; const files = fs.readdirSync(dir); const filteredFiles = files.filter((file) => !shouldExclude(file)); filteredFiles.forEach((file, index) => { const filePath = path.join(dir, file); const stats = fs.statSync(filePath); const isLast = index === filteredFiles.length - 1; const prefix = isLast ? "└── " : "├── "; if (stats.isDirectory()) { tree.push(`${" ".repeat(currentDepth)}${prefix}${file}`); traverse(filePath, currentDepth + 1); } else { tree.push(`${" ".repeat(currentDepth)}${prefix}${file}`); } }); } traverse(rootPath); return tree.join("\n"); }

Other Tools

Related Tools

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/andredezzy/deep-directory-tree-mcp'

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