search_files
Find files and directories by pattern across subfolders from a specified starting path within permitted locations.
Instructions
Recursively search for files and directories matching a pattern. Searches through all subdirectories from the starting path. Only searches within allowed directories.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes | ||
| pattern | Yes |
Implementation Reference
- src/tools/filesystem.ts:107-135 (handler)The main handler function for the search_files tool. Recursively searches for files and directories matching the given pattern starting from rootPath, with path validation for security.export async function searchFiles(rootPath: string, pattern: string): Promise<string[]> { const results: string[] = []; async function search(currentPath: string) { const entries = await fs.readdir(currentPath, { withFileTypes: true }); for (const entry of entries) { const fullPath = path.join(currentPath, entry.name); try { await validatePath(fullPath); if (entry.name.toLowerCase().includes(pattern.toLowerCase())) { results.push(fullPath); } if (entry.isDirectory()) { await search(fullPath); } } catch (error) { continue; } } } const validPath = await validatePath(rootPath); await search(validPath); return results; }
- src/tools/schemas.ts:58-61 (schema)Zod schema defining the input arguments for the search_files tool: path (starting directory) and pattern (search string).export const SearchFilesArgsSchema = z.object({ path: z.string(), pattern: z.string(), });
- src/server.ts:172-179 (registration)Tool registration in the ListTools response, including name, description, and input schema reference.{ name: "search_files", description: "Recursively search for files and directories matching a pattern. " + "Searches through all subdirectories from the starting path. " + "Only searches within allowed directories.", inputSchema: zodToJsonSchema(SearchFilesArgsSchema), },
- src/server.ts:308-313 (registration)Dispatch handler in CallToolRequest that parses args, calls the searchFiles function, and formats the response.case "search_files": { const parsed = SearchFilesArgsSchema.parse(args); const results = await searchFiles(parsed.path, parsed.pattern); return { content: [{ type: "text", text: results.length > 0 ? results.join('\n') : "No matches found" }], };