Skip to main content
Glama
kvnpetit

SRC (Structured Repo Context)

by kvnpetit

search_code

Search code by meaning with natural language queries. Returns relevant code chunks, file locations, function names, and call relationships.

Instructions

Search code semantically using natural language queries. USE THIS to find code by concept/meaning (e.g., 'authentication logic', 'error handling'). Requires index_codebase first. Returns relevant code chunks with file locations, function names, and call relationships (who calls what).

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
queryYesNatural language search query
directoryNoPath to the indexed directory (defaults to current directory).
limitNoMaximum number of results to return
thresholdNoMaximum distance threshold for results (lower = more similar)
modeNoSearch mode: 'vector' (semantic only), 'fts' (keyword only), 'hybrid' (combined with RRF fusion)hybrid
includeCallContextNoInclude caller/callee information for each result (uses cached call graph)

Implementation Reference

  • The main handler/execute function for the search_code tool. It takes SearchCodeInput, initializes Ollama client and vector store, performs hybrid search (vector + BM25 + RRF), optionally adds call context, and returns formatted results.
    export async function execute(input: SearchCodeInput): Promise<FeatureResult> {
      const { query, directory, limit, threshold, mode, includeCallContext } =
        input;
    
      // Validate directory exists
      if (!fs.existsSync(directory)) {
        return {
          success: false,
          error: `Directory not found: ${directory}`,
        };
      }
    
      const absoluteDir = path.resolve(directory);
    
      // Initialize components
      const ollamaClient = createOllamaClient(EMBEDDING_CONFIG);
      const vectorStore = createVectorStore(absoluteDir, EMBEDDING_CONFIG);
    
      // Check if index exists
      if (!vectorStore.exists()) {
        return {
          success: false,
          error: `No index found for directory. Run index_codebase first: ${absoluteDir}`,
        };
      }
    
      try {
        // Check Ollama health
        const health = await ollamaClient.healthCheck();
        if (!health.ok) {
          return {
            success: false,
            error: health.error ?? "Ollama is not available",
          };
        }
    
        // Connect to vector store
        await vectorStore.connect();
    
        // Generate query embedding
        const queryVector = await ollamaClient.embed(query);
    
        // Search for similar chunks using hybrid search (vector + BM25 + RRF)
        let results = await vectorStore.searchHybrid(queryVector, query, limit, {
          mode: mode as SearchMode,
        });
    
        // Apply threshold filter if specified (only for vector mode where lower = better)
        // For hybrid/fts modes, RRF scores are higher = better, so threshold is ignored
        if (threshold !== undefined && mode === "vector") {
          results = results.filter((r) => r.score <= threshold);
        }
    
        vectorStore.close();
    
        let formattedResults = formatResults(results, absoluteDir);
    
        // Add call context if requested
        if (includeCallContext && formattedResults.length > 0) {
          // Build call graph for the directory
          const ig = createIgnoreFilter(absoluteDir);
          const files = collectFiles(absoluteDir, ig, absoluteDir);
          const fileContents = files.map((f) => ({
            path: f,
            content: fs.readFileSync(f, "utf-8"),
          }));
    
          const callGraph = await buildCallGraph(fileContents);
    
          // Add call context to each result that has a symbol name
          formattedResults = formattedResults.map((result) => {
            if (!result.symbolName) {
              return result;
            }
    
            const fullPath = path.join(absoluteDir, result.filePath);
            const context = getCallContext(callGraph, fullPath, result.symbolName);
    
            if (context) {
              return {
                ...result,
                callContext: {
                  callers: context.callers.map((c) => c.name),
                  callees: context.callees.map((c) => c.name),
                },
              };
            }
    
            return result;
          });
        }
    
        const output: SearchOutput = {
          query,
          directory: absoluteDir,
          resultsCount: formattedResults.length,
          results: formattedResults,
        };
    
        if (formattedResults.length === 0) {
          return {
            success: true,
            message: "No matching code found",
            data: output,
          };
        }
    
        // Build text message with results
        const resultLines = formattedResults.map((r, i) => {
          const location = `${r.filePath}:${String(r.startLine)}-${String(r.endLine)}`;
          const symbol = r.symbolName
            ? ` (${r.symbolType ?? "symbol"}: ${r.symbolName})`
            : "";
          const preview = r.content.slice(0, 100).replace(/\n/g, " ");
    
          let callInfo = "";
          if (r.callContext) {
            const callers =
              r.callContext.callers.length > 0
                ? `Called by: ${r.callContext.callers.slice(0, 3).join(", ")}${r.callContext.callers.length > 3 ? "..." : ""}`
                : "";
            const callees =
              r.callContext.callees.length > 0
                ? `Calls: ${r.callContext.callees.slice(0, 3).join(", ")}${r.callContext.callees.length > 3 ? "..." : ""}`
                : "";
            if (callers || callees) {
              callInfo = `\n   ${[callers, callees].filter(Boolean).join(" | ")}`;
            }
          }
    
          return `${String(i + 1)}. [${r.language}] ${location}${symbol}\n   ${preview}...${callInfo}`;
        });
    
        const message = `Found ${String(formattedResults.length)} results for "${query}":\n\n${resultLines.join("\n\n")}`;
    
        return {
          success: true,
          message,
          data: output,
        };
      } catch (err) {
        vectorStore.close();
        const errorMsg = err instanceof Error ? err.message : String(err);
        return {
          success: false,
          error: `Search failed: ${errorMsg}`,
        };
      }
    }
  • Zod schema defining input validation for search_code: query (string, required), directory, limit (default 10), threshold, mode (hybrid/vector/fts, default hybrid), includeCallContext (default true).
    export const searchCodeSchema = z.object({
      query: z.string().min(1).describe("Natural language search query"),
      directory: z
        .string()
        .optional()
        .default(".")
        .describe("Path to the indexed directory (defaults to current directory)"),
      limit: z
        .number()
        .int()
        .positive()
        .optional()
        .default(10)
        .describe("Maximum number of results to return"),
      threshold: z
        .number()
        .min(0)
        .max(2)
        .optional()
        .describe("Maximum distance threshold for results (lower = more similar)"),
      mode: z
        .enum(["vector", "fts", "hybrid"])
        .optional()
        .default("hybrid")
        .describe(
          "Search mode: 'vector' (semantic only), 'fts' (keyword only), 'hybrid' (combined with RRF fusion)",
        ),
      includeCallContext: z
        .boolean()
        .optional()
        .default(true)
        .describe(
          "Include caller/callee information for each result (uses cached call graph)",
        ),
    });
  • The Feature object for search_code: registers the tool under name 'search_code' with its schema, description, and execute function.
    export const searchCodeFeature: Feature<typeof searchCodeSchema> = {
      name: "search_code",
      description:
        "Search code semantically using natural language queries. USE THIS to find code by concept/meaning (e.g., 'authentication logic', 'error handling'). Requires index_codebase first. Returns relevant code chunks with file locations, function names, and call relationships (who calls what).",
      schema: searchCodeSchema,
      execute,
    };
  • The features registry array that includes searchCodeFeature and exports it via the getFeature lookup function.
    // Registry of features exposed via CLI and MCP
    export const features: Feature[] = [
      infoFeature,
      indexCodebaseFeature,
      searchCodeFeature,
      getIndexStatusFeature,
      updateIndexFeature,
    ];
  • Generic adapter that registers any feature as an MCP tool on the server by calling server.tool() with feature name, description, schema, and execute wrapper.
    export function registerFeatureAsTool(
      server: McpServer,
      feature: Feature,
    ): void {
      const mcpSchema = zodToMcpSchema(feature.schema);
    
      // eslint-disable-next-line @typescript-eslint/no-deprecated
      server.tool(feature.name, feature.description, mcpSchema, async (params) => {
        const result = feature.execute(params);
    
        const formatResult = (
          res: Awaited<ReturnType<typeof feature.execute>>,
        ): {
          content: { type: "text"; text: string }[];
          isError: boolean;
        } => ({
          content: [
            {
              type: "text" as const,
              text: res.message ?? JSON.stringify(res.data, null, 2),
            },
          ],
          isError: !res.success,
        });
    
        if (result instanceof Promise) {
          return await result.then(formatResult);
        }
        return formatResult(result);
      });
    }
Behavior4/5

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

No annotations are provided, so the description carries full burden. It accurately describes the tool as a semantic search that returns code chunks with locations, function names, and call relationships. It also mentions the use of a cached call graph, which is a behavioral detail beyond the schema.

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

Conciseness5/5

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

The description is concise: three sentences that front-load the core purpose and usage. Every sentence adds value without redundancy.

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

Completeness4/5

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

Given no output schema, the description adequately explains the return value (code chunks, locations, functions, call relationships) and prerequisite. It lacks information about error handling or performance, but these are not critical for a search tool.

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?

Schema coverage is 100%, so baseline is 3. The description does not add additional parameter-level details beyond what the schema already provides, but it does mention the output structure which indirectly relates to parameters like includeCallContext.

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

Purpose5/5

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

The description clearly states it searches code semantically using natural language queries, distinguishing it from sibling tools like index_codebase and get_index_status. It provides specific examples ('authentication logic', 'error handling') and mentions the key functionality: finding code by concept/meaning.

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

Usage Guidelines4/5

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

The description explicitly says 'USE THIS to find code by concept/meaning' and notes the prerequisite 'Requires index_codebase first'. While it does not list alternatives or when-not-to-use, the usage context is clear.

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/kvnpetit/structured-repo-context-mcp'

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