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
| 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 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 }; } }
- src/server/tools.ts:85-87 (schema)Zod input schema defining the 'path' parameter for the listFiles tool.{ path: z.string().describe("The absolute path to the directory to list") },
- src/server/tools.ts:82-104 (registration)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 }; } } );
- src/utils/file.ts:41-62 (helper)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; } }