list_directory_with_sizes
Generate a detailed listing of files and directories in a specified path, including sizes. Results identify files and directories with [FILE] and [DIR] prefixes, helping analyze directory structure and locate specific items. Works within allowed directories.
Instructions
Get a detailed listing of all files and directories in a specified path, including sizes. Results clearly distinguish between files and directories with [FILE] and [DIR] prefixes. This tool is useful for understanding directory structure and finding specific files within a directory. Only works within allowed directories.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes | ||
| sortBy | No | Sort entries by name or size | name |
Input Schema (JSON Schema)
Implementation Reference
- src/filesystem/index.ts:445-505 (handler)The handler function that implements the list_directory_with_sizes tool. It reads the directory, stats each entry for size, sorts by name or size, formats the output with [DIR]/[FILE] prefixes and sizes, adds a summary of totals, and returns formatted text content.async (args: z.infer<typeof ListDirectoryWithSizesArgsSchema>) => { const validPath = await validatePath(args.path); const entries = await fs.readdir(validPath, { withFileTypes: true }); // Get detailed information for each entry const detailedEntries = await Promise.all( entries.map(async (entry) => { const entryPath = path.join(validPath, entry.name); try { const stats = await fs.stat(entryPath); return { name: entry.name, isDirectory: entry.isDirectory(), size: stats.size, mtime: stats.mtime }; } catch (error) { return { name: entry.name, isDirectory: entry.isDirectory(), size: 0, mtime: new Date(0) }; } }) ); // Sort entries based on sortBy parameter const sortedEntries = [...detailedEntries].sort((a, b) => { if (args.sortBy === 'size') { return b.size - a.size; // Descending by size } // Default sort by name return a.name.localeCompare(b.name); }); // Format the output const formattedEntries = sortedEntries.map(entry => `${entry.isDirectory ? "[DIR]" : "[FILE]"} ${entry.name.padEnd(30)} ${ entry.isDirectory ? "" : formatSize(entry.size).padStart(10) }` ); // Add summary const totalFiles = detailedEntries.filter(e => !e.isDirectory).length; const totalDirs = detailedEntries.filter(e => e.isDirectory).length; const totalSize = detailedEntries.reduce((sum, entry) => sum + (entry.isDirectory ? 0 : entry.size), 0); const summary = [ "", `Total: ${totalFiles} files, ${totalDirs} directories`, `Combined size: ${formatSize(totalSize)}` ]; const text = [...formattedEntries, ...summary].join("\n"); const contentBlock = { type: "text" as const, text }; return { content: [contentBlock], structuredContent: { content: [contentBlock] } }; }
- src/filesystem/index.ts:118-121 (schema)Zod schema defining the input arguments for the list_directory_with_sizes tool: path (required string) and sortBy (optional 'name' or 'size'). Used in the handler's type inference.const ListDirectoryWithSizesArgsSchema = z.object({ path: z.string(), sortBy: z.enum(['name', 'size']).optional().default('name').describe('Sort entries by name or size'), });
- src/filesystem/index.ts:429-444 (registration)Registers the list_directory_with_sizes tool with the MCP server, providing title, description, input/output schemas, and annotations. The handler function follows as the third argument.server.registerTool( "list_directory_with_sizes", { title: "List Directory with Sizes", description: "Get a detailed listing of all files and directories in a specified path, including sizes. " + "Results clearly distinguish between files and directories with [FILE] and [DIR] " + "prefixes. This tool is useful for understanding directory structure and " + "finding specific files within a directory. Only works within allowed directories.", inputSchema: { path: z.string(), sortBy: z.enum(["name", "size"]).optional().default("name").describe("Sort entries by name or size") }, outputSchema: { content: z.string() }, annotations: { readOnlyHint: true } },