glob_search
Search for files and directories using glob patterns within specified base paths, with configurable result limits and directory inclusion options.
Instructions
Find files matching a glob pattern
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| pattern | Yes | Glob pattern (e.g., **/*.ts) | |
| base_path | No | Base directory for search | |
| max_results | No | Maximum results (default: 1000) | |
| include_dirs | No | Include directories in results |
Implementation Reference
- src/tools/search.ts:32-102 (handler)The implementation of the glob_search tool.
async function globSearchImpl(input: GlobSearchInput): Promise<ToolResult> { try { const basePath = input.base_path ? path.resolve(input.base_path) : process.cwd(); // Use glob to find matching files const matches = await glob(input.pattern, { cwd: basePath, nodir: !input.include_dirs, absolute: true, maxDepth: 20, // Prevent excessive recursion }); // Limit results const limitedMatches = matches.slice(0, input.max_results); // Get basic info for each match const results = await Promise.all( limitedMatches.map(async (filePath) => { try { const stats = await fs.stat(filePath); return { path: filePath, name: path.basename(filePath), isFile: stats.isFile(), isDirectory: stats.isDirectory(), size: stats.size, }; } catch { return { path: filePath, name: path.basename(filePath), error: 'Could not stat file', }; } }) ); return { content: [ { type: 'text', text: JSON.stringify( { count: results.length, total_matches: matches.length, truncated: matches.length > input.max_results, results, }, null, 2 ), }, ], }; } catch (error) { const err = error as Error; return { isError: true, content: [ { type: 'text', text: JSON.stringify({ code: 'UNKNOWN_ERROR', message: `Error in glob search: ${err.message}`, }), }, ], }; } } - src/tools/search.ts:266-280 (registration)Registration of the glob_search tool with the MCP server.
// glob_search tool server.tool( 'glob_search', 'Find files matching a glob pattern', { pattern: z.string().describe('Glob pattern (e.g., **/*.ts)'), base_path: z.string().optional().describe('Base directory for search'), max_results: z.number().optional().describe('Maximum results (default: 1000)'), include_dirs: z.boolean().optional().describe('Include directories in results'), }, async (args) => { const input = GlobSearchInputSchema.parse(args); return await globSearchImpl(input); } );