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}`
                                }
                            ]
                        };
                    }
                })
        }
    )
Behavior1/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

Tool has no description.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness1/5

Is the description appropriately sized, front-loaded, and free of redundancy?

Tool has no description.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness1/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Tool has no description.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters1/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Tool has no description.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose1/5

Does the description clearly state what the tool does and how it differs from similar tools?

Tool has no description.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines1/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

Tool has no description.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

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