list_directory
Retrieve a structured listing of files and directories in a specified path, identifying each with [FILE] or [DIR] markers. Essential for analyzing directory contents and locating specific files within the Filesystem MCP Server.
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
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes |
Input Schema (JSON Schema)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"properties": {
"path": {
"type": "string"
}
},
"required": [
"path"
],
"type": "object"
}
Implementation Reference
- src/filesystem/index.ts:114-116 (schema)Schema definition for the input arguments of the list_directory tool, specifying the 'path' parameter.const ListDirectoryArgsSchema = z.object({ path: z.string(), });
- src/filesystem/index.ts:416-426 (handler)Handler function that validates the path, reads the directory entries using fs.readdir with file types, formats them with [DIR] or [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)Registration of the 'list_directory' tool with title, description, input/output schemas, annotations, and references 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 } }; } );