search_files
Recursively search files and directories by pattern from a specified path using the Desktop Commander MCP server. Quickly locate data within 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)Core implementation of the search_files tool: recursively searches for files/directories matching the pattern starting from rootPath, respecting allowed directories.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 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-178 (registration)Registration of the search_files tool 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 (handler)Execution handler for search_files tool call: parses input, invokes searchFiles helper, formats and returns results.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" }], };