Skip to main content
Glama
wrale

mcp-server-tree-sitter

by wrale

find_similar_code

Identify similar code snippets within a project by analyzing syntax and structure. Specify a code snippet, language, and similarity threshold to retrieve matching code locations for efficient code comparison and review.

Instructions

Find similar code to a snippet.

    Args:
        project: Project name
        snippet: Code snippet to find
        language: Language of the snippet
        threshold: Similarity threshold (0.0-1.0)
        max_results: Maximum number of results

    Returns:
        List of similar code locations
    

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
languageNo
max_resultsNo
projectYes
snippetYes
thresholdNo

Implementation Reference

  • Handler and registration for the find_similar_code MCP tool. Defines the tool function with @mcp_server.tool() decorator and implements logic using text search on project files filtered by language extension.
    @mcp_server.tool()
    def find_similar_code(
        project: str,
        snippet: str,
        language: Optional[str] = None,
        threshold: float = 0.8,
        max_results: int = 10,
    ) -> List[Dict[str, Any]]:
        """Find similar code to a snippet.
    
        Args:
            project: Project name
            snippet: Code snippet to find
            language: Language of the snippet
            threshold: Similarity threshold (0.0-1.0)
            max_results: Maximum number of results
    
        Returns:
            List of similar code locations
        """
        # This is a simple implementation that uses text search
        from ..tools.search import search_text
    
        # Clean the snippet to handle potential whitespace differences
        clean_snippet = snippet.strip()
    
        # Map language names to file extensions
        extension_map = {
            "python": "py",
            "javascript": "js",
            "typescript": "ts",
            "rust": "rs",
            "go": "go",
            "java": "java",
            "c": "c",
            "cpp": "cpp",
            "ruby": "rb",
            "swift": "swift",
            "kotlin": "kt",
        }
    
        # Get the appropriate file extension for the language
        extension = extension_map.get(language, language) if language else None
        file_pattern = f"**/*.{extension}" if extension else None
    
        return search_text(
            project_registry.get_project(project),
            clean_snippet,
            file_pattern=file_pattern,
            max_results=max_results,
            case_sensitive=False,  # Ignore case differences
            whole_word=False,  # Allow partial matches
            use_regex=False,  # Simple text search is more reliable for this case
        )
  • Test confirming that find_similar_code is registered as one of the expected tools in register_tools function.
        "find_similar_code",
        "find_usage",
        "clear_cache",
    ]

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/wrale/mcp-server-tree-sitter'

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