listFiles
List files and directories within a specified path using the Claude Code MCP server, enabling efficient file management and organization.
Instructions
Lists files and directories in a given path
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes | The absolute path to the directory to list |
Implementation Reference
- src/server/tools.ts:88-103 (handler)MCP tool handler for listFiles: invokes the file listing helper and returns formatted JSON response or error.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 }; } }
- src/server/tools.ts:85-87 (schema)Input schema for the listFiles tool: requires 'path' parameter validated by Zod.{ path: z.string().describe("The absolute path to the directory to list") },
- src/server/tools.ts:82-104 (registration)Full registration of the listFiles MCP tool on the server, including name, description, schema, and handler.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 }; } } );
- src/utils/file.ts:41-62 (helper)Core helper function that implements file listing logic using Node.js fs module, returning file details including name, path, type, size, and modification time.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; } }