listfiles
Retrieves and lists files from a specified directory path using the MCP File Server, enabling AI models to access and manage local file systems efficiently.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes |
Implementation Reference
- src/index.js:177-253 (handler)Handler function for the 'listfiles' tool that reads directory contents with fs.readdir, fetches stats for each entry, categorizes files/directories, computes summary stats, and returns a markdown table of the listing.async ({ path: dirPath }) => { return await Sentry.startSpan( { name: "listFiles", op: "tool.listfiles", attributes: { 'directory.path': dirPath } }, async (span) => { try { // list directory contents and return as a markdown table const files = await fs.readdir(dirPath); // Update span with file count information span.setAttribute('directory.file_count', files.length); // Process files using async/await properly const fileDetails = await Promise.all( files.map(async (file) => { const filePath = path.join(dirPath, file); const stats = await fs.stat(filePath); return { name: file, size: stats.size, type: stats.isDirectory() ? 'Directory' : 'File' }; }) ); // Update span with directory composition stats const dirCount = fileDetails.filter(file => file.type === 'Directory').length; const fileCount = fileDetails.filter(file => file.type === 'File').length; const totalSize = fileDetails.reduce((sum, file) => sum + file.size, 0); span.setAttributes({ 'directory.count': dirCount, 'file.count': fileCount, 'directory.total_size': totalSize }); // Create markdown table const tableRows = fileDetails.map(file => `| ${file.name} | ${file.size} bytes | ${file.type} |` ).join('\n'); return { content: [ { type: "text", text: `| File Name | File Size | File Type |\n| --- | --- | --- |\n${tableRows}` } ] }; } catch (error) { // Add error information to the span before capturing exception span.setAttributes({ 'error.message': error.message, 'error.stack': error.stack }); span.setStatus("error"); // Capture and report the error to Sentry Sentry.captureException(error); // Return an error message return { content: [ { type: "text", text: `Error listing files: ${error.message}` } ] }; } }) }
- src/index.js:174-176 (schema)Zod input schema for the 'listfiles' tool, defining a single 'path' parameter as a string.{ path: z.string() },
- src/index.js:172-254 (registration)Registration of the 'listfiles' tool using McpServer's tool() method, specifying name, input schema, and handler function.server.tool( "listfiles", { path: z.string() }, async ({ path: dirPath }) => { return await Sentry.startSpan( { name: "listFiles", op: "tool.listfiles", attributes: { 'directory.path': dirPath } }, async (span) => { try { // list directory contents and return as a markdown table const files = await fs.readdir(dirPath); // Update span with file count information span.setAttribute('directory.file_count', files.length); // Process files using async/await properly const fileDetails = await Promise.all( files.map(async (file) => { const filePath = path.join(dirPath, file); const stats = await fs.stat(filePath); return { name: file, size: stats.size, type: stats.isDirectory() ? 'Directory' : 'File' }; }) ); // Update span with directory composition stats const dirCount = fileDetails.filter(file => file.type === 'Directory').length; const fileCount = fileDetails.filter(file => file.type === 'File').length; const totalSize = fileDetails.reduce((sum, file) => sum + file.size, 0); span.setAttributes({ 'directory.count': dirCount, 'file.count': fileCount, 'directory.total_size': totalSize }); // Create markdown table const tableRows = fileDetails.map(file => `| ${file.name} | ${file.size} bytes | ${file.type} |` ).join('\n'); return { content: [ { type: "text", text: `| File Name | File Size | File Type |\n| --- | --- | --- |\n${tableRows}` } ] }; } catch (error) { // Add error information to the span before capturing exception span.setAttributes({ 'error.message': error.message, 'error.stack': error.stack }); span.setStatus("error"); // Capture and report the error to Sentry Sentry.captureException(error); // Return an error message return { content: [ { type: "text", text: `Error listing files: ${error.message}` } ] }; } }) } )