search_files
Search for files in a given directory using a glob pattern, with optional exclusion patterns to refine results.
Instructions
Search for files matching a pattern
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes | Base directory to search from | |
| pattern | Yes | Search pattern (glob format) | |
| excludePatterns | No | Patterns to exclude from search (glob format) |
Implementation Reference
- src/index.ts:390-431 (handler)The main handler function 'searchFiles' that recursively searches directories for files matching a glob pattern, with support for exclude patterns. Uses fsPromises.readdir, minimatch for pattern matching, and validatePath for security.
async function searchFiles(rootPath: string, pattern: string, excludePatterns: string[] = []): Promise<string[]> { const results: string[] = []; async function search(currentPath: string) { const entries = await fsPromises.readdir(currentPath, { withFileTypes: true }); 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; } // Check if the path matches the search pattern if (minimatch(entry.name, pattern, { nocase: true })) { results.push(fullPath); } if (entry.isDirectory()) { await search(fullPath); } } catch (error) { // Skip invalid paths during search continue; } } } await search(rootPath); return results; } - src/index.ts:599-613 (handler)The switch-case handler in the CallToolRequestSchema that dispatches 'search_files' tool calls. Validates path, extracts excludePatterns, calls searchFiles, and returns results.
case "search_files": { const validPath = await validatePath(args.path); const excludePatterns = args.excludePatterns || []; const results = await searchFiles(validPath, args.pattern, excludePatterns); return { toolResult: { success: true, content: [{ type: "text", text: results.length > 0 ? results.join("\n") : "No matching files found" }], message: `Found ${results.length} matching files` } }; } - src/index.ts:360-384 (schema)Input schema definition for the 'search_files' tool registered with ListToolsRequestSchema. Defines path (string, required), pattern (string, required), and excludePatterns (string array, optional).
{ name: "search_files", description: "Search for files matching a pattern", inputSchema: { type: "object", properties: { path: { type: "string", description: "Base directory to search from" }, pattern: { type: "string", description: "Search pattern (glob format)" }, excludePatterns: { type: "array", items: { type: "string" }, description: "Patterns to exclude from search (glob format)" } }, required: ["path", "pattern"] } } - src/index.ts:277-387 (registration)Tool registration via server.setRequestHandler(ListToolsRequestSchema, ...). The 'search_files' tool is registered as one of several tools in the tools array (line 361).
server.setRequestHandler(ListToolsRequestSchema, async () => { return { tools: [ // Image generation tool { name: "generate_3d_cartoon", description: "Generates a 3D style cartoon image for kids based on the given prompt", inputSchema: { type: "object", properties: { prompt: { type: "string", description: "The prompt describing the 3D cartoon image to generate" }, fileName: { type: "string", description: "The name of the output file (without extension)" } }, required: ["prompt", "fileName"] } }, // File system tools { name: "read_file", description: "Read the contents of a file", inputSchema: { type: "object", properties: { path: { type: "string", description: "Path to the file to read" } }, required: ["path"] } }, { name: "write_file", description: "Write content to a file", inputSchema: { type: "object", properties: { path: { type: "string", description: "Path to the file to write" }, content: { type: "string", description: "Content to write to the file" } }, required: ["path", "content"] } }, { name: "list_directory", description: "List the contents of a directory", inputSchema: { type: "object", properties: { path: { type: "string", description: "Path to the directory to list" } }, required: ["path"] } }, { name: "create_directory", description: "Create a new directory", inputSchema: { type: "object", properties: { path: { type: "string", description: "Path to the directory to create" } }, required: ["path"] } }, { name: "search_files", description: "Search for files matching a pattern", inputSchema: { type: "object", properties: { path: { type: "string", description: "Base directory to search from" }, pattern: { type: "string", description: "Search pattern (glob format)" }, excludePatterns: { type: "array", items: { type: "string" }, description: "Patterns to exclude from search (glob format)" } }, required: ["path", "pattern"] } } ] }; });