Skip to main content
Glama

Filesystem MCP Server

Official

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

NameRequiredDescriptionDefault
pathYes

Input Schema (JSON Schema)

{ "$schema": "http://json-schema.org/draft-07/schema#", "additionalProperties": false, "properties": { "path": { "type": "string" } }, "required": [ "path" ], "type": "object" }

Implementation Reference

  • Schema definition for the input arguments of the list_directory tool, specifying the 'path' parameter.
    const ListDirectoryArgsSchema = z.object({ path: z.string(), });
  • 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 } }; }
  • 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 } }; } );

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/modelcontextprotocol/filesystem'

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