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:
parent
eb7db5b6ec
commit
b2b8f29816
1 changed files with 16 additions and 2 deletions
|
|
@ -13,6 +13,7 @@ import os from 'os';
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
import { zodToJsonSchema } from "zod-to-json-schema";
|
import { zodToJsonSchema } from "zod-to-json-schema";
|
||||||
import { diffLines, createTwoFilesPatch } from 'diff';
|
import { diffLines, createTwoFilesPatch } from 'diff';
|
||||||
|
import minimatch from 'minimatch';
|
||||||
|
|
||||||
// Command line argument parsing
|
// Command line argument parsing
|
||||||
const args = process.argv.slice(2);
|
const args = process.argv.slice(2);
|
||||||
|
|
@ -134,6 +135,7 @@ const MoveFileArgsSchema = z.object({
|
||||||
const SearchFilesArgsSchema = z.object({
|
const SearchFilesArgsSchema = z.object({
|
||||||
path: z.string(),
|
path: z.string(),
|
||||||
pattern: z.string(),
|
pattern: z.string(),
|
||||||
|
excludePatterns: z.array(z.string()).optional().default([])
|
||||||
});
|
});
|
||||||
|
|
||||||
const GetFileInfoArgsSchema = z.object({
|
const GetFileInfoArgsSchema = z.object({
|
||||||
|
|
@ -183,6 +185,7 @@ async function getFileStats(filePath: string): Promise<FileInfo> {
|
||||||
async function searchFiles(
|
async function searchFiles(
|
||||||
rootPath: string,
|
rootPath: string,
|
||||||
pattern: string,
|
pattern: string,
|
||||||
|
excludePatterns: string[] = []
|
||||||
): Promise<string[]> {
|
): Promise<string[]> {
|
||||||
const results: string[] = [];
|
const results: string[] = [];
|
||||||
|
|
||||||
|
|
@ -196,6 +199,17 @@ async function searchFiles(
|
||||||
// Validate each path before processing
|
// Validate each path before processing
|
||||||
await validatePath(fullPath);
|
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())) {
|
if (entry.name.toLowerCase().includes(pattern.toLowerCase())) {
|
||||||
results.push(fullPath);
|
results.push(fullPath);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue