Skip to content

Commit d4e94ad

Browse files
authored
Merge pull request #5378 from modelcontextprotocol/v2/fix/5190-filesystem-unstatable-entry-size
fix(filesystem): show an unstattable entry's size as unknown in list_directory_with_sizes
2 parents 0dce8bd + 9192ea3 commit d4e94ad

4 files changed

Lines changed: 64 additions & 13 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@modelcontextprotocol/server-filesystem": patch
3+
---
4+
5+
`list_directory_with_sizes` no longer reports an entry it could not stat (unreadable, a dangling symlink, removed mid-listing, or a symlink leaving the allowed directories) as `0 B`. Such an entry is shown with size `unknown`, sorts last by size, and is left out of the combined size, which notes how many files it excludes.

‎src/filesystem/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ The server's directory access control follows this flow:
154154
- `sortBy` (string, optional): Sort entries by "name" or "size" (default: "name")
155155
- Returns detailed listing with file sizes and summary statistics
156156
- Shows total files, directories, and combined size
157+
- An entry that cannot be stat'd (unreadable, a dangling symlink, or a symlink leaving the allowed directories) is listed with size `unknown` and left out of the combined size
157158

158159
- **move_file**
159160
- Move or rename files and directories

‎src/filesystem/__tests__/server-tools.test.ts‎

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,15 +1227,38 @@ describe("list_directory_with_sizes", () => {
12271227
expect(files).toEqual(["b.txt", "a.txt"]);
12281228
});
12291229

1230-
it("lists an entry it cannot stat (a dangling symlink) as size 0", async () => {
1230+
it("lists an entry it cannot stat (a dangling symlink) as unknown size, left out of the total (#5190)", async () => {
12311231
await fs.symlink(path.join(dir, "gone"), path.join(dir, "dangling"));
12321232
const result = await call(client, "list_directory_with_sizes", {
12331233
path: dir,
12341234
});
12351235
expect(textOf(result)).toContain(
1236-
`[FILE] ${"dangling".padEnd(30)} ${"0 B".padStart(10)}`,
1236+
`[FILE] ${"dangling".padEnd(30)} ${"unknown".padStart(10)}`,
12371237
);
1238+
expect(textOf(result)).not.toContain(" 0 B");
12381239
expect(textOf(result)).toContain("Total: 3 files, 1 directories");
1240+
expect(textOf(result)).toContain(
1241+
"Combined size: 2.00 KB (excludes 1 file of unknown size)",
1242+
);
1243+
});
1244+
1245+
it("sorts entries of unknown size last when sorting by size (#5190)", async () => {
1246+
await fs.symlink(path.join(dir, "gone"), path.join(dir, "dangling"));
1247+
await fs.symlink(path.join(dir, "gone2"), path.join(dir, "dangling2"));
1248+
await fs.writeFile(path.join(dir, "empty.txt"), "");
1249+
const result = await call(client, "list_directory_with_sizes", {
1250+
path: dir,
1251+
sortBy: "size",
1252+
});
1253+
const files = textOf(result)
1254+
.split("\n")
1255+
.filter((line) => line.startsWith("[FILE]"))
1256+
.map((line) => line.split(/\s+/)[1]);
1257+
expect(files.slice(0, 3)).toEqual(["b.txt", "a.txt", "empty.txt"]);
1258+
expect(files.slice(3).sort()).toEqual(["dangling", "dangling2"]);
1259+
expect(textOf(result)).toContain(
1260+
"Combined size: 2.00 KB (excludes 2 files of unknown size)",
1261+
);
12391262
});
12401263

12411264
it("does not report the size of a symlink target outside the allowed directories", async () => {
@@ -1250,10 +1273,12 @@ describe("list_directory_with_sizes", () => {
12501273
path: dir,
12511274
});
12521275
expect(textOf(result)).toContain(
1253-
`[FILE] ${"link-out".padEnd(30)} ${"0 B".padStart(10)}`,
1276+
`[FILE] ${"link-out".padEnd(30)} ${"unknown".padStart(10)}`,
12541277
);
12551278
expect(textOf(result)).not.toContain("1.50 KB");
1256-
expect(textOf(result)).toContain("Combined size: 2.00 KB");
1279+
expect(textOf(result)).toContain(
1280+
"Combined size: 2.00 KB (excludes 1 file of unknown size)",
1281+
);
12571282
} finally {
12581283
await fs.rm(outside, { recursive: true, force: true });
12591284
}

‎src/filesystem/server.ts‎

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -601,9 +601,15 @@ export function createServer(
601601
const validPath = await validatePath(args.path, allowedDirectories);
602602
const entries = await fs.readdir(validPath, { withFileTypes: true });
603603

604-
// Get detailed information for each entry
604+
// Get detailed information for each entry; size is undefined when the
605+
// entry could not be stat'd
606+
type SizedEntry = {
607+
name: string;
608+
isDirectory: boolean;
609+
size: number | undefined;
610+
};
605611
const detailedEntries = await Promise.all(
606-
entries.map(async (entry) => {
612+
entries.map(async (entry): Promise<SizedEntry> => {
607613
const entryPath = path.join(validPath, entry.name);
608614
try {
609615
// fs.stat follows symlinks, so validate each entry first: a
@@ -618,14 +624,16 @@ export function createServer(
618624
name: entry.name,
619625
isDirectory: entry.isDirectory(),
620626
size: stats.size,
621-
mtime: stats.mtime,
622627
};
623628
} catch {
629+
// The entry could not be stat'd (unreadable, a dangling symlink,
630+
// removed since readdir, or outside the allowed directories).
631+
// Its size is unknown, not 0: inventing one would list it as an
632+
// empty file and count it in the total (#5190).
624633
return {
625634
name: entry.name,
626635
isDirectory: entry.isDirectory(),
627-
size: 0,
628-
mtime: new Date(0),
636+
size: undefined,
629637
};
630638
}
631639
}),
@@ -634,7 +642,8 @@ export function createServer(
634642
// Sort entries based on sortBy parameter
635643
const sortedEntries = [...detailedEntries].sort((a, b) => {
636644
if (args.sortBy === "size") {
637-
return b.size - a.size; // Descending by size
645+
// Descending by size; an entry of unknown size sorts last
646+
return (b.size ?? -1) - (a.size ?? -1);
638647
}
639648
// Default sort by name
640649
return a.name.localeCompare(b.name);
@@ -644,22 +653,33 @@ export function createServer(
644653
const formattedEntries = sortedEntries.map(
645654
(entry) =>
646655
`${entry.isDirectory ? "[DIR]" : "[FILE]"} ${entry.name.padEnd(30)} ${
647-
entry.isDirectory ? "" : formatSize(entry.size).padStart(10)
656+
entry.isDirectory
657+
? ""
658+
: (entry.size === undefined
659+
? "unknown"
660+
: formatSize(entry.size)
661+
).padStart(10)
648662
}`,
649663
);
650664

651665
// Add summary
652666
const totalFiles = detailedEntries.filter((e) => !e.isDirectory).length;
653667
const totalDirs = detailedEntries.filter((e) => e.isDirectory).length;
654668
const totalSize = detailedEntries.reduce(
655-
(sum, entry) => sum + (entry.isDirectory ? 0 : entry.size),
669+
(sum, entry) => sum + (entry.isDirectory ? 0 : (entry.size ?? 0)),
656670
0,
657671
);
672+
const unknownFiles = detailedEntries.filter(
673+
(e) => !e.isDirectory && e.size === undefined,
674+
).length;
658675

659676
const summary = [
660677
"",
661678
`Total: ${totalFiles} files, ${totalDirs} directories`,
662-
`Combined size: ${formatSize(totalSize)}`,
679+
`Combined size: ${formatSize(totalSize)}` +
680+
(unknownFiles > 0
681+
? ` (excludes ${unknownFiles} ${unknownFiles === 1 ? "file" : "files"} of unknown size)`
682+
: ""),
663683
];
664684

665685
const text = [...formattedEntries, ...summary].join("\n");

0 commit comments

Comments
 (0)