Skip to main content
Glama
codyde
by codyde

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
NameRequiredDescriptionDefault
pathYes

Implementation Reference

  • 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}`
                            }
                        ]
                    };
                }
            })
    }
  • 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}`
                                }
                            ]
                        };
                    }
                })
        }
    )
Install Server

Other Tools

Related Tools

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/codyde/mcp-file-tool'

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