Skip to main content
Glama

listFiles

List files and directories in a specified path to view folder contents and manage file structures within Claude Code MCP server.

Instructions

Lists files and directories in a given path

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
pathYesThe absolute path to the directory to list

Implementation Reference

  • MCP tool handler for listFiles that invokes the utility function and formats the response as JSON.
    async ({ path }) => {
      try {
        const files = await listFiles(path);
        return {
          content: [{ type: "text", text: JSON.stringify(files, null, 2) }]
        };
      } catch (error) {
        return {
          content: [{ 
            type: "text", 
            text: error instanceof Error ? error.message : String(error)
          }],
          isError: true
        };
      }
    }
  • Zod input schema defining the 'path' parameter for the listFiles tool.
    {
      path: z.string().describe("The absolute path to the directory to list")
    },
  • Registration of the listFiles MCP tool using server.tool().
    server.tool(
      "listFiles",
      "Lists files and directories in a given path",
      {
        path: z.string().describe("The absolute path to the directory to list")
      },
      async ({ path }) => {
        try {
          const files = await listFiles(path);
          return {
            content: [{ type: "text", text: JSON.stringify(files, null, 2) }]
          };
        } catch (error) {
          return {
            content: [{ 
              type: "text", 
              text: error instanceof Error ? error.message : String(error)
            }],
            isError: true
          };
        }
      }
    );
  • Helper utility function that lists files in a directory with details using Node.js fs module.
    export async function listFiles(dirPath: string): Promise<any[]> {
      try {
        const files = await fs.readdir(dirPath);
        const fileDetails = await Promise.all(
          files.map(async (file) => {
            const filePath = path.join(dirPath, file);
            const stats = await fs.stat(filePath);
            return {
              name: file,
              path: filePath,
              isDirectory: stats.isDirectory(),
              size: stats.size,
              modified: stats.mtime.toISOString()
            };
          })
        );
        
        return fileDetails;
      } catch (error) {
        throw error;
      }
    }

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/auchenberg/claude-code-mcp'

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