Skip to main content
Glama
pwilkin

MCP File Editor Server

by pwilkin

list_files

Retrieve a detailed list of files and subdirectories within a specified directory path using the server's file management capabilities.

Instructions

List all files and subdirectories in a given directory.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
directory_pathYesAbsolute path to the directory to list

Implementation Reference

  • The execute handler for list_files tool. Validates the directory path, reads the directory contents using fs.readdirSync, determines file/dir type and size, formats output as list.
    execute: async ({ directory_path }) => {
      const absolutePath = validateAbsolutePath(directory_path, 'directory_path');
      validateDirectoryExists(absolutePath);
    
      try {
        const items = fs.readdirSync(absolutePath);
        const results: string[] = [];
    
        items.forEach(item => {
          const itemPath = path.join(absolutePath, item);
          const stats = fs.statSync(itemPath);
          const type = stats.isDirectory() ? '[DIR]' : '[FILE]';
          const size = stats.isFile() ? `(${stats.size} bytes)` : '';
          results.push(`${type} ${item} ${size}`.trim());
        });
    
        if (results.length === 0) {
          return `Directory "${absolutePath}" is empty.`;
        }
    
        return results.join('\n');
      } catch (error: any) {
        if (error instanceof UserError) throw error;
        throw new UserError(`Error listing directory "${absolutePath}": ${error.message}`);
      }
    }
  • Input schema using Zod: requires absolute directory_path as string.
    parameters: z.object({
      directory_path: z.string().describe('Absolute path to the directory to list')
    }),
  • src/index.ts:484-516 (registration)
    Registration of the list_files tool via server.addTool, including name, description, schema, and handler.
    server.addTool({
      name: 'list_files',
      description: 'List all files and subdirectories in a given directory.',
      parameters: z.object({
        directory_path: z.string().describe('Absolute path to the directory to list')
      }),
      execute: async ({ directory_path }) => {
        const absolutePath = validateAbsolutePath(directory_path, 'directory_path');
        validateDirectoryExists(absolutePath);
    
        try {
          const items = fs.readdirSync(absolutePath);
          const results: string[] = [];
    
          items.forEach(item => {
            const itemPath = path.join(absolutePath, item);
            const stats = fs.statSync(itemPath);
            const type = stats.isDirectory() ? '[DIR]' : '[FILE]';
            const size = stats.isFile() ? `(${stats.size} bytes)` : '';
            results.push(`${type} ${item} ${size}`.trim());
          });
    
          if (results.length === 0) {
            return `Directory "${absolutePath}" is empty.`;
          }
    
          return results.join('\n');
        } catch (error: any) {
          if (error instanceof UserError) throw error;
          throw new UserError(`Error listing directory "${absolutePath}": ${error.message}`);
        }
      }
    });
  • Helper function validateAbsolutePath used in list_files to ensure directory_path is absolute.
    export function validateAbsolutePath(filePath: string, parameterName: string = 'path'): string {
      if (!path.isAbsolute(filePath)) {
        throw new UserError(
          `The ${parameterName} must be an absolute path. You provided a relative path: "${filePath}". ` +
          `Please provide the full absolute path (e.g., "/home/user/file.txt" on Linux/Mac or "C:\\Users\\user\\file.txt" on Windows).`
        );
      }
      return filePath;
    }
  • Helper function validateDirectoryExists used in list_files to check if the directory exists and is accessible.
    export function validateDirectoryExists(dirPath: string): void {
      try {
        const stats = fs.statSync(dirPath);
        if (!stats.isDirectory()) {
          throw new UserError(
            `The path "${dirPath}" exists but is not a directory. Please ensure you're providing the path to a directory.`
          );
        }
      } catch (error: any) {
        if (error.code === 'ENOENT') {
          throw new UserError(
            `Directory not found: "${dirPath}". Please verify that the directory exists and the path is correct.`
          );
        } else if (error.code === 'EACCES') {
          throw new UserError(
            `Permission denied: Cannot access directory "${dirPath}". Please check directory permissions.`
          );
        } else {
          throw new UserError(
            `Error accessing directory "${dirPath}": ${error.message}`
          );
        }
      }
    }
Behavior2/5

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

With no annotations provided, the description carries the full burden of behavioral disclosure. It states the action but doesn't mention important behavioral aspects like whether it requires specific permissions, how it handles non-existent directories, what the return format looks like (e.g., list structure, pagination), or any rate limits. This leaves significant gaps for an agent to understand the tool's behavior.

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

Conciseness5/5

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

The description is a single, efficient sentence that directly states the tool's purpose without any unnecessary words. It's appropriately sized and front-loaded with the core functionality, making it easy for an agent to parse quickly.

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

Completeness2/5

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

For a tool with no annotations and no output schema, the description is incomplete. It doesn't explain what the output looks like (e.g., format, structure), error conditions, or behavioral constraints. Given the complexity of file system operations and the lack of structured data, more context is needed for an agent to use this tool effectively.

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

Parameters3/5

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

The schema description coverage is 100%, with the single parameter 'directory_path' clearly documented as 'Absolute path to the directory to list'. The description adds no additional parameter semantics beyond what the schema provides, so it meets the baseline for high schema coverage but doesn't add extra value.

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

Purpose4/5

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

The description clearly states the verb ('List') and resource ('all files and subdirectories') with the scope ('in a given directory'), making the purpose immediately understandable. However, it doesn't explicitly differentiate from sibling tools like 'search_directory' which might also list files but with filtering capabilities.

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

Usage Guidelines2/5

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

The description provides no guidance on when to use this tool versus alternatives like 'search_directory' or 'search_file'. It mentions the directory scope but doesn't indicate whether this is for unfiltered listing versus searching, or any prerequisites for usage.

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/pwilkin/mcp-file-edit'

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