Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,13 @@ server.setRequestHandler(ListToolsRequestSchema, async () => {
- 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
Expand Down
26 changes: 22 additions & 4 deletions src/tools/filesystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -894,7 +894,9 @@ export async function listDirectory(dirPath: string, depth: number = 2): Promise
const validPath = await validatePath(dirPath);
const results: string[] = [];

async function listRecursive(currentPath: string, currentDepth: number, relativePath: string = ''): Promise<void> {
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;
Expand All @@ -907,7 +909,17 @@ export async function listDirectory(dirPath: string, depth: number = 2): Promise
return;
}

for (const entry of entries) {
// 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;

Expand All @@ -919,17 +931,23 @@ export async function listDirectory(dirPath: string, depth: number = 2): Promise
try {
// Validate the path before recursing
await validatePath(fullPath);
await listRecursive(fullPath, currentDepth - 1, displayPath);
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);
await listRecursive(validPath, depth, '', true);
return results;
}

Expand Down