Skip to content

Commit fbddfb0

Browse files
cliffhallclaude
andcommitted
fix(filesystem): return no lines for read_text_file head/tail 0
read_text_file tested head and tail for truthiness, so a count of 0 fell through to reading the whole file, and head: 0 with tail: 0 was not rejected. Compare against undefined instead, make tailFile return empty text for 0 lines (slice(-0) returned every line), and accept only non-negative integers for both counts. Closes #5184 Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent 446be48 commit fbddfb0

5 files changed

Lines changed: 76 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+
`read_text_file` (and the deprecated `read_file`) now treats `head: 0` or `tail: 0` as a request for zero lines and returns empty text, instead of the whole file. `head` and `tail` accept only non-negative integers, and `head` with `tail` is rejected even when either is 0.

‎src/filesystem/README.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ The server's directory access control follows this flow:
8282
- Read complete contents of a file as text
8383
- Inputs:
8484
- `path` (string)
85-
- `head` (number, optional): First N lines
86-
- `tail` (number, optional): Last N lines
85+
- `head` (non-negative integer, optional): First N lines; `0` returns no lines
86+
- `tail` (non-negative integer, optional): Last N lines; `0` returns no lines
8787
- Always treats the file as UTF-8 text regardless of extension
8888
- Cannot specify both `head` and `tail` simultaneously
8989

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

Lines changed: 54 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: Number.MAX_SAFE_INTEGER,
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: Number.MAX_SAFE_INTEGER,
217221
description:
218222
"If provided, returns only the first N lines of the file",
219223
},
@@ -449,12 +453,55 @@ describe("read_text_file and read_file", () => {
449453
});
450454
});
451455

452-
it("treats head: 0 as absent and returns the whole file", async () => {
453-
const result = await call(client, "read_text_file", {
454-
path: file,
455-
head: 0,
456+
describe("a zero count (#5184)", () => {
457+
it.each(["head", "tail"])("%s: 0 returns no lines", async (key) => {
458+
const result = await call(client, "read_text_file", {
459+
path: file,
460+
[key]: 0,
461+
});
462+
expect(result.isError).toBeFalsy();
463+
expect(result.structuredContent).toEqual({ content: "" });
464+
});
465+
466+
it.each(["head", "tail"])(
467+
"%s: 0 still reports a missing file",
468+
async (key) => {
469+
const result = await call(client, "read_text_file", {
470+
path: path.join(dir, "missing.txt"),
471+
[key]: 0,
472+
});
473+
expect(result.isError).toBe(true);
474+
expect(textOf(result)).toMatch(/^ENOENT: no such file or directory/);
475+
},
476+
);
477+
478+
it("rejects head: 0 and tail: 0 together", async () => {
479+
const result = await call(client, "read_text_file", {
480+
path: file,
481+
head: 0,
482+
tail: 0,
483+
});
484+
expect(result.isError).toBe(true);
485+
expect(textOf(result)).toBe(
486+
"Cannot specify both head and tail parameters simultaneously",
487+
);
488+
});
489+
490+
it.each([
491+
["head", -1],
492+
["tail", -1],
493+
["head", 1.5],
494+
["tail", 1.5],
495+
])("rejects %s: %s as invalid input", async (key, value) => {
496+
for (const tool of ["read_text_file", "read_file"]) {
497+
const result = await call(client, tool, {
498+
path: file,
499+
[key]: value,
500+
});
501+
expect(result.isError, tool).toBe(true);
502+
expect(textOf(result), tool).toMatch(/Input validation error/);
503+
}
456504
});
457-
expect(textOf(result)).toBe("one\ntwo\nthree\nfour\n");
458505
});
459506

460507
it("rejects head and tail together", async () => {

‎src/filesystem/lib.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -833,7 +833,8 @@ export async function tailFile(
833833
const stats = await fs.stat(filePath);
834834
const fileSize = stats.size;
835835

836-
if (fileSize === 0) return "";
836+
// Zero lines is an empty result; slice(-0) below would return every line.
837+
if (fileSize === 0 || numLines === 0) return "";
837838

838839
// Open file for reading
839840
const fileHandle = await fs.open(filePath, "r");

‎src/filesystem/server.ts‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,14 @@ const ReadTextFileArgsSchema = z.object({
5151
path: z.string(),
5252
tail: z
5353
.number()
54+
.int()
55+
.min(0)
5456
.optional()
5557
.describe("If provided, returns only the last N lines of the file"),
5658
head: z
5759
.number()
60+
.int()
61+
.min(0)
5862
.optional()
5963
.describe("If provided, returns only the first N lines of the file"),
6064
});
@@ -259,16 +263,18 @@ export function createServer(
259263
) => {
260264
const validPath = await validatePath(args.path, allowedDirectories);
261265

262-
if (args.head && args.tail) {
266+
// Compared against undefined, not tested for truthiness: a count of 0
267+
// asks for zero lines, not for the whole file (#5184).
268+
if (args.head !== undefined && args.tail !== undefined) {
263269
throw new Error(
264270
"Cannot specify both head and tail parameters simultaneously",
265271
);
266272
}
267273

268274
let content: string;
269-
if (args.tail) {
275+
if (args.tail !== undefined) {
270276
content = await tailFile(validPath, args.tail);
271-
} else if (args.head) {
277+
} else if (args.head !== undefined) {
272278
content = await headFile(validPath, args.head);
273279
} else {
274280
content = await readFileContent(validPath);
@@ -309,10 +315,14 @@ export function createServer(
309315
path: z.string(),
310316
tail: z
311317
.number()
318+
.int()
319+
.min(0)
312320
.optional()
313321
.describe("If provided, returns only the last N lines of the file"),
314322
head: z
315323
.number()
324+
.int()
325+
.min(0)
316326
.optional()
317327
.describe("If provided, returns only the first N lines of the file"),
318328
}),

0 commit comments

Comments
 (0)