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",
    ]
Behavior2/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 states the tool returns 'List of similar code locations,' which hints at a read-only operation, but doesn't clarify permissions, rate limits, performance implications, or what 'similar' means algorithmically. This is inadequate for a tool with 5 parameters and no annotation coverage.

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 well-structured and front-loaded with the core purpose, followed by parameter and return details in a clear format. Every sentence adds value without redundancy, making it efficient for an agent to parse.

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

Completeness2/5

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

Given the tool's complexity (5 parameters, no annotations, no output schema), the description is incomplete. It lacks details on behavioral traits (e.g., performance, error handling), output format specifics, and usage context relative to siblings. For a code analysis tool with multiple parameters, this leaves significant gaps for an agent.

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 description lists all 5 parameters with brief explanations (e.g., 'Similarity threshold (0.0-1.0)'), adding meaning beyond the schema's 0% description coverage. However, it doesn't elaborate on how parameters interact (e.g., how 'threshold' affects results) or provide examples, leaving some ambiguity. This compensates partially but not fully for the schema gap.

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: 'Find similar code to a snippet.' It specifies the verb ('find') and resource ('similar code'), making it immediately understandable. However, it doesn't explicitly differentiate from sibling tools like 'find_text' or 'find_usage,' which prevents a perfect score.

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

Usage Guidelines2/5

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

The description provides no guidance on when to use this tool versus alternatives. It doesn't mention sibling tools like 'find_text' (for text search) or 'find_usage' (for usage patterns), nor does it specify prerequisites or exclusions. This leaves the agent with minimal context for tool selection.

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

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

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