Skip to content

fix(filesystem): add optional depth parameter to list_directory#3882

Open
lui62233 wants to merge 1 commit intomodelcontextprotocol:mainfrom
lui62233:fix-list-directory-depth
Open

fix(filesystem): add optional depth parameter to list_directory#3882
lui62233 wants to merge 1 commit intomodelcontextprotocol:mainfrom
lui62233:fix-list-directory-depth

Conversation

@lui62233
Copy link
Copy Markdown

@lui62233 lui62233 commented Apr 9, 2026

Summary

Add optional depth parameter to the list_directory tool's Zod schema, inputSchema, and handler to support recursive directory listing.

Problem

Some LLMs (e.g., lmstudio-community/gpt-oss-120b-MLX-8bit) generate a depth argument when calling list_directory, but the schema rejected it as "params is not allowed to have the additional property 'depth'".

Solution

  1. Added depth: z.number().optional() to ListDirectoryArgsSchema
  2. Added depth to the inputSchema in the tool registration
  3. Updated handler to perform recursive listing when depth > 0

Closes #2703

Add optional depth parameter to list_directory tool to support
recursive directory listing. Some LLMs generate a depth argument
for directory listing which was previously rejected as an
"additional property" error.

Fixes: modelcontextprotocol#2703
Copy link
Copy Markdown
Member

@olaservo olaservo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the idea — a depth parameter for list_directory could be useful. However, the current implementation has some issues that need to be addressed:

  1. Runtime crash: listDirRecursive returns an array of formatted strings, but the code after the call still tries to access .isDirectory() and .name on each entry (which are Dirent methods, not string methods). This will throw at runtime.

  2. Infinite recursion when depth is omitted: When depth is undefined (the default), the condition maxDepth === undefined || currentDepth < maxDepth is always true, causing the function to recurse into every subdirectory indefinitely. The tool should behave exactly as before when depth is not provided (list only the immediate directory).

  3. Missing input schema update: The Zod schema is updated but the inline inputSchema in registerTool is not, so the depth parameter won't be advertised to clients.

  4. No tests or CI: No tests are included, and CI doesn't appear to be running on this branch.

  5. No depth cap: Consider adding a maximum depth limit (e.g., 10) to prevent a runaway traversal on large filesystems.


This review was assisted by Claude Code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bug] list_directory params is not allowed to have the additional property depth

2 participants