find_similar
Discover related code implementations and similar patterns by searching for symbols with comparable functionality across multiple programming languages.
Instructions
Find code similar to a given symbol. Useful for discovering related implementations, similar patterns, or alternative approaches.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| symbolName | Yes | The name of the symbol to find similar code for | |
| limit | No | Maximum number of similar symbols to return. Default: 5 |
Implementation Reference
- src/retriever.ts:278-290 (handler)The handler method in `retriever.ts` that orchestrates finding the target symbol and calling the search engine.
async findSimilarSymbols(symbolName: string, limit: number = 5): Promise<string> { // Find the target symbol first const symbols = this.indexer.findSymbols(symbolName); if (symbols.length === 0) { return `Symbol "${symbolName}" not found.\nTry using find_symbol to locate it first.`; } const targetSymbol = symbols[0]; const allSymbols = this.getAllSymbols(); // Use search engine to find similar code const similar = CodeSearchEngine.findSimilarSymbols(targetSymbol, allSymbols, limit); - src/search-engine.ts:445-470 (handler)The core algorithm for calculating symbol similarity using Jaccard index.
static findSimilarSymbols(targetSymbol: Symbol, allSymbols: Symbol[], limit: number = 5): SearchMatch[] { const matches: SearchMatch[] = []; const targetTokens = this.tokenize(targetSymbol.name + ' ' + targetSymbol.code); for (const symbol of allSymbols) { if (symbol === targetSymbol) continue; const symbolTokens = this.tokenize(symbol.name + ' ' + symbol.code); // Calculate Jaccard similarity (intersection over union) const intersection = targetTokens.filter(t => symbolTokens.includes(t)).length; const union = new Set([...targetTokens, ...symbolTokens]).size; const similarity = union > 0 ? intersection / union : 0; if (similarity > 0.1) { // At least 10% similar matches.push({ symbol, score: similarity * 100, matchReason: [`${(similarity * 100).toFixed(1)}% similar code`], highlights: [symbol.name], }); } } matches.sort((a, b) => b.score - a.score); return matches.slice(0, limit); - src/index.ts:565-584 (registration)The tool registration and request handler switch case in `src/index.ts`.
case 'find_similar': { const a = args as any; const symbolName: string = a.symbolName || a.symbol || a.name; const limit: number = a.limit || a.max || a.maxResults || 5; if (!symbolName) { return { content: [{ type: 'text', text: 'Error: symbolName is required.' }], isError: true, }; } const result = await retriever.findSimilarSymbols(symbolName, limit); return { content: [ { type: 'text', text: result, }, ], }; }