list_directory
Retrieve a detailed listing of files and directories in a specified path with [FILE] and [DIR] prefixes for easy identification. Works within allowed directories for clear organization.
Instructions
Get a detailed listing of all files and directories in a specified path. Results distinguish between files and directories with [FILE] and [DIR] prefixes. Only works within allowed directories.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes |
Implementation Reference
- src/server.ts:294-300 (handler)Handler for the list_directory tool: parses input arguments using the schema, calls the listDirectory helper function with the path, and returns the directory entries as a formatted text response.case "list_directory": { const parsed = ListDirectoryArgsSchema.parse(args); const entries = await listDirectory(parsed.path); return { content: [{ type: "text", text: entries.join('\n') }], }; }
- src/tools/filesystem.ts:95-99 (helper)Exact implementation of the listDirectory function: validates the directory path for security, reads the directory contents asynchronously using fs.readdir with file types, and maps entries to strings prefixed with [DIR] or [FILE].export async function listDirectory(dirPath: string): Promise<string[]> { const validPath = await validatePath(dirPath); const entries = await fs.readdir(validPath, { withFileTypes: true }); return entries.map((entry) => `${entry.isDirectory() ? "[DIR]" : "[FILE]"} ${entry.name}`); }
- src/server.ts:156-162 (registration)Tool registration in the ListToolsRequestHandler: defines the tool name, detailed description, and references the input schema.{ name: "list_directory", description: "Get a detailed listing of all files and directories in a specified path. " + "Results distinguish between files and directories with [FILE] and [DIR] prefixes. " + "Only works within allowed directories.", inputSchema: zodToJsonSchema(ListDirectoryArgsSchema),
- src/tools/schemas.ts:49-51 (schema)Zod input schema for the list_directory tool, requiring a single 'path' string parameter.export const ListDirectoryArgsSchema = z.object({ path: z.string(), });