get_directory_structure
Retrieve complete directory structures with file metadata to analyze project organization and locate files efficiently.
Instructions
Get the complete directory structure with file metadata
Tool call template
This tool declares no required parameters. Add optional parameters from the input schema as needed.
{
"id": 1,
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"arguments": {},
"name": "get_directory_structure"
}
}Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| max_depth | No | Maximum depth to traverse (default: 10) | |
| include_hidden | No | Include hidden files and directories | |
| file_types | No | Filter by file extensions (e.g., [".js", ".py", ".md"]) |
Implementation Reference
- server.js:863-881 (handler)Main handler function for 'get_directory_structure' tool. Parses input arguments, invokes the recursive directory traversal helper, and returns the directory structure as formatted JSON.
async handleGetDirectoryStructure(args) { const { max_depth = 10, include_hidden = false, file_types } = args; const structure = await this.getDirectoryStructure( this.workingDirectory, max_depth, include_hidden, file_types ); return { content: [ { type: 'text', text: JSON.stringify(structure, null, 2), }, ], }; } - server.js:949-1007 (helper)Core recursive implementation that builds the directory tree structure using fs.readdir and fs.stat, filtering by depth, hidden files, and file types, including file metadata.
async getDirectoryStructure(dirPath, maxDepth = 10, includeHidden = false, fileTypes = null, currentDepth = 0) { if (currentDepth >= maxDepth) { return null; } try { const entries = await fs.readdir(dirPath, { withFileTypes: true }); const structure = { name: path.basename(dirPath), path: path.relative(this.workingDirectory, dirPath) || '.', type: 'directory', children: [], }; for (const entry of entries) { if (!includeHidden && entry.name.startsWith('.')) { continue; } const fullPath = path.join(dirPath, entry.name); const relativePath = path.relative(this.workingDirectory, fullPath); if (entry.isDirectory()) { const subStructure = await this.getDirectoryStructure( fullPath, maxDepth, includeHidden, fileTypes, currentDepth + 1 ); if (subStructure) { structure.children.push(subStructure); } } else { const ext = path.extname(entry.name); if (!fileTypes || fileTypes.includes(ext)) { const stats = await fs.stat(fullPath); structure.children.push({ name: entry.name, path: relativePath, type: 'file', size: stats.size, modified: stats.mtime.toISOString(), extension: ext, }); } } } return structure; } catch (error) { return { name: path.basename(dirPath), path: path.relative(this.workingDirectory, dirPath), type: 'directory', error: error.message, }; } } - server.js:68-87 (schema)Input schema validation for tool parameters: max_depth, include_hidden, file_types.
inputSchema: { type: 'object', properties: { max_depth: { type: 'number', description: 'Maximum depth to traverse (default: 10)', default: 10, }, include_hidden: { type: 'boolean', description: 'Include hidden files and directories', default: false, }, file_types: { type: 'array', description: 'Filter by file extensions (e.g., [".js", ".py", ".md"])', items: { type: 'string' }, }, }, }, - server.js:457-458 (registration)Dispatch registration in the switch statement handling tool calls, routing to the specific handler.
case 'get_directory_structure': return await this.handleGetDirectoryStructure(args); - server.js:65-88 (registration)Tool registration in the tools list returned by ListTools handler, including name, description, and input schema.
{ name: 'get_directory_structure', description: 'Get the complete directory structure with file metadata', inputSchema: { type: 'object', properties: { max_depth: { type: 'number', description: 'Maximum depth to traverse (default: 10)', default: 10, }, include_hidden: { type: 'boolean', description: 'Include hidden files and directories', default: false, }, file_types: { type: 'array', description: 'Filter by file extensions (e.g., [".js", ".py", ".md"])', items: { type: 'string' }, }, }, }, },