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
5 changes: 5 additions & 0 deletions .changeset/filesystem-oldtext-description.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@modelcontextprotocol/server-filesystem": patch
---

`edit_file`'s `edits[].oldText` parameter description now states the matching rule the tool applies (exact apart from line endings, else whole lines ignoring leading and trailing whitespace), instead of "must match exactly", which contradicted the tool's own description.
2 changes: 1 addition & 1 deletion src/filesystem/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ Files inside the allowed directories are also [resources](https://modelcontextpr
- Inputs:
- `path` (string): File to edit
- `edits` (array): List of edit operations
- `oldText` (string): Text to search for (can be substring)
- `oldText` (string): Text to search for: an exact substring of the file (line endings aside) or, when there is none, a run of whole lines that differ only in leading and trailing whitespace (see Matching below)
- `newText` (string): Text to replace with
- `dryRun` (boolean): Preview changes without applying (default: false)
- Returns detailed diff and match information for dry runs, otherwise applies changes
Expand Down
79 changes: 79 additions & 0 deletions src/filesystem/__tests__/edit-oldtext-description.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// edit_file's edits[].oldText parameter description, as a client reads it from
// tools/list (#5688). It once said "must match exactly" while the tool's own
// description, and the matching it does, allow line endings to differ and fall
// back to whole lines compared without leading and trailing whitespace. A
// model reads both, so this pins the parameter description to the same rule
// and proves the fallback it describes actually applies.

import fs from "fs/promises";
import path from "path";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import {
call,
connect,
makeTempDir,
quietStderr,
type Connected,
} from "./helpers.js";

type SchemaNode = {
description?: string;
properties?: Record<string, SchemaNode>;
items?: SchemaNode;
};

let dir: string;
let conn: Connected;

beforeEach(async () => {
// The server logs its roots decision to stderr on every connect.
quietStderr();
dir = await makeTempDir("mcp-fs-oldtext-");
conn = await connect([dir]);
});

afterEach(async () => {
vi.restoreAllMocks();
await conn.close();
await fs.rm(dir, { recursive: true, force: true });
});

async function oldTextDescription(): Promise<string> {
const { tools } = await conn.client.listTools();
const editFile = tools.find((tool) => tool.name === "edit_file");
const schema = editFile?.inputSchema as SchemaNode | undefined;
const oldText = schema?.properties?.edits?.items?.properties?.oldText;
expect(typeof oldText?.description).toBe("string");
return oldText?.description ?? "";
}

describe("edit_file edits[].oldText description (#5688)", () => {
it("no longer claims oldText must match exactly", async () => {
expect(await oldTextDescription()).not.toMatch(/must match exactly/i);
});

it("states the line-ending and whole-line whitespace rule", async () => {
const description = await oldTextDescription();
expect(description).toContain("line endings (LF or CRLF) never matter");
expect(description).toContain(
"a run of whole lines matches if each line is equal once leading and " +
"trailing whitespace is ignored",
);
expect(description).toContain("newText then takes the file's indentation");
});

it("matches the behavior it describes: unindented lines edit indented ones", async () => {
// "x = 1\ny = 2" is not a substring of the file, so only the whole-line
// fallback can match it, and the replacement takes the file's indentation.
const file = path.join(dir, "sample.py");
await fs.writeFile(file, "def f():\n x = 1\n y = 2\n");
const result = await call(conn.client, "edit_file", {
path: file,
edits: [{ oldText: "x = 1\ny = 2", newText: "x = 3\ny = 4" }],
});
expect(result.isError).toBeFalsy();
expect(await fs.readFile(file, "utf8")).toBe(
"def f():\n x = 3\n y = 4\n",
);
});
});
7 changes: 6 additions & 1 deletion src/filesystem/__tests__/server-tools.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,12 @@ describe("server identity and tool list", () => {
properties: {
oldText: {
type: "string",
description: "Text to search for - must match exactly",
description:
"Text to search for. It matches the file's text exactly, except that line " +
"endings (LF or CRLF) never matter; when there is no exact match, a run of " +
"whole lines matches if each line is equal once leading and trailing " +
"whitespace is ignored, and newText then takes the file's indentation. " +
"Whitespace inside a line must match.",
},
newText: {
type: "string",
Expand Down
10 changes: 9 additions & 1 deletion src/filesystem/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,15 @@ const AppendFileArgsSchema = z.object({
});

const EditOperation = z.object({
oldText: z.string().describe("Text to search for - must match exactly"),
oldText: z
.string()
.describe(
"Text to search for. It matches the file's text exactly, except that line " +
"endings (LF or CRLF) never matter; when there is no exact match, a run of " +
"whole lines matches if each line is equal once leading and trailing " +
"whitespace is ignored, and newText then takes the file's indentation. " +
"Whitespace inside a line must match.",
),
newText: z.string().describe("Text to replace with"),
});

Expand Down