search_files
Search for text within files across directories using file patterns to locate specific content in your development projects.
Instructions
Search for text within files in a directory (recursive)
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes | Text to search for | |
| path | No | Directory to search in (defaults to projects directory) | |
| file_pattern | No | File pattern to match (e.g., '*.js', '*.py') |
Implementation Reference
- index.js:301-338 (handler)The handler function for the search_files tool. It resolves the search path, uses glob to find matching files, reads each file, searches lines case-insensitively for the query, and returns formatted results.async searchFiles(query, searchPath, filePattern = '*') { const resolvedPath = this.resolvePath(searchPath || '.'); const pattern = path.join(resolvedPath, '**', filePattern); const files = await glob(pattern, { ignore: ['**/node_modules/**', '**/.git/**'], nodir: true, }); const results = []; for (const file of files) { try { const content = await fs.readFile(file, 'utf-8'); const lines = content.split('\n'); lines.forEach((line, index) => { if (line.toLowerCase().includes(query.toLowerCase())) { results.push(`${file}:${index + 1}:${line.trim()}`); } }); } catch (error) { // Skip files that can't be read } } const output = results.length > 0 ? `Search results for "${query}" in ${resolvedPath}:\n\n${results.slice(0, 100).join('\n')}` : `No results found for "${query}" in ${resolvedPath}`; return { content: [ { type: 'text', text: output, }, ], }; }
- index.js:136-153 (schema)Input schema defining the parameters for the search_files tool: query (required string), optional path and file_pattern.inputSchema: { type: 'object', properties: { query: { type: 'string', description: 'Text to search for', }, path: { type: 'string', description: 'Directory to search in (defaults to projects directory)', }, file_pattern: { type: 'string', description: "File pattern to match (e.g., '*.js', '*.py')", }, }, required: ['query'], },
- index.js:133-154 (registration)Registration of the search_files tool in the ListToolsRequest handler, providing name, description, and input schema.{ name: 'search_files', description: 'Search for text within files in a directory (recursive)', inputSchema: { type: 'object', properties: { query: { type: 'string', description: 'Text to search for', }, path: { type: 'string', description: 'Directory to search in (defaults to projects directory)', }, file_pattern: { type: 'string', description: "File pattern to match (e.g., '*.js', '*.py')", }, }, required: ['query'], }, },
- index.js:178-180 (registration)Dispatch registration in the CallToolRequest switch statement that maps the tool call to the searchFiles handler method.case 'search_files': return await this.searchFiles(args.query, args.path, args.file_pattern);