search_files
Search files in a directory using a pattern. Specify the path and pattern to retrieve matching files.
Instructions
Search files by pattern
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes | ||
| pattern | Yes |
Implementation Reference
- src/filesystem_tools.js:339-392 (handler)The actual searchFiles function that performs the file search using glob pattern matching. Takes a searchPath, pattern, and allowedDirs; validates path access; uses globSync with '**/<pattern>' to find matching files/directories; returns results with relative paths and types.
function searchFiles(searchPath, pattern, allowedDirs) { try { if (!searchPath) { return { success: false, message: 'No search path provided' }; } if (!pattern) { return { success: false, message: 'No search pattern provided' }; } if (!isPathAllowed(searchPath, allowedDirs)) { return { success: false, message: `Access to path "${searchPath}" is not allowed` }; } const resolvedPath = path.resolve(searchPath); // Check if directory exists if (!fs.existsSync(resolvedPath)) { return { success: false, message: `Directory not found: ${searchPath}` }; } // Check if it's a directory const stats = fs.statSync(resolvedPath); if (!stats.isDirectory()) { return { success: false, message: `Path is not a directory: ${searchPath}` }; } // Use glob to search for files const searchPattern = path.join(resolvedPath, '**', pattern); const matches = globSync(searchPattern, { nodir: false }); // Convert absolute paths back to relative paths const results = matches.map(match => ({ path: path.relative(process.cwd(), match), type: fs.statSync(match).isDirectory() ? 'directory' : 'file' })); // Create content string for compatibility const content = results.length > 0 ? `Found ${results.length} files matching "${pattern}":\n${results.map(r => `${r.type === 'directory' ? '[DIR]' : '[FILE]'} ${r.path}`).join('\n')}` : `No files found matching "${pattern}" in ${searchPath}`; return { success: true, results, count: results.length, pattern, content // Add content field for compatibility }; } catch (error) { logger.error(`Error searching files: ${error.message}`); return { success: false, message: `Error searching files: ${error.message}` }; } } - src/mcp/server.js:90-90 (schema)MCP tool registration/schema for 'search_files': describes it as 'Search files by pattern', requires 'path' (string) and 'pattern' (string) as input parameters.
{ name:'search_files', description:'Search files by pattern', inputSchema:{ type:'object', properties:{ path:{type:'string'}, pattern:{type:'string'} }, required:['path','pattern'] } }, - src/mcp/server.js:238-239 (registration)The tools/call handler for 'search_files' that delegates to filesystemTools.searchFiles(args.path, args.pattern, allowedDirectories). This is the dispatch point in the MCP server.
case 'search_files': data = filesystemTools.searchFiles(args.path, args.pattern, allowedDirectories); - src/filesystem_tools.js:740-747 (helper)Export of the searchFiles function in module.exports from filesystem_tools.js.
module.exports = { readFile, readMultipleFiles, writeFile, listDirectory, createDirectory, moveFile, searchFiles, - macOS Spotlight-based file search using AppleScript (mdfind command) - a different searchFiles implementation for macOS Spotlight.
searchFiles: (query) => ` do shell script "mdfind " & quoted form of "${query}" `,