Skip to content

Commit cc4b040

Browse files
committed
fix(filesystem): return empty result on head/tail 0 and validate integer count
Signed-off-by: Power <[email protected]>
1 parent 82acf2a commit cc4b040

3 files changed

Lines changed: 47 additions & 12 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+
Return empty result for `read_text_file` when `head: 0` or `tail: 0`, and enforce non-negative integer schemas (#5184).

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

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -209,11 +209,15 @@ describe("server identity and tool list", () => {
209209
properties: {
210210
path: { type: "string" },
211211
tail: {
212-
type: "number",
212+
type: "integer",
213+
minimum: 0,
214+
maximum: 9007199254740991,
213215
description: "If provided, returns only the last N lines of the file",
214216
},
215217
head: {
216-
type: "number",
218+
type: "integer",
219+
minimum: 0,
220+
maximum: 9007199254740991,
217221
description:
218222
"If provided, returns only the first N lines of the file",
219223
},
@@ -365,22 +369,40 @@ describe("read_text_file and read_file", () => {
365369
expect(textOf(result)).toBe("four\n");
366370
});
367371

368-
it("treats head: 0 as absent and returns the whole file", async () => {
372+
it("returns an empty string when head is 0 (#5184)", async () => {
369373
const result = await call(client, "read_text_file", {
370374
path: file,
371375
head: 0,
372376
});
373-
expect(textOf(result)).toBe("one\ntwo\nthree\nfour\n");
377+
expect(textOf(result)).toBe("");
374378
});
375379

376-
it("rejects head and tail together", async () => {
380+
it("returns an empty string when tail is 0 (#5184)", async () => {
377381
const result = await call(client, "read_text_file", {
382+
path: file,
383+
tail: 0,
384+
});
385+
expect(textOf(result)).toBe("");
386+
});
387+
388+
it("rejects head and tail together, including when both are 0 (#5184)", async () => {
389+
const result1 = await call(client, "read_text_file", {
378390
path: file,
379391
head: 1,
380392
tail: 1,
381393
});
382-
expect(result.isError).toBe(true);
383-
expect(textOf(result)).toBe(
394+
expect(result1.isError).toBe(true);
395+
expect(textOf(result1)).toBe(
396+
"Cannot specify both head and tail parameters simultaneously",
397+
);
398+
399+
const result2 = await call(client, "read_text_file", {
400+
path: file,
401+
head: 0,
402+
tail: 0,
403+
});
404+
expect(result2.isError).toBe(true);
405+
expect(textOf(result2)).toBe(
384406
"Cannot specify both head and tail parameters simultaneously",
385407
);
386408
});

‎src/filesystem/server.ts‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,14 @@ const ReadTextFileArgsSchema = z.object({
4949
path: z.string(),
5050
tail: z
5151
.number()
52+
.int()
53+
.min(0)
5254
.optional()
5355
.describe("If provided, returns only the last N lines of the file"),
5456
head: z
5557
.number()
58+
.int()
59+
.min(0)
5660
.optional()
5761
.describe("If provided, returns only the first N lines of the file"),
5862
});
@@ -257,17 +261,17 @@ export function createServer(
257261
) => {
258262
const validPath = await validatePath(args.path, allowedDirectories);
259263

260-
if (args.head && args.tail) {
264+
if (args.head !== undefined && args.tail !== undefined) {
261265
throw new Error(
262266
"Cannot specify both head and tail parameters simultaneously",
263267
);
264268
}
265269

266270
let content: string;
267-
if (args.tail) {
268-
content = await tailFile(validPath, args.tail);
269-
} else if (args.head) {
270-
content = await headFile(validPath, args.head);
271+
if (args.tail !== undefined) {
272+
content = args.tail === 0 ? "" : await tailFile(validPath, args.tail);
273+
} else if (args.head !== undefined) {
274+
content = args.head === 0 ? "" : await headFile(validPath, args.head);
271275
} else {
272276
content = await readFileContent(validPath);
273277
}
@@ -307,10 +311,14 @@ export function createServer(
307311
path: z.string(),
308312
tail: z
309313
.number()
314+
.int()
315+
.min(0)
310316
.optional()
311317
.describe("If provided, returns only the last N lines of the file"),
312318
head: z
313319
.number()
320+
.int()
321+
.min(0)
314322
.optional()
315323
.describe("If provided, returns only the first N lines of the file"),
316324
}),

0 commit comments

Comments
 (0)