get_deep_directory_tree
Visualize complex directory structures by generating a deep tree view. Specify path, depth, and exclude patterns to efficiently navigate and manage filesystem hierarchies.
Instructions
Get deep directory tree
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| options | No | Tree generation options | |
| path | Yes | Path to get the directory tree (preferably absolute path) |
Implementation Reference
- src/index.ts:93-104 (handler)The asynchronous handler function provided to mcp.tool for executing the get_deep_directory_tree tool logic. It invokes getDirectoryTree and returns the result formatted as MCP content.async ({ path, options }) => { const directoryStructure = getDirectoryTree(path, options); return { content: [ { type: "text", text: directoryStructure, }, ], }; }
- src/index.ts:73-92 (schema)Zod schema defining the input parameters for the get_deep_directory_tree tool: 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"), },
- src/index.ts:70-105 (registration)Registration of the get_deep_directory_tree tool with the MCP server using mcp.tool(), including name, description, input schema, and handler.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, }, ], }; } );
- src/index.ts:26-68 (helper)Core helper function that traverses the directory, builds the tree structure as a string array, handles exclusion patterns and depth limits, and returns the formatted tree.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"); }