Skip to main content
Glama

list_directory

Retrieve a structured listing of files and directories within a specified path, marked with [FILE] and [DIR] prefixes. Use this tool to analyze directory structures and locate specific items efficiently 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
NameRequiredDescriptionDefault
pathYesPath of the directory to list

Implementation Reference

  • Handler for the list_directory tool: parses arguments, validates path access, reads directory contents using fs.readdir with file types, sorts entries (directories first alphabetically, then files), formats output with [DIR] or [FILE] prefixes, logs the operation, and returns formatted 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 }], } }
  • Zod schema for list_directory tool input, requiring a 'path' string parameter.
    const ListDirectoryArgsSchema = z.object({ path: z.string().describe('Path of the directory to list'), })
  • src/index.ts:279-287 (registration)
    Tool registration in the ListTools response: defines 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 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, },

Other Tools

Related Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/gabrielmaialva33/mcp-filesystem'

If you have feedback or need assistance with the MCP directory API, please join our Discord server