Skip to main content
Glama
wonderwhy-er

Claude Desktop Commander MCP

list_directory

List and organize files and directories in a specified path with clear [FILE] and [DIR] prefixes. Use absolute paths for reliable results, as relative paths may fail. Works within allowed directories for structured file navigation.

Instructions

                    Get a detailed listing of all files and directories in a specified path.
                    
                    Use this instead of 'execute_command' with ls/dir commands.
                    Results distinguish between files and directories with [FILE] and [DIR] prefixes.
                    Only works within allowed directories.
                    
                    IMPORTANT: Always use absolute paths for reliability. Paths are automatically normalized regardless of slash direction. Relative paths may fail as they depend on the current working directory. Tilde paths (~/...) might not work in all contexts. Unless the user explicitly asks for relative paths, use absolute paths.
                    This command can be referenced as "DC: ..." or "use Desktop Commander to ..." in your instructions.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
pathYes

Implementation Reference

  • The main handler function for the 'list_directory' tool. Parses input arguments using ListDirectoryArgsSchema, calls the listDirectory helper function, formats the results as text, and returns a ServerResult.
    /**
     * Handle list_directory command
     */
    export async function handleListDirectory(args: unknown): Promise<ServerResult> {
        try {
            const startTime = Date.now();
            const parsed = ListDirectoryArgsSchema.parse(args);
            const entries = await listDirectory(parsed.path, parsed.depth);
            const duration = Date.now() - startTime;
    
            const resultText = entries.join('\n');
    
            return {
                content: [{ type: "text", text: resultText }],
            };
        } catch (error) {
            const errorMessage = error instanceof Error ? error.message : String(error);
            return createErrorResponse(errorMessage);
        }
    }
  • Zod schema defining the input parameters for the list_directory tool: 'path' (required string) and 'depth' (optional number, defaults to 2). Used for validation in the handler.
    export const ListDirectoryArgsSchema = z.object({
      path: z.string(),
      depth: z.number().optional().default(2),
    });
  • src/server.ts:421-454 (registration)
    Tool registration in the MCP listTools handler. Defines the tool name, description, input schema reference (ListDirectoryArgsSchema), and annotations for the protocol.
    {
        name: "list_directory",
        description: `
                Get a detailed listing of all files and directories in a specified path.
                
                Use this instead of 'execute_command' with ls/dir commands.
                Results distinguish between files and directories with [FILE] and [DIR] prefixes.
                
                Supports recursive listing with the 'depth' parameter (default: 2):
                - depth=1: Only direct contents of the directory
                - depth=2: Contents plus one level of subdirectories
                - depth=3+: Multiple levels deep
                
                CONTEXT OVERFLOW PROTECTION:
                - Top-level directory shows ALL items
                - Nested directories are limited to 100 items maximum per directory
                - When a nested directory has more than 100 items, you'll see a warning like:
                  [WARNING] node_modules: 500 items hidden (showing first 100 of 600 total)
                - This prevents overwhelming the context with large directories like node_modules
                
                Results show full relative paths from the root directory being listed.
                Example output with depth=2:
                [DIR] src
                [FILE] src/index.ts
                [DIR] src/tools
                [FILE] src/tools/filesystem.ts
                
                If a directory cannot be accessed, it will show [DENIED] instead.
                Only works within allowed directories.
                
                ${PATH_GUIDANCE}
                ${CMD_PREFIX_DESCRIPTION}`,
        inputSchema: zodToJsonSchema(ListDirectoryArgsSchema),
        annotations: {
  • Core helper function that performs the recursive directory listing. Validates path, reads directories recursively up to the specified depth, formats entries with [DIR]/[FILE] prefixes, handles access denials and item limits for performance.
    export async function listDirectory(dirPath: string, depth: number = 2): Promise<string[]> {
        const validPath = await validatePath(dirPath);
        const results: string[] = [];
    
        const MAX_NESTED_ITEMS = 100; // Maximum items to show per nested directory
    
        async function listRecursive(currentPath: string, currentDepth: number, relativePath: string = '', isTopLevel: boolean = true): Promise<void> {
            if (currentDepth <= 0) return;
    
            let entries;
            try {
                entries = await fs.readdir(currentPath, { withFileTypes: true });
            } catch (error) {
                // If we can't read this directory (permission denied), show as denied
                const displayPath = relativePath || path.basename(currentPath);
                results.push(`[DENIED] ${displayPath}`);
                return;
            }
    
            // Apply filtering for nested directories (not top level)
            const totalEntries = entries.length;
            let entriesToShow = entries;
            let filteredCount = 0;
    
            if (!isTopLevel && totalEntries > MAX_NESTED_ITEMS) {
                entriesToShow = entries.slice(0, MAX_NESTED_ITEMS);
                filteredCount = totalEntries - MAX_NESTED_ITEMS;
            }
    
            for (const entry of entriesToShow) {
                const fullPath = path.join(currentPath, entry.name);
                const displayPath = relativePath ? path.join(relativePath, entry.name) : entry.name;
    
                // Add this entry to results
                results.push(`${entry.isDirectory() ? "[DIR]" : "[FILE]"} ${displayPath}`);
    
                // If it's a directory and we have depth remaining, recurse
                if (entry.isDirectory() && currentDepth > 1) {
                    try {
                        // Validate the path before recursing
                        await validatePath(fullPath);
                        await listRecursive(fullPath, currentDepth - 1, displayPath, false);
                    } catch (error) {
                        // If validation fails or we can't access it, it will be marked as denied
                        // when we try to read it in the recursive call
                        continue;
                    }
                }
            }
    
            // Add warning message if items were filtered
            if (filteredCount > 0) {
                const displayPath = relativePath || path.basename(currentPath);
                results.push(`[WARNING] ${displayPath}: ${filteredCount} items hidden (showing first ${MAX_NESTED_ITEMS} of ${totalEntries} total)`);
            }
        }
    
        await listRecursive(validPath, depth, '', true);
        return results;
    }
Behavior4/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 effectively describes key traits: it distinguishes files and directories with prefixes, works only within allowed directories, and handles path normalization. However, it lacks details on error handling or performance aspects like rate limits, keeping it from a perfect score.

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

Conciseness4/5

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

The description is appropriately sized and front-loaded, starting with the core purpose. However, it includes minor redundancy in path advice and an unrelated note about referencing as 'DC:' or 'Desktop Commander,' which slightly reduces efficiency without adding critical value.

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 (one parameter, no output schema, no annotations), the description is largely complete, covering purpose, usage, behavior, and parameters. It could improve by detailing output format or error cases, but it provides sufficient context for effective use in most scenarios.

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

Parameters5/5

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

The input schema has 0% description coverage for the single parameter 'path,' but the description compensates fully by explaining its semantics: it must be a specified path, with detailed guidance on using absolute paths, normalization, and avoiding relative or tilde paths. This adds significant meaning beyond the basic 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 verb ('Get a detailed listing') and resource ('all files and directories in a specified path'), making the purpose specific. It distinguishes from sibling tools like 'execute_command' by explicitly stating to use this instead for ls/dir commands, ensuring it stands out in the context of the server.

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

Usage Guidelines5/5

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

The description provides explicit guidance on when to use this tool ('Use this instead of 'execute_command' with ls/dir commands') and includes alternatives, such as avoiding relative paths in favor of absolute ones. It also specifies exclusions like 'Only works within allowed directories,' offering clear context for proper 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/wonderwhy-er/DesktopCommanderMCP'

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