Skip to main content
Glama

search_files

Find files by name or extension using wildcard patterns. Recursively search directories to locate matching files and return their paths.

Instructions

Recursively search for files matching a pattern in a directory. Supports wildcards (* and **) and returns matching file paths. Useful for finding files by name or extension.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
pathYesThe directory to search in
patternYesThe pattern to match (e.g., '*.ts', '**/*.json')

Implementation Reference

  • Handler for the 'search_files' tool call: extracts path and pattern arguments, invokes the searchFiles helper, and returns JSON-formatted results including matches and count.
    case "search_files": { const searchPath = args.path as string; const pattern = args.pattern as string; const results = await searchFiles(searchPath, pattern); return { content: [ { type: "text", text: JSON.stringify( { matches: results, count: results.length, }, null, 2 ), }, ], }; }
  • Recursive file search implementation: converts glob-like pattern to regex, traverses directory tree, matches files against pattern, collects full paths of matches.
    async function searchFiles( dirPath: string, pattern: string ): Promise<string[]> { const results: string[] = []; // Convert glob pattern to regex const regexPattern = pattern .replace(/\./g, "\\.") .replace(/\*\*/g, ".*") .replace(/\*/g, "[^/]*"); const regex = new RegExp(regexPattern); async function search(currentPath: string) { try { const entries = await fs.readdir(currentPath, { withFileTypes: true }); for (const entry of entries) { const fullPath = path.join(currentPath, entry.name); const relativePath = path.relative(dirPath, fullPath); if (entry.isDirectory()) { await search(fullPath); } else if (regex.test(relativePath)) { results.push(fullPath); } } } catch (error) { // Skip directories we can't access } } await search(dirPath); return results; }
  • Input schema defining required 'path' (directory) and 'pattern' (glob-like string) parameters for the search_files tool.
    inputSchema: { type: "object", properties: { path: { type: "string", description: "The directory to search in", }, pattern: { type: "string", description: "The pattern to match (e.g., '*.ts', '**/*.json')", }, }, required: ["path", "pattern"], },
  • src/index.ts:190-207 (registration)
    Registration of the 'search_files' tool in the TOOLS array, including name, description, and input schema, used by ListToolsRequestSchema handler.
    { name: "search_files", description: "Recursively search for files matching a pattern in a directory. Supports wildcards (* and **) and returns matching file paths. Useful for finding files by name or extension.", inputSchema: { type: "object", properties: { path: { type: "string", description: "The directory to search in", }, pattern: { type: "string", description: "The pattern to match (e.g., '*.ts', '**/*.json')", }, }, required: ["path", "pattern"], }, },

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/koopatroopa787/first_mcp'

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