search_files
Find files by name or extension using wildcard patterns in specified directories. Recursively searches and returns matching file paths for file management tasks.
Instructions
Recursively search for files matching a pattern in a directory. Supports wildcards (* and **) and returns matching file paths. Useful for finding files by name or extension.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes | The directory to search in | |
| pattern | Yes | The pattern to match (e.g., '*.ts', '**/*.json') |
Implementation Reference
- src/index.ts:482-503 (handler)Handler case within the CallToolRequestSchema request handler that parses input arguments, invokes the searchFiles helper function, and returns the search results in JSON format.case "search_files": { const searchPath = args.path as string; const pattern = args.pattern as string; const results = await searchFiles(searchPath, pattern); return { content: [ { type: "text", text: JSON.stringify( { matches: results, count: results.length, }, null, 2 ), }, ], }; }
- src/index.ts:194-206 (schema)Input schema defining the parameters for the search_files tool: path (directory to search) and pattern (glob-like pattern to match). Used for validation.type: "object", properties: { path: { type: "string", description: "The directory to search in", }, pattern: { type: "string", description: "The pattern to match (e.g., '*.ts', '**/*.json')", }, }, required: ["path", "pattern"], },
- src/index.ts:190-207 (registration)Registration of the search_files tool in the TOOLS array, which is returned by the ListToolsRequestSchema handler. Includes name, description, and schema.{ name: "search_files", description: "Recursively search for files matching a pattern in a directory. Supports wildcards (* and **) and returns matching file paths. Useful for finding files by name or extension.", inputSchema: { type: "object", properties: { path: { type: "string", description: "The directory to search in", }, pattern: { type: "string", description: "The pattern to match (e.g., '*.ts', '**/*.json')", }, }, required: ["path", "pattern"], }, },
- src/index.ts:211-245 (helper)Helper function implementing the core recursive file search logic: converts glob pattern to regex, traverses directory tree, and collects absolute paths of matching files.async function searchFiles( dirPath: string, pattern: string ): Promise<string[]> { const results: string[] = []; // Convert glob pattern to regex const regexPattern = pattern .replace(/\./g, "\\.") .replace(/\*\*/g, ".*") .replace(/\*/g, "[^/]*"); const regex = new RegExp(regexPattern); async function search(currentPath: string) { try { const entries = await fs.readdir(currentPath, { withFileTypes: true }); for (const entry of entries) { const fullPath = path.join(currentPath, entry.name); const relativePath = path.relative(dirPath, fullPath); if (entry.isDirectory()) { await search(fullPath); } else if (regex.test(relativePath)) { results.push(fullPath); } } } catch (error) { // Skip directories we can't access } } await search(dirPath); return results; }