Skip to main content
Glama
modelcontextprotocol

Filesystem MCP Server

Official

list_directory

List files and directories in a specified path with clear [FILE] and [DIR] labels to understand directory structure and locate content.

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
pathYes

Implementation Reference

  • The handler function that validates the path, reads directory entries using fs.readdir, formats them with [DIR]/[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 }
      };
    }
  • Tool registration using server.registerTool, specifying the name 'list_directory', title, description, input/output schemas, annotations, and 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 }
        };
      }
    );
  • Zod schema for input arguments of the list_directory tool, defining the required 'path' parameter.
    const ListDirectoryArgsSchema = z.object({
      path: z.string(),
    });

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