find_files
Search for files in directories using glob patterns like '.ts' or 'index.' to quickly locate matching filenames.
Instructions
Find files by name glob pattern (e.g. '.ts', 'index.').
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| directory | Yes | Directory to search in | |
| pattern | Yes | Glob pattern for filename (e.g. '*.ts') | |
| max_results | No | Max results to return (default 200) | |
| workspace_root | No | Workspace root directory (optional) |
Implementation Reference
- src/index.ts:135-162 (handler)Core handler function that recursively walks directories, filters out hidden dirs/node_modules/dist, and matches filenames against a glob pattern.
async function findFiles( dir: string, pattern: string, maxResults = 200 ): Promise<string[]> { const results: string[] = []; async function walk(current: string) { if (results.length >= maxResults) return; let entries; try { entries = await readdir(current, { withFileTypes: true }); } catch { return; } for (const entry of entries) { if (results.length >= maxResults) return; if (entry.name.startsWith(".") || entry.name === "node_modules" || entry.name === "dist") continue; const full = join(current, entry.name); if (entry.isDirectory()) { await walk(full); } else if (matchGlob(entry.name, pattern)) { results.push(full); } } } await walk(dir); return results; } - src/index.ts:275-288 (schema)Tool definition/input schema for find_files, declaring directory, pattern, max_results, workspace_root parameters.
{ name: "find_files", description: "Find files by name glob pattern (e.g. '*.ts', 'index.*').", inputSchema: { type: "object", properties: { directory: { type: "string", description: "Directory to search in" }, pattern: { type: "string", description: "Glob pattern for filename (e.g. '*.ts')" }, max_results: { type: "number", description: "Max results to return (default 200)" }, workspace_root: { type: "string", description: "Workspace root directory (optional)" }, }, required: ["directory", "pattern"], }, }, - src/index.ts:521-528 (registration)Registration/dispatch logic inside the CallToolRequestSchema handler that invokes findFiles and formats the response.
case "find_files": { const dir = resolveWorkspacePath(a.directory as string, a.workspace_root as string | undefined); const files = await findFiles(dir, a.pattern as string, (a.max_results as number | undefined) ?? 200); if (files.length === 0) { return { content: [{ type: "text", text: "No files found." }] }; } return { content: [{ type: "text", text: files.join("\n") }] }; } - src/index.ts:130-133 (helper)Helper function that converts a glob pattern to a regex for filename matching.
function matchGlob(name: string, pattern: string): boolean { const escaped = pattern.replace(/[.+^${}()|[\]\\]/g, "\\$&").replace(/\*/g, ".*").replace(/\?/g, "."); return new RegExp(`^${escaped}$`).test(name); }