listFiles
Retrieve a list of files from a specified directory to manage project files efficiently in Expo-based React Native development environments.
Instructions
List files in a directory
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| directoryPath | Yes | The path to the directory to list files from |
Implementation Reference
- src/file.ts:64-93 (handler)The main handler function implementing the listFiles tool. Normalizes the directory path, lists files and directories using fs.promises.readdir, constructs a list with name, isDirectory, and full path for each entry, and returns a JSON-formatted text response. Includes logging and error handling.
export async function listFiles(args: { directoryPath: string }, { log }: LogContext) { try { log.info(`Listing files in directory: ${args.directoryPath}`); const normalizedPath = path.normalize(args.directoryPath); const files = await fs.promises.readdir(normalizedPath, { withFileTypes: true }); const fileList = files.map((file) => ({ name: file.name, isDirectory: file.isDirectory(), path: path.join(normalizedPath, file.name), })); log.info( `Successfully listed ${fileList.length} files in ${normalizedPath}`, ); return { content: [ { type: "text", text: JSON.stringify(fileList, null, 2), }, ], }; } catch (error: any) { log.error(`Error listing files: ${error.message}`); throw new Error(`Failed to list files: ${error.message}`); } } - src/index.ts:37-46 (registration)Registration of the listFiles tool on the FastMCP server using addTool. Includes the tool name, description, Zod input schema for directoryPath parameter, and assignment of the handler function to execute.
addTool({ name: "listFiles", description: "List files in a directory", parameters: z.object({ directoryPath: z .string() .describe("The path to the directory to list files from"), }), execute: listFiles, }); - src/index.ts:40-44 (schema)Zod schema defining the input parameters for the listFiles tool: a single required string parameter directoryPath with description.
parameters: z.object({ directoryPath: z .string() .describe("The path to the directory to list files from"), }),