list_directory
Retrieve detailed file and directory listings from a specified path, using [FILE] and [DIR] prefixes for clear differentiation. Essential for analyzing directory structures and locating specific files within permitted directories.
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/handlers/directory-handlers.ts:51-66 (handler)The main handler function for list_directory tool. Parses arguments, validates path, reads directory contents using fs.readdir, formats as [DIR]/[FILE] list, and returns as text content.export async function handleListDirectory( args: unknown, allowedDirectories: string[], symlinksMap: Map<string, string>, noFollowSymlinks: boolean ) { const parsed = parseArgs(ListDirectoryArgsSchema, args, 'list_directory'); const validPath = await validatePath(parsed.path, allowedDirectories, symlinksMap, noFollowSymlinks); 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", text: formatted }], }; }
- Input schema definition for list_directory: requires a 'path' string parameter.export const ListDirectoryArgsSchema = Type.Object({ path: Type.String(), }); export type ListDirectoryArgs = Static<typeof ListDirectoryArgsSchema>;
- index.ts:208-209 (registration)Registers the list_directory tool handler by binding the handleListDirectory function with context parameters in the toolHandlers object.list_directory: (a: unknown) => handleListDirectory(a, allowedDirectories, symlinksMap, noFollowSymlinks),
- index.ts:307-307 (registration)Declares the list_directory tool metadata (name and description) in the allTools array, used for conditional registration based on permissions.{ name: "list_directory", description: "List directory contents" },
- src/schemas/index.ts:77-77 (schema)Maps the ListDirectoryArgsSchema to the 'list_directory' key in the central toolSchemas export, making it available for registration.list_directory: ListDirectoryArgsSchema,