Skip to main content
Glama
MrGNSS

Desktop Commander MCP

list_directory

View all files and folders in a specific directory path, with clear labels to distinguish between files [FILE] and directories [DIR].

Instructions

Get a detailed listing of all files and directories in a specified path. Results distinguish between files and directories with [FILE] and [DIR] prefixes. Only works within allowed directories.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
pathYes

Implementation Reference

  • The core handler function that implements the 'list_directory' tool logic. It validates the input path for security restrictions, reads the directory entries, and returns formatted list distinguishing files [FILE] and directories [DIR].
    export async function listDirectory(dirPath: string): Promise<string[]> {
        const validPath = await validatePath(dirPath);
        const entries = await fs.readdir(validPath, { withFileTypes: true });
        return entries.map((entry) => `${entry.isDirectory() ? "[DIR]" : "[FILE]"} ${entry.name}`);
    }
  • Zod input schema for the 'list_directory' tool, validating a single 'path' parameter.
    export const ListDirectoryArgsSchema = z.object({
      path: z.string(),
    });
  • src/server.ts:157-162 (registration)
    Tool registration in the MCP server's ListTools handler, defining name, description, and input schema for 'list_directory'.
    name: "list_directory",
    description:
      "Get a detailed listing of all files and directories in a specified path. " +
      "Results distinguish between files and directories with [FILE] and [DIR] prefixes. " +
      "Only works within allowed directories.",
    inputSchema: zodToJsonSchema(ListDirectoryArgsSchema),
  • src/server.ts:294-300 (registration)
    Server-side dispatch handler for the 'list_directory' tool call, parsing args, invoking the handler, and formatting response.
    case "list_directory": {
      const parsed = ListDirectoryArgsSchema.parse(args);
      const entries = await listDirectory(parsed.path);
      return {
        content: [{ type: "text", text: entries.join('\n') }],
      };
    }
  • Security helper function used by listDirectory to validate paths are within allowed directories, handles symlinks and non-existent files.
    export async function validatePath(requestedPath: string): Promise<string> {
        const expandedPath = expandHome(requestedPath);
        const absolute = path.isAbsolute(expandedPath)
            ? path.resolve(expandedPath)
            : path.resolve(process.cwd(), expandedPath);
            
        const normalizedRequested = normalizePath(absolute);
    
        // Check if path is within allowed directories
        const isAllowed = allowedDirectories.some(dir => normalizedRequested.startsWith(normalizePath(dir)));
        if (!isAllowed) {
            throw new Error(`Access denied - path outside allowed directories: ${absolute}`);
        }
    
        // Handle symlinks by checking their real path
        try {
            const realPath = await fs.realpath(absolute);
            const normalizedReal = normalizePath(realPath);
            const isRealPathAllowed = allowedDirectories.some(dir => normalizedReal.startsWith(normalizePath(dir)));
            if (!isRealPathAllowed) {
                throw new Error("Access denied - symlink target outside allowed directories");
            }
            return realPath;
        } catch (error) {
            // For new files that don't exist yet, verify parent directory
            const parentDir = path.dirname(absolute);
            try {
                const realParentPath = await fs.realpath(parentDir);
                const normalizedParent = normalizePath(realParentPath);
                const isParentAllowed = allowedDirectories.some(dir => normalizedParent.startsWith(normalizePath(dir)));
                if (!isParentAllowed) {
                    throw new Error("Access denied - parent directory outside allowed directories");
                }
                return absolute;
            } catch {
                throw new Error(`Parent directory does not exist: ${parentDir}`);
            }
        }
    }
Behavior3/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 reveals important behavioral traits: the output format includes [FILE] and [DIR] prefixes for distinction, and there's a constraint about allowed directories. However, it doesn't disclose other potential behaviors like error conditions, permission requirements, or whether this is a read-only operation (though implied by 'Get').

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 perfectly concise with three sentences that each earn their place: first states the core purpose, second explains output formatting, third adds critical constraint. It's front-loaded with the main functionality and wastes no words.

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

Completeness4/5

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

Given the tool's moderate complexity (single parameter, no output schema, no annotations), the description is quite complete. It covers purpose, output format, and operational constraints. The main gap is lack of explicit mention about whether this is a read-only operation, though that's somewhat implied. For a listing tool, this provides sufficient context for an agent to use it correctly.

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

Parameters4/5

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

The schema has 0% description coverage for its single parameter 'path', but the description compensates by explaining what the parameter represents ('a specified path') and adding crucial context about its constraints ('Only works within allowed directories'). This provides meaningful semantic information beyond the bare schema.

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

Purpose5/5

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

The description clearly states the tool's purpose with specific verbs ('Get a detailed listing') and resource ('files and directories in a specified path'). It distinguishes from siblings like 'search_files' by focusing on comprehensive listing rather than searching, and from 'list_allowed_directories' by operating within directories rather than listing allowed ones.

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

Usage Guidelines4/5

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

The description provides clear context about when to use this tool ('in a specified path') and includes an important constraint ('Only works within allowed directories'). However, it doesn't explicitly mention when NOT to use it or name specific alternatives among the many sibling tools available.

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

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/MrGNSS/ClaudeDesktopCommander'

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