list_directory
Lists all files and directories in a specified path with clear [FILE] and [DIR] labels to understand directory structure and locate specific items.
Instructions
Get a detailed listing of all files and directories in a specified path. Results clearly distinguish between files and directories with [FILE] and [DIR] prefixes. This tool is essential for understanding directory structure and finding specific files within a directory. Only works within allowed directories.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes | Path of the directory to list |
Implementation Reference
- src/index.ts:490-518 (handler)The handler function for the 'list_directory' tool. Validates input using ListDirectoryArgsSchema, resolves and validates the path, reads directory entries with fs.readdir, sorts them (dirs first), formats with [DIR]/[FILE] prefixes, and returns as text content.case 'list_directory': { const parsed = ListDirectoryArgsSchema.safeParse(a) if (!parsed.success) { throw new FileSystemError(`Invalid arguments for ${name}`, 'INVALID_ARGS', undefined, { errors: parsed.error.format(), }) } const validPath = await validatePath(parsed.data.path, config) const entries = await fs.readdir(validPath, { withFileTypes: true }) // Sort directories first, then files, both alphabetically entries.sort((c, d) => { if (c.isDirectory() && !d.isDirectory()) return -1 if (!c.isDirectory() && d.isDirectory()) return 1 return c.name.localeCompare(d.name) }) const formatted = entries .map((entry) => `${entry.isDirectory() ? '[DIR]' : '[FILE]'} ${entry.name}`) .join('\n') await logger.debug(`Listed directory: ${validPath}`, { entryCount: entries.length }) endMetric() return { content: [{ type: 'text', text: formatted }], } }
- src/index.ts:120-122 (schema)Zod schema defining the input for list_directory tool: requires a 'path' string.const ListDirectoryArgsSchema = z.object({ path: z.string().describe('Path of the directory to list'), })
- src/index.ts:279-286 (registration)Registration of the 'list_directory' tool in the ListTools response, including name, description, and input schema converted to JSON schema.{ name: 'list_directory', description: 'Get a detailed listing of all files and directories in a specified path. ' + 'Results clearly distinguish between files and directories with [FILE] and [DIR] ' + 'prefixes. This tool is essential for understanding directory structure and ' + 'finding specific files within a directory. Only works within allowed directories.', inputSchema: zodToJsonSchema(ListDirectoryArgsSchema) as ToolInput,