|
| 1 | +// What the search_files description tells a model, checked against what the |
| 2 | +// tool does (#896, #3419). Models read "search for files matching a pattern" |
| 3 | +// as a content search (grep) and got "No matches found" for text that was in |
| 4 | +// the files, so the description now says the tool matches names, never |
| 5 | +// contents. These tests pin that wording and each matching rule it states, so |
| 6 | +// the description cannot drift from the behavior again. |
| 7 | + |
| 8 | +import fs from "fs/promises"; |
| 9 | +import path from "path"; |
| 10 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 11 | +import type { Client } from "@modelcontextprotocol/client"; |
| 12 | +import { |
| 13 | + call, |
| 14 | + connect, |
| 15 | + makeTempDir, |
| 16 | + quietStderr, |
| 17 | + textOf, |
| 18 | + type Connected, |
| 19 | +} from "./helpers.js"; |
| 20 | + |
| 21 | +let dir: string; |
| 22 | +let conn: Connected; |
| 23 | +let client: Client; |
| 24 | + |
| 25 | +beforeEach(async () => { |
| 26 | + quietStderr(); |
| 27 | + dir = await makeTempDir("mcp-fs-search-desc-"); |
| 28 | + await fs.mkdir(path.join(dir, "src", "config"), { recursive: true }); |
| 29 | + await fs.writeFile(path.join(dir, "notes.md"), "needle"); |
| 30 | + await fs.writeFile(path.join(dir, "src", "app.md"), ""); |
| 31 | + await fs.writeFile(path.join(dir, "config"), ""); |
| 32 | + await fs.writeFile(path.join(dir, "README.MD"), ""); |
| 33 | + conn = await connect([dir]); |
| 34 | + client = conn.client; |
| 35 | +}); |
| 36 | + |
| 37 | +afterEach(async () => { |
| 38 | + vi.restoreAllMocks(); |
| 39 | + await conn.close(); |
| 40 | + await fs.rm(dir, { recursive: true, force: true }); |
| 41 | +}); |
| 42 | + |
| 43 | +async function search(pattern: string): Promise<string[]> { |
| 44 | + const text = textOf( |
| 45 | + await call(client, "search_files", { path: dir, pattern }), |
| 46 | + ); |
| 47 | + return text.split("\n").sort(); |
| 48 | +} |
| 49 | + |
| 50 | +describe("search_files description (#896, #3419)", () => { |
| 51 | + it("says it matches names, not file contents", async () => { |
| 52 | + const { tools } = await client.listTools(); |
| 53 | + const description = tools.find( |
| 54 | + (t) => t.name === "search_files", |
| 55 | + )?.description; |
| 56 | + expect(description).toMatch( |
| 57 | + /^Find files and directories by NAME, not by content/, |
| 58 | + ); |
| 59 | + expect(description).toContain("never reads or searches inside files"); |
| 60 | + expect(description).toContain("relative to 'path'"); |
| 61 | + expect(description).not.toContain("working directory"); |
| 62 | + }); |
| 63 | + |
| 64 | + it("does not match a file's contents", async () => { |
| 65 | + expect(await search("**/needle")).toEqual(["No matches found"]); |
| 66 | + }); |
| 67 | + |
| 68 | + it("matches '*.ext' only directly under path", async () => { |
| 69 | + expect(await search("*.md")).toEqual([path.join(dir, "notes.md")]); |
| 70 | + }); |
| 71 | + |
| 72 | + it("matches '**/name' at any depth, the top level included", async () => { |
| 73 | + expect(await search("**/config")).toEqual([ |
| 74 | + path.join(dir, "config"), |
| 75 | + path.join(dir, "src", "config"), |
| 76 | + ]); |
| 77 | + }); |
| 78 | + |
| 79 | + it("matches case-sensitively", async () => { |
| 80 | + expect(await search("*.MD")).toEqual([path.join(dir, "README.MD")]); |
| 81 | + }); |
| 82 | +}); |
0 commit comments