list_directory
Lists the contents of a specified directory path to view files and subdirectories within a file system.
Instructions
List the contents of a directory
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes | The directory path to list |
Implementation Reference
- src/index.ts:32-34 (schema)Zod schema for validating input arguments to the list_directory tool.const ListDirectoryArgsSchema = z.object({ path: z.string().describe("The directory path to list") });
- src/index.ts:144-157 (registration)Tool registration in the ListToolsRequestSchema handler, defining name, description, and JSON input schema.{ name: "list_directory", description: "List the contents of a directory", inputSchema: { type: "object", properties: { path: { type: "string", description: "The directory path to list" } }, required: ["path"] } },
- src/index.ts:216-268 (handler)Main handler logic for the list_directory tool: validates input, reads directory, gets stats for each entry, formats output with icons, sizes, and modification times.case "list_directory": { const { path: dirPath } = ListDirectoryArgsSchema.parse(args); const safePath = validatePath(dirPath); const entries = await fs.readdir(safePath, { withFileTypes: true }); const items = await Promise.all( entries.map(async (entry) => { const fullPath = path.join(safePath, entry.name); try { const stats = await fs.stat(fullPath); return { name: entry.name, type: entry.isDirectory() ? 'directory' : 'file', size: entry.isFile() ? formatFileSize(stats.size) : null, modified: stats.mtime.toISOString() }; } catch (error) { return { name: entry.name, type: entry.isDirectory() ? 'directory' : 'file', size: null, modified: null, error: 'Unable to read stats' }; } }) ); return { content: [ { type: "text", text: `Directory listing for: ${safePath}\n\n` + items .sort((a, b) => { // Sort directories first, then files if (a.type !== b.type) { return a.type === 'directory' ? -1 : 1; } return a.name.localeCompare(b.name); }) .map(item => { const icon = item.type === 'directory' ? 'π' : 'π'; const size = item.size ? ` (${item.size})` : ''; const modified = item.modified ? ` - Modified: ${new Date(item.modified).toLocaleString()}` : ''; return `${icon} ${item.name}${size}${modified}`; }) .join('\n') } ] }; }