Skip to main content
Glama
PWalaGov

Enhanced Directory Context MCP Server

by PWalaGov

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

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
max_depthNoMaximum depth to traverse (default: 10)
include_hiddenNoInclude hidden files and directories
file_typesNoFilter by file extensions (e.g., [".js", ".py", ".md"])

Implementation Reference

  • 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), }, ], }; }
  • 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, }; } }
  • 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' }, }, }, }, },

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/PWalaGov/File-Control-MCP'

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