diff --git a/.changeset/filesystem-oldtext-description.md b/.changeset/filesystem-oldtext-description.md new file mode 100644 index 0000000000..433bc05b9b --- /dev/null +++ b/.changeset/filesystem-oldtext-description.md @@ -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. diff --git a/src/filesystem/README.md b/src/filesystem/README.md index 7435ff3606..280364c14e 100644 --- a/src/filesystem/README.md +++ b/src/filesystem/README.md @@ -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 diff --git a/src/filesystem/__tests__/edit-oldtext-description.test.ts b/src/filesystem/__tests__/edit-oldtext-description.test.ts new file mode 100644 index 0000000000..7bc889a06c --- /dev/null +++ b/src/filesystem/__tests__/edit-oldtext-description.test.ts @@ -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; + 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 { + 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", + ); + }); +}); diff --git a/src/filesystem/__tests__/server-tools.test.ts b/src/filesystem/__tests__/server-tools.test.ts index eadae875a4..2d3993bb99 100644 --- a/src/filesystem/__tests__/server-tools.test.ts +++ b/src/filesystem/__tests__/server-tools.test.ts @@ -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", diff --git a/src/filesystem/server.ts b/src/filesystem/server.ts index 4b7a59ef30..94f74fa9f5 100644 --- a/src/filesystem/server.ts +++ b/src/filesystem/server.ts @@ -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"), });