Skip to content

Commit 4441e23

Browse files
cliffhallclaude
andcommitted
fix(filesystem): say search_files matches names, not contents
Models read "search for files and directories matching a pattern" as a content search and called search_files to grep, getting "No matches found" for text that was in the files. The description now opens with "by NAME, not by content", says the glob is matched case-sensitively against each entry's path relative to 'path' (it said the working directory), and gives '**/name' as the any-depth form. The README entry says the same. A new test pins the wording and each matching rule it states. Closes #896 Closes #3419 Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent 446be48 commit 4441e23

4 files changed

Lines changed: 95 additions & 5 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+
The `search_files` description now says plainly that it finds files and directories by name, not by content, and that its glob is matched case-sensitively against each entry's path relative to `path` (not the working directory), so a model no longer reaches for it to grep file contents.

‎src/filesystem/README.md‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,13 @@ The server's directory access control follows this flow:
169169
and fails with an error; move its files one at a time instead
170170

171171
- **search_files**
172-
- Recursively search for files/directories that match or do not match patterns
172+
- Recursively find files/directories by name (a glob over their paths), not
173+
by content: it never reads or searches inside files
173174
- Inputs:
174175
- `path` (string): Starting directory
175-
- `pattern` (string): Search pattern
176+
- `pattern` (string): Glob matched, case-sensitively, against each entry's
177+
path relative to `path`: `*.ext` matches entries directly under `path`,
178+
`**/*.ext` or `**/name` matches at any depth
176179
- `excludePatterns` (string[]): Exclude any patterns. An excluded entry is
177180
skipped before it is resolved or read, so excluding a slow folder keeps
178181
the search out of it entirely.
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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+
});

‎src/filesystem/server.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -752,9 +752,9 @@ export function createServer(
752752
{
753753
title: "Search Files",
754754
description:
755-
"Recursively search for files and directories matching a pattern. " +
756-
"The patterns should be glob-style patterns that match paths relative to the working directory. " +
757-
"Use pattern like '*.ext' to match files in current directory, and '**/*.ext' to match files in all subdirectories. " +
755+
"Find files and directories by NAME, not by content: this tool never reads or searches inside files. " +
756+
"It walks the tree under 'path' and matches a glob pattern against each entry's path relative to 'path' (case-sensitive). " +
757+
"Use '*.ext' to match entries directly under 'path', and '**/*.ext' or '**/name' to match at any depth. " +
758758
"Returns full paths to all matching items. Great for finding files when you don't know their exact location. " +
759759
"Only searches within allowed directories.",
760760
inputSchema: jsonSchema2020(SearchFilesArgsSchema.shape),

0 commit comments

Comments
 (0)