Skip to main content
Glama

semantic_code_search

Find code by meaning using semantic search to locate relevant files based on concepts rather than exact keywords, enabling discovery of related functionality across your codebase.

Instructions

Search the codebase by MEANING, not just exact variable names. Uses Ollama embeddings over file headers and symbol names. Example: searching 'user authentication' finds files about login, sessions, JWT even if those exact words aren't used, with matched definition lines.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
queryYesNatural language description of what you're looking for. Example: 'how are transactions signed'
top_kNoNumber of matches to return. Default: 5.
semantic_weightNoWeight for embedding similarity in hybrid ranking. Default: 0.72.
keyword_weightNoWeight for keyword overlap in hybrid ranking. Default: 0.28.
min_semantic_scoreNoMinimum semantic score filter. Accepts 0-1 or 0-100.
min_keyword_scoreNoMinimum keyword score filter. Accepts 0-1 or 0-100.
min_combined_scoreNoMinimum final score filter. Accepts 0-1 or 0-100.
require_keyword_matchNoWhen true, only return files with keyword overlap.
require_semantic_matchNoWhen true, only return files with positive semantic similarity.

Implementation Reference

  • The primary handler for the 'semantic_code_search' tool, which builds an index and performs a hybrid search.
    export async function semanticCodeSearch(options: SemanticSearchOptions): Promise<string> {
      const index = await buildIndex(options.rootDir);
      const searchOptions: SearchQueryOptions = {
        topK: options.topK,
        semanticWeight: options.semanticWeight,
        keywordWeight: options.keywordWeight,
        minSemanticScore: options.minSemanticScore,
        minKeywordScore: options.minKeywordScore,
        minCombinedScore: options.minCombinedScore,
        requireKeywordMatch: options.requireKeywordMatch,
        requireSemanticMatch: options.requireSemanticMatch,
      };
      const results = await index.search(options.query, searchOptions);
    
      if (results.length === 0) return "No matching files found for the given query.";
    
      const lines: string[] = [`Top ${results.length} hybrid matches for: "${options.query}"\n`];
    
      for (let i = 0; i < results.length; i++) {
        const r = results[i];
        lines.push(`${i + 1}. ${r.path} (${r.score}% total)`);
        lines.push(`   Semantic: ${r.semanticScore}% | Keyword: ${r.keywordScore}%`);
        if (r.header) lines.push(`   Header: ${r.header}`);
        if (r.matchedSymbols.length > 0) lines.push(`   Matched symbols: ${r.matchedSymbols.join(", ")}`);
        if (r.matchedSymbolLocations.length > 0) lines.push(`   Definition lines: ${r.matchedSymbolLocations.join(", ")}`);
        lines.push("");
      }
    
      return lines.join("\n");
    }
  • Input options interface for the semantic_code_search tool.
    export interface SemanticSearchOptions {
      rootDir: string;
      query: string;
      topK?: number;
      semanticWeight?: number;
      keywordWeight?: number;
      minSemanticScore?: number;
      minKeywordScore?: number;
      minCombinedScore?: number;
      requireKeywordMatch?: boolean;
      requireSemanticMatch?: boolean;
    }

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/ForLoopCodes/contextplus'

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