search_files
Recursively search files and directories by pattern within specified paths using Claude Desktop Commander MCP. Quickly locate matches in allowed subdirectories for efficient file management.
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 primary handler function for the 'search_files' tool. It recursively traverses the directory tree starting from rootPath, validates each path, and collects full paths of files/directories whose names match the pattern (case-insensitive).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 validating the input arguments for the search_files tool: 'path' (starting directory) and 'pattern' (search string). Used for both parsing in handler and generating JSON schema for tool registration.export const SearchFilesArgsSchema = z.object({ path: z.string(), pattern: z.string(), });
- src/server.ts:172-178 (registration)Tool registration in the MCP server's listTools response, defining the name, description, and input schema for search_files.{ 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 the MCP server's callTool request handler, which parses arguments using the schema and invokes the searchFiles implementation.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" }], };