Skip to main content
Glama

search_files

Search for text within files in a directory using recursive scanning to locate specific content across your codebase.

Instructions

Search for text within files in a directory (recursive)

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
queryYesText to search for
pathNoDirectory to search in (defaults to projects directory)
file_patternNoFile pattern to match (e.g., '*.js', '*.py')

Implementation Reference

  • The core handler function that implements the logic 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, }, ], }; }
  • Input schema definition for the 'search_files' tool, specifying parameters: query (required), path (optional), file_pattern (optional).
    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 ListToolsRequestSchema response, including 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-179 (registration)
    Dispatch/registration of the 'search_files' handler in the CallToolRequestSchema switch statement, mapping tool call to the searchFiles method.
    case 'search_files': return await this.searchFiles(args.query, args.path, args.file_pattern);
  • Helper method used by searchFiles (and other tools) to resolve relative paths to absolute paths within the projects directory.
    resolvePath(inputPath) { if (!inputPath || inputPath === '.') { return PROJECTS_DIR; } if (path.isAbsolute(inputPath)) { return inputPath; } return path.join(PROJECTS_DIR, inputPath); }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/talentedmrweb/local-dev-bridge-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server