From 7d36dccf3294077ff19ec7fb56f5db98534870a1 Mon Sep 17 00:00:00 2001 From: TTClaw Date: Thu, 9 Apr 2026 21:28:19 +0800 Subject: [PATCH] fix(filesystem): add optional depth parameter to list_directory 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: #2703 --- src/filesystem/index.ts | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/filesystem/index.ts b/src/filesystem/index.ts index 7b67e63e58..7718e742a5 100644 --- a/src/filesystem/index.ts +++ b/src/filesystem/index.ts @@ -132,6 +132,7 @@ const CreateDirectoryArgsSchema = z.object({ const ListDirectoryArgsSchema = z.object({ path: z.string(), + depth: z.number().optional().describe("Recursive depth for directory listing (1 = one level deep)"), }); const ListDirectoryWithSizesArgsSchema = z.object({ @@ -433,8 +434,26 @@ server.registerTool( annotations: { readOnlyHint: true } }, async (args: z.infer) => { + + + async function listDirRecursive(dirPath, currentDepth, maxDepth) { + const entries = await fs.readdir(dirPath, { withFileTypes: true }); + const results = []; + + for (const entry of entries) { + const fullPath = path.join(dirPath, entry.name); + const prefix = currentDepth > 0 ? " ".repeat(currentDepth) : ""; + results.push(`${prefix}${entry.isDirectory() ? "[DIR]" : "[FILE]"} ${entry.name}`); + + if (entry.isDirectory() && (maxDepth === undefined || currentDepth < maxDepth)) { + const subEntries = await listDirRecursive(fullPath, currentDepth + 1, maxDepth); + results.push(...subEntries); + } + } + return results; + } const validPath = await validatePath(args.path); - const entries = await fs.readdir(validPath, { withFileTypes: true }); + const entries = await listDirRecursive(validPath, 0, args.depth); const formatted = entries .map((entry) => `${entry.isDirectory() ? "[DIR]" : "[FILE]"} ${entry.name}`) .join("\n");