Skip to content

Commit a92c30c

Browse files
feat: add excludePatterns to search_files for filtering directories
- Add excludePatterns property to SearchFilesArgsSchema - Modify searchFiles function to handle path exclusions - Add minimatch import for glob pattern matching This change allows excluding specific directories (like node_modules) from file searches to prevent context window overflow. Issue: modelcontextprotocol#251
1 parent 4c03c52 commit a92c30c

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/filesystem/index.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import os from 'os';
1313
import { z } from "zod";
1414
import { zodToJsonSchema } from "zod-to-json-schema";
1515
import { diffLines, createTwoFilesPatch } from 'diff';
16+
import minimatch from 'minimatch';
1617

1718
// Command line argument parsing
1819
const args = process.argv.slice(2);
@@ -134,6 +135,7 @@ const MoveFileArgsSchema = z.object({
134135
const SearchFilesArgsSchema = z.object({
135136
path: z.string(),
136137
pattern: z.string(),
138+
excludePatterns: z.array(z.string()).optional().default([])
137139
});
138140

139141
const GetFileInfoArgsSchema = z.object({
@@ -183,6 +185,7 @@ async function getFileStats(filePath: string): Promise<FileInfo> {
183185
async function searchFiles(
184186
rootPath: string,
185187
pattern: string,
188+
excludePatterns: string[] = []
186189
): Promise<string[]> {
187190
const results: string[] = [];
188191

@@ -191,11 +194,22 @@ async function searchFiles(
191194

192195
for (const entry of entries) {
193196
const fullPath = path.join(currentPath, entry.name);
194-
197+
195198
try {
196199
// Validate each path before processing
197200
await validatePath(fullPath);
198201

202+
// Check if path matches any exclude pattern
203+
const relativePath = path.relative(rootPath, fullPath);
204+
const shouldExclude = excludePatterns.some(pattern => {
205+
const globPattern = pattern.startsWith('*') ? pattern : `*/${pattern}/*`;
206+
return minimatch(relativePath, globPattern, { dot: true });
207+
});
208+
209+
if (shouldExclude) {
210+
continue;
211+
}
212+
199213
if (entry.name.toLowerCase().includes(pattern.toLowerCase())) {
200214
results.push(fullPath);
201215
}
@@ -574,4 +588,4 @@ async function runServer() {
574588
runServer().catch((error) => {
575589
console.error("Fatal error running server:", error);
576590
process.exit(1);
577-
});
591+
});

0 commit comments

Comments
 (0)