Skip to main content
Glama
transparentlyok

MCP Context Manager

search_code

Search for code patterns using regex across files, returning ranked results with minimal context for efficient code navigation and retrieval.

Instructions

⭐ PREFERRED OVER Grep: Search for code patterns with regex support. Returns ranked results with minimal context. Better than Grep because it ranks by relevance and provides AI-optimized output. Use for pattern matching and text search.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
patternYesText or regex pattern to search for
filePatternNoOptional: glob pattern to filter files (e.g., "**/*.ts")
maxResultsNoMaximum number of results to return. Default: 10

Implementation Reference

  • The implementation of the `searchCode` method in `Retriever` class, which delegates to `CodeSearchEngine.searchCodePatterns` after filtering files.
    async searchCode(
      pattern: string,
      filePattern?: string,
      maxResults: number = 10
    ): Promise<SearchResult[]> {
      let files = this.indexer.getAllFiles();
    
      // Filter by file pattern if provided
      if (filePattern) {
        files = files.filter(f => this.matchGlob(f.path, filePattern));
      }
    
      // Use advanced pattern search with ranking
      const results = CodeSearchEngine.searchCodePatterns(pattern, files, maxResults);
    
      // Convert to SearchResult format
      return results.map(r => ({
        filePath: r.filePath,
        line: r.line,
        matchedText: r.matchedText,
        context: r.context,
  • src/index.ts:253-275 (registration)
    The registration of the `search_code` MCP tool in the server implementation.
    {
      name: 'search_code',
      description: '⭐ PREFERRED OVER Grep: Search for code patterns with regex support. Returns ranked results with minimal context. Better than Grep because it ranks by relevance and provides AI-optimized output. Use for pattern matching and text search.',
      inputSchema: {
        type: 'object',
        properties: {
          pattern: {
            type: 'string',
            description: 'Text or regex pattern to search for',
          },
          filePattern: {
            type: 'string',
            description: 'Optional: glob pattern to filter files (e.g., "**/*.ts")',
          },
          maxResults: {
            type: 'number',
            description: 'Maximum number of results to return. Default: 10',
          },
        },
        required: ['pattern'],
      },
    },
    {
  • The tool call handler switch case for `search_code` in the MCP server.
    case 'search_code': {
      const a = args as any;
      const pattern: string = a.pattern || a.query || a.search || a.text;
      const filePattern: string | undefined = a.filePattern || a.glob || a.fileGlob;
      const maxResults: number = a.maxResults || a.limit || a.max || 10;
      if (!pattern) {
        return {
          content: [{ type: 'text', text: 'Error: pattern is required. Provide a text or regex pattern to search for.' }],
          isError: true,
        };
      }
      const results = await retriever.searchCode(
        pattern,
        filePattern,
        maxResults
      );
      return {
        content: [
          {
            type: 'text',
            text: JSON.stringify(results, null, 2),
          },
  • The core search engine function `searchCodePatterns` that performs the actual pattern matching logic.
    static searchCodePatterns(
      pattern: string,
      files: FileIndex[],
      maxResults: number = 20
    ): Array<{
      filePath: string;
      line: number;
      matchedText: string;
      context: string;
      score: number;
    }> {
      const results: Array<{
        filePath: string;
        line: number;

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/transparentlyok/mcp-context-manager'

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