list_directory
List files and directories in a specified path with clear [FILE] and [DIR] labels to understand directory structure and locate content.
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 |
Implementation Reference
- src/filesystem/index.ts:416-426 (handler)The handler function that validates the path, reads directory entries using fs.readdir, formats them with [DIR]/[FILE] prefixes, and returns the formatted list.async (args: z.infer<typeof ListDirectoryArgsSchema>) => { const validPath = await validatePath(args.path); const entries = await fs.readdir(validPath, { withFileTypes: true }); const formatted = entries .map((entry) => `${entry.isDirectory() ? "[DIR]" : "[FILE]"} ${entry.name}`) .join("\n"); return { content: [{ type: "text" as const, text: formatted }], structuredContent: { content: formatted } }; }
- src/filesystem/index.ts:401-427 (registration)Tool registration using server.registerTool, specifying the name 'list_directory', title, description, input/output schemas, annotations, and the handler function.server.registerTool( "list_directory", { title: "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: { path: z.string() }, outputSchema: { content: z.string() }, annotations: { readOnlyHint: true } }, async (args: z.infer<typeof ListDirectoryArgsSchema>) => { const validPath = await validatePath(args.path); const entries = await fs.readdir(validPath, { withFileTypes: true }); const formatted = entries .map((entry) => `${entry.isDirectory() ? "[DIR]" : "[FILE]"} ${entry.name}`) .join("\n"); return { content: [{ type: "text" as const, text: formatted }], structuredContent: { content: formatted } }; } );
- src/filesystem/index.ts:114-116 (schema)Zod schema for input arguments of the list_directory tool, defining the required 'path' parameter.const ListDirectoryArgsSchema = z.object({ path: z.string(), });