Skip to content

Commit 31fda3e

Browse files
authored
Merge pull request #5620 from modelcontextprotocol/v2/feat/5553-filesystem-search-files-examples
feat(filesystem): say what an empty search_files result searched, and how to widen the pattern
2 parents 257d888 + bd89c54 commit 31fda3e

7 files changed

Lines changed: 62 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+
`search_files` no longer answers a search that matched nothing with a bare `No matches found`. The result still begins with those words, and now names the pattern and the directory searched, says the pattern is a case-sensitive glob over names relative to that directory (never file contents), and, for a pattern with no `**`, suggests the `**/` form that reaches subdirectories.

‎src/filesystem/README.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,10 @@ Files inside the allowed directories are also [resources](https://modelcontextpr
297297
entirely.
298298
- Glob-style pattern matching
299299
- Returns full paths to matches
300+
- When nothing matches, the result begins `No matches found` and goes on to
301+
name the pattern and the directory searched and to say how the pattern is
302+
read. For a pattern with no `**`, which only matches at one depth, it also
303+
suggests the `**/` form that matches at any depth
300304
- Bounded: see [Slow and very large trees](#slow-and-very-large-trees)
301305
- A subdirectory that cannot be read (no permission, say) is skipped, and the
302306
result says so: after the matches, a note that the search is incomplete

‎src/filesystem/__tests__/exclude-glob-prunes-dir.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ describe('excludePatterns ending in "/**" (#5445)', () => {
140140
excludePatterns: ["**/node_modules/**"],
141141
});
142142
expect(result.isError).toBeFalsy();
143-
expect(textOf(result)).toBe("No matches found");
143+
expect(textOf(result)).toMatch(/^No matches found for pattern /);
144144
});
145145

146146
it("directory_tree does not list the excluded directory, even as empty", async () => {

‎src/filesystem/__tests__/search-files-description.test.ts‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
// the files, so the description now says the tool matches names, never
55
// contents. These tests pin that wording and each matching rule it states, so
66
// the description cannot drift from the behavior again.
7+
//
8+
// They also pin what an empty result says (#5553): a bare "No matches found"
9+
// gave a model nothing to correct a pattern with, so it repeated
10+
// '*.ext' patterns that cannot reach a subdirectory. The empty result now
11+
// names the pattern and the directory, restates how the pattern is read, and
12+
// suggests the '**/' form when the pattern has no '**'.
713

814
import fs from "fs/promises";
915
import path from "path";
@@ -62,7 +68,32 @@ describe("search_files description (#896, #3419)", () => {
6268
});
6369

6470
it("does not match a file's contents", async () => {
65-
expect(await search("**/needle")).toEqual(["No matches found"]);
71+
expect(await search("**/needle")).toEqual([
72+
`No matches found for pattern '**/needle' under ${dir}. ` +
73+
"The pattern is a glob matched case-sensitively against each entry's path " +
74+
"relative to that directory: it matches names, never file contents.",
75+
]);
76+
});
77+
78+
it("suggests the '**/' form when an empty search's pattern has no '**'", async () => {
79+
const result = await call(client, "search_files", {
80+
path: dir,
81+
pattern: "app*.md",
82+
});
83+
expect(result.isError).toBeFalsy();
84+
const text = textOf(result);
85+
expect(text).toBe(
86+
`No matches found for pattern 'app*.md' under ${dir}. ` +
87+
"The pattern is a glob matched case-sensitively against each entry's path " +
88+
"relative to that directory: it matches names, never file contents. " +
89+
"It has no '**', so it only matches at one depth ('*.ext' only directly " +
90+
"under the directory); try '**/app*.md' to match at any depth.",
91+
);
92+
expect(result.structuredContent).toEqual({ content: text });
93+
// The suggested pattern is one that works.
94+
expect(await search("**/app*.md")).toEqual([
95+
path.join(dir, "src", "app.md"),
96+
]);
6697
});
6798

6899
it("matches '*.ext' only directly under path", async () => {

‎src/filesystem/__tests__/search-unreadable.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ describe("search_files with an unreadable subdirectory (#5189)", () => {
100100
});
101101
expect(result.isError).toBeFalsy();
102102
const [matches, note] = textOf(result).split("\n\n");
103-
expect(matches).toBe("No matches found");
103+
expect(matches).toMatch(/^No matches found for pattern /);
104104
const [heading, ...listed] = note.split("\n");
105105
expect(heading).toBe(
106106
"The search is incomplete: 2 directories could not be read, and any matches inside are missing:",

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2122,7 +2122,7 @@ describe("search_files", () => {
21222122
path: dir,
21232123
pattern: "*.none",
21242124
});
2125-
expect(textOf(result)).toBe("No matches found");
2125+
expect(textOf(result)).toMatch(/^No matches found for pattern '\*\.none' /);
21262126
});
21272127

21282128
it("silently skips a symlink that leaves the allowed directories", async () => {

‎src/filesystem/server.ts‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1063,7 +1063,24 @@ export function createServer(
10631063
},
10641064
},
10651065
);
1066-
let text = results.length > 0 ? results.join("\n") : "No matches found";
1066+
// An empty result says what was searched and how the pattern is read,
1067+
// and how to widen a pattern that cannot reach subdirectories: a bare
1068+
// "No matches found" left a model repeating the same '*.ext' pattern
1069+
// (#5553). It stays one paragraph, opening with those three words.
1070+
let text: string;
1071+
if (results.length > 0) {
1072+
text = results.join("\n");
1073+
} else {
1074+
text =
1075+
`No matches found for pattern '${args.pattern}' under ${args.path}. ` +
1076+
"The pattern is a glob matched case-sensitively against each entry's path " +
1077+
"relative to that directory: it matches names, never file contents.";
1078+
if (!args.pattern.includes("**")) {
1079+
text +=
1080+
" It has no '**', so it only matches at one depth ('*.ext' only directly " +
1081+
`under the directory); try '**/${args.pattern}' to match at any depth.`;
1082+
}
1083+
}
10671084
if (unreadable.length > 0) {
10681085
text +=
10691086
`\n\nThe search is incomplete: ${unreadable.length} ` +

0 commit comments

Comments
 (0)