Skip to main content
Glama

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
NameRequiredDescriptionDefault
patternYesGlob pattern (e.g., **/*.ts)
base_pathNoBase directory for search
max_resultsNoMaximum results (default: 1000)
include_dirsNoInclude directories in results

Implementation Reference

  • 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}`,
              }),
            },
          ],
        };
      }
    }
  • 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);
      }
    );

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/mcp-tool-shop-org/mcp-file-forge'

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