Skip to content

Commit e7b956e

Browse files
cliffhallclaude
andcommitted
fix(filesystem): prune the directory an excludePatterns "/**" glob names
minimatch matches "**/node_modules/**" only against paths below a node_modules, never the directory entry itself. So with that exclude, search_files and directory_tree still counted, realpath'd and readdir'd every node_modules (defeating the #4162 promise that an excluded slow folder is never touched), search_files could return the directory, and directory_tree listed it as empty. isExcludedPath now also matches a wildcard pattern ending in "/**" with that suffix removed, so the named directory is excluded and its subtree pruned before it is resolved or read, in both tools. Wildcard semantics are otherwise unchanged. Closes #5445 Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent 435dc70 commit e7b956e

6 files changed

Lines changed: 234 additions & 7 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+
An `excludePatterns` glob ending in `/**`, such as `**/node_modules/**`, now also excludes the directory it names in `search_files` and `directory_tree`. Before, the directory itself was still resolved and read, `search_files` could return it, and `directory_tree` listed it as an empty directory; now the whole subtree is pruned before it is touched.

‎src/filesystem/README.md‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,12 @@ Files inside the allowed directories are also [resources](https://modelcontextpr
253253
of that name at any depth and everything below it; it matches whole
254254
names, so `node_modules_old` is kept. A pattern containing `*` is a glob
255255
matched against the path relative to `path` as written: `*.log` excludes
256-
only entries directly under `path`, `**/*.log` at any depth. An excluded
257-
entry is skipped before it is resolved or read, so excluding a slow
258-
folder keeps the search out of it entirely.
256+
only entries directly under `path`, `**/*.log` at any depth. A glob
257+
ending in `/**` also excludes the directory it names, so
258+
`**/node_modules/**` leaves out every `node_modules` directory itself,
259+
not only what is inside it. An excluded entry is skipped before it is
260+
resolved or read, so excluding a slow folder keeps the search out of it
261+
entirely.
259262
- Glob-style pattern matching
260263
- Returns full paths to matches
261264
- Bounded: see [Slow and very large trees](#slow-and-very-large-trees)
@@ -271,7 +274,9 @@ Files inside the allowed directories are also [resources](https://modelcontextpr
271274
- `excludePatterns` (string[]): Entries to leave out, read the same way as
272275
`search_files`'s: a bare name excludes that name at any depth and the
273276
subtree below it, and a pattern containing `*` is a glob matched against
274-
the path relative to `path` as written.
277+
the path relative to `path` as written. A glob ending in `/**` also
278+
excludes the directory it names, so it is not listed, even as empty, and
279+
is never read.
275280
- `depth` (positive integer, optional): How many levels to descend. `1`
276281
lists only the entries directly in `path`, `2` adds their children, and
277282
so on. Omitted, the whole tree is returned
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
// An excludePatterns glob ending in "/**" prunes the directory it names (#5445).
2+
// minimatch matches "**/node_modules/**" only against paths below a
3+
// node_modules, never the directory entry itself, so search_files and
4+
// directory_tree used to realpath and readdir an excluded node_modules (breaking
5+
// the #4162 promise that an excluded slow folder is never touched) and could
6+
// still return it: search_files listed the directory, directory_tree showed it
7+
// empty. isExcludedPath now also matches such a pattern with the "/**" removed.
8+
// These tests drive both tools through a real client and assert the directory
9+
// is neither touched nor returned, then pin the predicate's rule directly.
10+
11+
import fs from "fs/promises";
12+
import path from "path";
13+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
14+
import type { Client } from "@modelcontextprotocol/client";
15+
import {
16+
call,
17+
connect,
18+
makeTempDir,
19+
quietStderr,
20+
textOf,
21+
type Connected,
22+
} from "./helpers.js";
23+
import { isExcludedPath } from "../lib.js";
24+
25+
interface TreeEntry {
26+
name: string;
27+
type: "file" | "directory";
28+
children?: TreeEntry[];
29+
}
30+
31+
let dir: string;
32+
let conn: Connected;
33+
let client: Client;
34+
35+
// dir/
36+
// index.js
37+
// node_modules/dep.js
38+
// node_modules_old/keep.js
39+
// packages/app/main.js
40+
// packages/app/node_modules/nested.js
41+
beforeEach(async () => {
42+
quietStderr();
43+
dir = await makeTempDir("mcp-fs-exclude-glob-");
44+
const files = [
45+
"index.js",
46+
"node_modules/dep.js",
47+
"node_modules_old/keep.js",
48+
"packages/app/main.js",
49+
"packages/app/node_modules/nested.js",
50+
];
51+
for (const file of files) {
52+
await fs.mkdir(path.dirname(path.join(dir, file)), { recursive: true });
53+
await fs.writeFile(path.join(dir, file), "");
54+
}
55+
conn = await connect([dir]);
56+
client = conn.client;
57+
});
58+
59+
afterEach(async () => {
60+
vi.restoreAllMocks();
61+
await conn.close();
62+
await fs.rm(dir, { recursive: true, force: true });
63+
});
64+
65+
const p = (posix: string) => path.join(...posix.split("/"));
66+
67+
/** The search_files result as paths relative to `dir`, sorted. */
68+
async function search(
69+
pattern: string,
70+
excludePatterns: string[],
71+
): Promise<string[]> {
72+
const result = await call(client, "search_files", {
73+
path: dir,
74+
pattern,
75+
excludePatterns,
76+
});
77+
expect(result.isError).toBeFalsy();
78+
return textOf(result)
79+
.split("\n")
80+
.map((line) => path.relative(dir, line))
81+
.sort();
82+
}
83+
84+
/** Every entry (files and directories) in a directory_tree result, relative to `dir`, sorted. */
85+
async function treeEntries(excludePatterns: string[]): Promise<string[]> {
86+
const result = await call(client, "directory_tree", {
87+
path: dir,
88+
excludePatterns,
89+
});
90+
expect(result.isError).toBeFalsy();
91+
const entries: string[] = [];
92+
const walk = (list: TreeEntry[], prefix: string) => {
93+
for (const entry of list) {
94+
const relative = path.join(prefix, entry.name);
95+
entries.push(relative);
96+
walk(entry.children ?? [], relative);
97+
}
98+
};
99+
walk(JSON.parse(textOf(result)) as TreeEntry[], "");
100+
return entries.sort();
101+
}
102+
103+
/** Run `action` and return every realpath/readdir target under a node_modules directory. */
104+
async function nodeModulesTouched(action: () => Promise<unknown>) {
105+
const realpath = vi.spyOn(fs, "realpath");
106+
const readdir = vi.spyOn(fs, "readdir");
107+
await action();
108+
const excluded = [
109+
path.join(dir, "node_modules"),
110+
path.join(dir, "packages", "app", "node_modules"),
111+
];
112+
return [...realpath.mock.calls, ...readdir.mock.calls]
113+
.map(([target]) => target.toString())
114+
.filter((target) =>
115+
excluded.some(
116+
(root) => target === root || target.startsWith(root + path.sep),
117+
),
118+
);
119+
}
120+
121+
const kept = [
122+
"index.js",
123+
"node_modules_old",
124+
"node_modules_old/keep.js",
125+
"packages",
126+
"packages/app",
127+
"packages/app/main.js",
128+
].map(p);
129+
130+
describe('excludePatterns ending in "/**" (#5445)', () => {
131+
it("search_files does not return the excluded directory or anything below it", async () => {
132+
expect(await search("**/*", ["**/node_modules/**"])).toEqual(kept);
133+
});
134+
135+
it("search_files does not return the directory when the pattern names it", async () => {
136+
const result = await call(client, "search_files", {
137+
path: dir,
138+
pattern: "**/node_modules",
139+
excludePatterns: ["**/node_modules/**"],
140+
});
141+
expect(result.isError).toBeFalsy();
142+
expect(textOf(result)).toBe("No matches found");
143+
});
144+
145+
it("directory_tree does not list the excluded directory, even as empty", async () => {
146+
expect(await treeEntries(["**/node_modules/**"])).toEqual(kept);
147+
});
148+
149+
it("search_files never realpaths or reads the excluded directory (#4162)", async () => {
150+
expect(
151+
await nodeModulesTouched(() => search("**/*", ["**/node_modules/**"])),
152+
).toEqual([]);
153+
});
154+
155+
it("directory_tree never realpaths or reads the excluded directory (#4162)", async () => {
156+
expect(
157+
await nodeModulesTouched(() => treeEntries(["**/node_modules/**"])),
158+
).toEqual([]);
159+
});
160+
161+
it("prunes only the directory a rooted pattern names", async () => {
162+
// "node_modules/**" names the root's node_modules only; the nested one stays.
163+
expect(await search("**/*.js", ["node_modules/**"])).toEqual(
164+
[
165+
"index.js",
166+
"node_modules_old/keep.js",
167+
"packages/app/main.js",
168+
"packages/app/node_modules/nested.js",
169+
].map(p),
170+
);
171+
});
172+
});
173+
174+
describe('isExcludedPath with a "/**" pattern', () => {
175+
it("matches the named directory as well as the paths below it", () => {
176+
expect(isExcludedPath("node_modules", ["**/node_modules/**"])).toBe(true);
177+
expect(
178+
isExcludedPath(p("packages/app/node_modules"), ["**/node_modules/**"]),
179+
).toBe(true);
180+
expect(
181+
isExcludedPath(p("node_modules/dep.js"), ["**/node_modules/**"]),
182+
).toBe(true);
183+
});
184+
185+
it("keeps whole-name matching and the rest of wildcard semantics", () => {
186+
expect(isExcludedPath("node_modules_old", ["**/node_modules/**"])).toBe(
187+
false,
188+
);
189+
expect(isExcludedPath(p("a/node_modules"), ["node_modules/**"])).toBe(
190+
false,
191+
);
192+
// Only a trailing "/**" is stripped: "**" alone still matches everything,
193+
// and a pattern without the suffix is matched as written.
194+
expect(isExcludedPath("anything", ["**"])).toBe(true);
195+
expect(isExcludedPath("node_modules", ["**/node_modules/*"])).toBe(false);
196+
});
197+
});

‎src/filesystem/__tests__/input-descriptions.test.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,12 @@ describe("excludePatterns, as the descriptions state it (#3669)", () => {
127127
path.join(dir, "pkg", "keep.txt"),
128128
]);
129129
});
130+
131+
it("a glob ending in '/**' leaves out the directory it names (#5445)", async () => {
132+
const result = await call(conn.client, "directory_tree", {
133+
path: dir,
134+
excludePatterns: ["**/node_modules/**"],
135+
});
136+
expect(textOf(result)).not.toContain("node_modules");
137+
});
130138
});

‎src/filesystem/lib.ts‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1192,6 +1192,12 @@ export async function headFile(
11921192
// prunes every `node_modules` directory, not only one at the root. Names are
11931193
// matched whole: `node_modules` does not exclude `node_modules_old`.
11941194
//
1195+
// A wildcard pattern ending in `/**` also excludes the directory it names
1196+
// (#5445): minimatch matches `**/node_modules/**` only against paths below a
1197+
// `node_modules`, never the directory entry itself, so without this the walk
1198+
// would still resolve and read the directory and only drop its children. The
1199+
// pattern is matched a second time with that suffix removed (`**/node_modules`).
1200+
//
11951201
// Callers check it before validating an entry, so an excluded subtree is never
11961202
// realpath'd or read (#4162).
11971203
export function isExcludedPath(
@@ -1200,7 +1206,13 @@ export function isExcludedPath(
12001206
): boolean {
12011207
return excludePatterns.some((pattern) => {
12021208
if (pattern.includes("*")) {
1203-
return minimatch(relativePath, pattern, { dot: true });
1209+
return (
1210+
minimatch(relativePath, pattern, { dot: true }) ||
1211+
(pattern.endsWith("/**") &&
1212+
minimatch(relativePath, pattern.slice(0, -"/**".length), {
1213+
dot: true,
1214+
}))
1215+
);
12041216
}
12051217
return (
12061218
minimatch(relativePath, pattern, { dot: true }) ||

‎src/filesystem/server.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ const DirectoryTreeArgsSchema = z.object({
158158
.describe(
159159
"Patterns for entries to leave out, with their contents, matched against each entry's path relative to path. " +
160160
"A pattern with no '*' matches that name at any depth ('node_modules' leaves out every node_modules directory); " +
161-
"a pattern with '*' is a glob matched as written.",
161+
"a pattern with '*' is a glob matched as written, and one ending in '/**' also leaves out the directory it names.",
162162
),
163163
depth: z
164164
.number()
@@ -200,7 +200,7 @@ const SearchFilesArgsSchema = z.object({
200200
.describe(
201201
"Patterns for entries to leave out, with their contents, matched against each entry's path relative to path. " +
202202
"A pattern with no '*' matches that name at any depth ('node_modules' leaves out every node_modules directory); " +
203-
"a pattern with '*' is a glob matched as written.",
203+
"a pattern with '*' is a glob matched as written, and one ending in '/**' also leaves out the directory it names.",
204204
),
205205
});
206206

0 commit comments

Comments
 (0)