fix(filesystem): add optional depth parameter to list_directory#3882
fix(filesystem): add optional depth parameter to list_directory#3882lui62233 wants to merge 1 commit intomodelcontextprotocol:mainfrom
Conversation
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
olaservo
left a comment
There was a problem hiding this comment.
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:
-
Runtime crash:
listDirRecursivereturns an array of formatted strings, but the code after the call still tries to access.isDirectory()and.nameon each entry (which are Dirent methods, not string methods). This will throw at runtime. -
Infinite recursion when depth is omitted: When
depthisundefined(the default), the conditionmaxDepth === undefined || currentDepth < maxDepthis always true, causing the function to recurse into every subdirectory indefinitely. The tool should behave exactly as before whendepthis not provided (list only the immediate directory). -
Missing input schema update: The Zod schema is updated but the inline
inputSchemainregisterToolis not, so thedepthparameter won't be advertised to clients. -
No tests or CI: No tests are included, and CI doesn't appear to be running on this branch.
-
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.
Summary
Add optional
depthparameter to thelist_directorytool's Zod schema, inputSchema, and handler to support recursive directory listing.Problem
Some LLMs (e.g.,
lmstudio-community/gpt-oss-120b-MLX-8bit) generate adepthargument when callinglist_directory, but the schema rejected it as "params is not allowed to have the additional property 'depth'".Solution
depth: z.number().optional()toListDirectoryArgsSchemadepthto theinputSchemain the tool registrationdepth > 0Closes #2703