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/servers#251
This commit is contained in:
devin-ai-integration[bot]
2024-12-06 21:40:54 +00:00
parent eb7db5b6ec
commit b2b8f29816

View File

@@ -13,6 +13,7 @@ import os from 'os';
import { z } from "zod";
import { zodToJsonSchema } from "zod-to-json-schema";
import { diffLines, createTwoFilesPatch } from 'diff';
import minimatch from 'minimatch';
// Command line argument parsing
const args = process.argv.slice(2);
@@ -134,6 +135,7 @@ const MoveFileArgsSchema = z.object({
const SearchFilesArgsSchema = z.object({
path: z.string(),
pattern: z.string(),
excludePatterns: z.array(z.string()).optional().default([])
});
const GetFileInfoArgsSchema = z.object({
@@ -183,6 +185,7 @@ async function getFileStats(filePath: string): Promise<FileInfo> {
async function searchFiles(
rootPath: string,
pattern: string,
excludePatterns: string[] = []
): Promise<string[]> {
const results: string[] = [];
@@ -191,11 +194,22 @@ async function searchFiles(
for (const entry of entries) {
const fullPath = path.join(currentPath, entry.name);
try {
// Validate each path before processing
await validatePath(fullPath);
// Check if path matches any exclude pattern
const relativePath = path.relative(rootPath, fullPath);
const shouldExclude = excludePatterns.some(pattern => {
const globPattern = pattern.startsWith('*') ? pattern : `*/${pattern}/*`;
return minimatch(relativePath, globPattern, { dot: true });
});
if (shouldExclude) {
continue;
}
if (entry.name.toLowerCase().includes(pattern.toLowerCase())) {
results.push(fullPath);
}
@@ -574,4 +588,4 @@ async function runServer() {
runServer().catch((error) => {
console.error("Fatal error running server:", error);
process.exit(1);
});
});