Skip to main content
Glama
wrale

mcp-server-tree-sitter

by wrale

build_query

Construct tree-sitter queries from predefined templates or custom patterns for specified programming languages. Combine patterns using logical operators to enhance code analysis and context extraction.

Instructions

Build a tree-sitter query from templates or patterns.

    Args:
        language: Language name
        patterns: List of template names or custom patterns
        combine: How to combine patterns ("or" or "and")

    Returns:
        Combined query
    

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
combineNoor
languageYes
patternsYes

Implementation Reference

  • The primary handler function for the 'build_query' MCP tool. It is registered via the @mcp_server.tool() decorator and delegates the core query building logic to build_compound_query.
    @mcp_server.tool()
    def build_query(language: str, patterns: List[str], combine: str = "or") -> Dict[str, str]:
        """Build a tree-sitter query from templates or patterns.
    
        Args:
            language: Language name
            patterns: List of template names or custom patterns
            combine: How to combine patterns ("or" or "and")
    
        Returns:
            Combined query
        """
        from ..tools.query_builder import build_compound_query
    
        query = build_compound_query(language, patterns, combine)
        return {
            "language": language,
            "query": query,
        }
  • Core helper function that implements the query building logic by resolving templates and combining them with OR or simplified AND logic.
    def build_compound_query(language: str, patterns: List[str], combine: str = "or") -> str:
        """
        Build a compound query from multiple patterns.
    
        Args:
            language: Language identifier
            patterns: List of pattern names or custom patterns
            combine: How to combine patterns ("or" or "and")
    
        Returns:
            Combined query string
        """
        queries = []
    
        for pattern in patterns:
            template = get_template(language, pattern)
            if template:
                queries.append(template)
    
        # For 'or' we can just concatenate
        if combine.lower() == "or":
            return "\n".join(queries)
    
        # For 'and' we need to add predicates
        # This is a simplified implementation
        combined = "\n".join(queries)
        combined += "\n\n;; Add your #match predicates here to require combinations"
    
        return combined
  • Supporting helper that resolves individual patterns to query templates using get_query_template from language registry.
    def get_template(language: str, pattern: str) -> str:
        """
        Get a query template with optional parameter replacement.
    
        Args:
            language: Language identifier
            pattern: Template name or custom pattern
    
        Returns:
            Query string
        """
        # Check if this is a template name
        template = get_query_template(language, pattern)
        if template:
            return template
    
        # Otherwise return as-is
        return pattern

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