Skip to main content
Glama

search_context

Find relevant code snippets in your project using natural language queries. This tool automatically indexes your codebase and returns semantically related results with file paths and line numbers.

Instructions

Search for relevant code context based on a query within a specific project. This tool automatically performs incremental indexing before searching, ensuring results are always up-to-date. Returns formatted text snippets from the codebase that are semantically related to your query. Supports cross-platform paths including Windows WSL.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
project_root_pathYesAbsolute path to the project root directory. Supports cross-platform paths: Windows (C:/Users/...), WSL UNC (\\wsl$\Ubuntu\home\...), Unix (/home/...), WSL-to-Windows (/mnt/c/...). Paths are automatically normalized.
queryYesNatural language search query to find relevant code context. This tool performs semantic search and returns code snippets that match your query. Examples: 'logging configuration setup initialization logger' (finds logging setup code), 'user authentication login' (finds auth-related code), 'database connection pool' (finds DB connection code), 'error handling exception' (finds error handling patterns), 'API endpoint routes' (finds API route definitions). The tool returns formatted text snippets with file paths and line numbers showing where the relevant code is located.

Implementation Reference

  • The main handler function searchContextTool that implements the tool logic: validates inputs, normalizes project path, creates IndexManager instance, calls searchContext method, and returns formatted ToolResult.
    export async function searchContextTool(arguments_: SearchContextArgs): Promise<ToolResult> {
      try {
        const projectRootPath = arguments_.project_root_path;
        const query = arguments_.query;
    
        if (!projectRootPath) {
          return { type: 'text', text: 'Error: project_root_path is required' };
        }
    
        if (!query) {
          return { type: 'text', text: 'Error: query is required' };
        }
    
        // 规范化路径,支持 WSL 和跨平台兼容性
        let normalizedPath: string;
        try {
          normalizedPath = normalizeProjectPath(projectRootPath);
        } catch (error: any) {
          return { type: 'text', text: `Error: Invalid project path - ${error.message}` };
        }
    
        logger.info(`Tool invoked: search_context for project ${normalizedPath} with query: ${query}`);
    
        const config = getConfig();
        const indexManager = new IndexManager(
          config.indexStoragePath,
          config.baseUrl,
          config.token,
          config.textExtensions,
          config.batchSize,
          config.maxLinesPerBlob,
          config.excludePatterns
        );
    
        const result = await indexManager.searchContext(normalizedPath, query);
    
        return { type: 'text', text: result };
      } catch (error: any) {
        logger.exception('Error in search_context_tool', error);
        return { type: 'text', text: `Error: ${error.message}` };
      }
    }
  • MCP JSON input schema for the search_context tool, defining properties for project_root_path and query with detailed descriptions.
    inputSchema: {
      type: 'object',
      properties: {
        project_root_path: {
          type: 'string',
          description:
            'Absolute path to the project root directory. Supports cross-platform paths: Windows (C:/Users/...), WSL UNC (\\\\wsl$\\Ubuntu\\home\\...), Unix (/home/...), WSL-to-Windows (/mnt/c/...). Paths are automatically normalized.',
        },
        query: {
          type: 'string',
          description:
            "Natural language search query to find relevant code context. This tool performs semantic search and returns code snippets that match your query. Examples: 'logging configuration setup initialization logger' (finds logging setup code), 'user authentication login' (finds auth-related code), 'database connection pool' (finds DB connection code), 'error handling exception' (finds error handling patterns), 'API endpoint routes' (finds API route definitions). The tool returns formatted text snippets with file paths and line numbers showing where the relevant code is located.",
        },
      },
      required: ['project_root_path', 'query'],
    },
  • src/index.ts:62-88 (registration)
    MCP server registration of search_context tool via ListToolsRequestHandler, providing name, description, and input schema.
    server.setRequestHandler(ListToolsRequestSchema, async () => {
      const tools: Tool[] = [
        {
          name: 'search_context',
          description:
            'Search for relevant code context based on a query within a specific project. This tool automatically performs incremental indexing before searching, ensuring results are always up-to-date. Returns formatted text snippets from the codebase that are semantically related to your query. Supports cross-platform paths including Windows WSL.',
          inputSchema: {
            type: 'object',
            properties: {
              project_root_path: {
                type: 'string',
                description:
                  'Absolute path to the project root directory. Supports cross-platform paths: Windows (C:/Users/...), WSL UNC (\\\\wsl$\\Ubuntu\\home\\...), Unix (/home/...), WSL-to-Windows (/mnt/c/...). Paths are automatically normalized.',
              },
              query: {
                type: 'string',
                description:
                  "Natural language search query to find relevant code context. This tool performs semantic search and returns code snippets that match your query. Examples: 'logging configuration setup initialization logger' (finds logging setup code), 'user authentication login' (finds auth-related code), 'database connection pool' (finds DB connection code), 'error handling exception' (finds error handling patterns), 'API endpoint routes' (finds API route definitions). The tool returns formatted text snippets with file paths and line numbers showing where the relevant code is located.",
              },
            },
            required: ['project_root_path', 'query'],
          },
        },
      ];
    
      return { tools };
    });
  • src/index.ts:98-107 (registration)
    Dispatch logic in CallToolRequestHandler that invokes searchContextTool when name === 'search_context'.
    if (name === 'search_context') {
      const result = await searchContextTool(args as any);
      return {
        content: [
          {
            type: 'text',
            text: result.text,
          },
        ],
      };
  • Core helper method in IndexManager that performs automatic incremental indexing, loads indexed blobs, and executes semantic search via remote API, returning formatted retrieval results.
    async searchContext(projectRootPath: string, query: string): Promise<string> {
      const normalizedPath = this.normalizePath(projectRootPath);
      logger.info(`Searching context in project ${normalizedPath} with query: ${query}`);
    
      try {
        // 步骤 1: 自动索引
        logger.info(`Auto-indexing project ${normalizedPath} before search...`);
        const indexResult = await this.indexProject(projectRootPath);
    
        if (indexResult.status === 'error') {
          return `Error: Failed to index project before search. ${indexResult.message}`;
        }
    
        // 记录索引统计信息
        if (indexResult.stats) {
          logger.info(
            `Auto-indexing completed: total=${indexResult.stats.total_blobs}, existing=${indexResult.stats.existing_blobs}, new=${indexResult.stats.new_blobs}`
          );
        }
    
        // 步骤 2: 加载已索引的 blob 名称
        const projects = this.loadProjects();
        const blobNames = projects[normalizedPath] || [];
    
        if (blobNames.length === 0) {
          return `Error: No blobs found for project ${normalizedPath} after indexing.`;
        }
    
        // 步骤 3: 执行搜索
        logger.info(`Performing search with ${blobNames.length} blobs...`);
        const payload = {
          information_request: query,
          blobs: {
            checkpoint_id: null,
            added_blobs: blobNames,
            deleted_blobs: [],
          },
          dialog: [],
          max_output_length: 0,
          disable_codebase_retrieval: false,
          enable_commit_retrieval: false,
        };
    
        const searchRequest = async () => {
          // 使用 httpClient 确保配置一致性
          const response = await this.httpClient.post(
            `${this.baseUrl}/agents/codebase-retrieval`,
            payload,
            {
              timeout: 60000,  // 搜索请求使用更长的超时时间
            }
          );
          return response.data;
        };
    
        let result: any;
        try {
          result = await this.retryRequest(searchRequest, 3, 2000);
        } catch (error: any) {
          logger.error(`Search request failed after retries: ${error.message}`);
          return `Error: Search request failed after 3 retries. ${error.message}`;
        }
    
        const formattedRetrieval = result.formatted_retrieval || '';
    
        if (!formattedRetrieval) {
          logger.warning(`Search returned empty result for project ${normalizedPath}`);
          return 'No relevant code context found for your query.';
        }
    
        logger.info(`Search completed for project ${normalizedPath}`);
        return formattedRetrieval;
      } catch (error: any) {
        logger.error(`Failed to search context in project ${normalizedPath}: ${error.message}`);
        return `Error: ${error.message}`;
      }
    }
Behavior4/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries the full burden of behavioral disclosure. It adds valuable context beyond basic functionality: it mentions automatic incremental indexing before searching, ensures results are up-to-date, returns formatted text snippets with file paths and line numbers, and supports cross-platform paths. However, it lacks details on performance, rate limits, or error handling, preventing a perfect score.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is appropriately sized and front-loaded, with the core purpose stated first. Each sentence adds value: indexing behavior, result format, and platform support. There's minimal waste, though it could be slightly more streamlined by integrating some details more tightly.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness3/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool's moderate complexity (2 parameters, no output schema, no annotations), the description is adequate but has gaps. It covers purpose, behavior, and parameters well, but without an output schema, it doesn't fully explain return values (e.g., format of snippets or error cases). This leaves room for improvement in completeness.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

The schema description coverage is 100%, meaning the input schema already documents both parameters thoroughly. The description adds some semantic context by mentioning 'cross-platform paths' for project_root_path and giving examples for query, but this doesn't significantly enhance understanding beyond the schema. This meets the baseline for high schema coverage.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool's purpose: 'Search for relevant code context based on a query within a specific project.' It specifies the verb (search), resource (code context), and scope (within a specific project). However, without sibling tools, it cannot demonstrate differentiation from alternatives, so it doesn't reach the highest score.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines3/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description implies usage context through phrases like 'within a specific project' and 'Supports cross-platform paths,' but it doesn't explicitly state when to use this tool versus alternatives or provide any exclusions. Since no sibling tools exist, there's no opportunity for comparative guidance, leaving the guidance at an implied level.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

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/yeuxuan/Ace-Mcp-Node'

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