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
Behavior2/5

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

No annotations are provided, so the description carries the full burden. It states what the tool does but lacks behavioral details such as whether it validates inputs, handles errors, caches results, or has performance considerations. For a tool with no annotation coverage, this is insufficient.

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

Conciseness4/5

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

The description is appropriately sized and front-loaded with the core purpose. The Args and Returns sections are structured clearly, though the formatting with indentation might be slightly verbose. Overall, it's efficient with minimal waste.

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

Completeness3/5

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

Given no annotations, 0% schema coverage, and no output schema, the description is moderately complete. It covers the basic purpose and parameters but lacks details on behavior, error handling, and output specifics. For a tool with three parameters and no structured support, it's adequate but has clear gaps.

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 description coverage is 0%, so the description must compensate. It lists the parameters (language, patterns, combine) and provides some semantics (e.g., 'patterns' can be template names or custom patterns, 'combine' has options 'or' or 'and'), but it doesn't fully explain usage details like format examples or constraints. Baseline is 3 as it adds moderate value beyond the bare schema.

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: 'Build a tree-sitter query from templates or patterns.' It specifies the verb ('Build') and resource ('tree-sitter query'), though it doesn't explicitly distinguish it from sibling tools like 'run_query' or 'adapt_query', which might have overlapping functionality.

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 'run_query' (which might execute queries) or 'adapt_query' (which might modify them), leaving the agent without 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