Skip to content

Commit

Permalink
Merge pull request #256 from devin-open-source/devin/1733521206-add-e…
Browse files Browse the repository at this point in the history
…xclude-patterns-to-search-files

Add excludePatterns to search_files in filesystem server
  • Loading branch information
dsp-ant authored Dec 10, 2024
2 parents 79794fe + 245aece commit 0968e43
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 8 deletions.
26 changes: 25 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/filesystem/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ Node.js server implementing Model Context Protocol (MCP) for filesystem operatio
- Inputs:
- `path` (string): Starting directory
- `pattern` (string): Search pattern
- `excludePatterns` (string[]): Exclude any patterns. Glob formats are supported.
- Case-insensitive matching
- Returns full paths to matches

Expand Down
26 changes: 20 additions & 6 deletions src/filesystem/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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[] = [];

Expand All @@ -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.includes('*') ? pattern : `**/${pattern}/**`;
return minimatch(relativePath, globPattern, { dot: true });
});

if (shouldExclude) {
continue;
}

if (entry.name.toLowerCase().includes(pattern.toLowerCase())) {
results.push(fullPath);
}
Expand Down Expand Up @@ -522,7 +536,7 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
throw new Error(`Invalid arguments for search_files: ${parsed.error}`);
}
const validPath = await validatePath(parsed.data.path);
const results = await searchFiles(validPath, parsed.data.pattern);
const results = await searchFiles(validPath, parsed.data.pattern, parsed.data.excludePatterns);
return {
content: [{ type: "text", text: results.length > 0 ? results.join("\n") : "No matches found" }],
};
Expand All @@ -544,9 +558,9 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {

case "list_allowed_directories": {
return {
content: [{
type: "text",
text: `Allowed directories:\n${allowedDirectories.join('\n')}`
content: [{
type: "text",
text: `Allowed directories:\n${allowedDirectories.join('\n')}`
}],
};
}
Expand Down Expand Up @@ -574,4 +588,4 @@ async function runServer() {
runServer().catch((error) => {
console.error("Fatal error running server:", error);
process.exit(1);
});
});
4 changes: 3 additions & 1 deletion src/filesystem/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@
"@modelcontextprotocol/sdk": "0.5.0",
"diff": "^5.1.0",
"glob": "^10.3.10",
"minimatch": "^10.0.1",
"zod-to-json-schema": "^3.23.5"
},
"devDependencies": {
"@types/diff": "^5.0.9",
"@types/minimatch": "^5.1.2",
"@types/node": "^20.11.0",
"shx": "^0.3.4",
"typescript": "^5.3.3"
}
}
}

0 comments on commit 0968e43

Please sign in to comment.