Skip to main content
Glama
ast-grep

ast-grep MCP Server

by ast-grep

find_code_by_rule

Search codebases using advanced AST pattern matching with YAML rules to locate complex code structures like nested or related syntax elements.

Instructions

Find code using ast-grep's YAML rule in a project folder. YAML rule is more powerful than simple pattern and can perform complex search like find AST inside/having another AST. It is a more advanced search tool than the simple find_code.

Tip: When using relational rules (inside/has), add stopBy: end to ensure complete traversal.

Internally calls: ast-grep scan --inline-rules [--json] <project_folder>

Output formats:

  • text (default): Compact text format with file:line-range headers and complete match text Example: Found 2 matches:

    src/models.py:45-52 class UserModel: def init(self): self.id = None self.name = None

    src/views.py:12 class SimpleView: pass

  • json: Full match objects with metadata including ranges, meta-variables, etc.

The max_results parameter limits the number of complete matches returned (not individual lines). When limited, the header shows "Found X matches (showing first Y of Z)".

Example usage: find_code_by_rule(yaml="id: x\nlanguage: python\nrule: {pattern: 'class $NAME'}", max_results=20) find_code_by_rule(yaml="...", output_format="json") # For full metadata

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
project_folderYesThe absolute path to the project folder. It must be absolute path.
yamlYesThe ast-grep YAML rule to search. It must have id, language, rule fields.
max_resultsNoMaximum results to return
output_formatNo'text' or 'json'text
Behavior4/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 effectively describes key behaviors: the internal implementation ('Internally calls: ast-grep scan'), output formats with detailed examples, how max_results works ('limits the number of complete matches'), and handling of limited results. It doesn't mention error conditions or performance characteristics, but covers most essential operational details.

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 well-structured with clear sections: purpose statement, comparison to sibling tool, usage tip, implementation details, output formats with examples, parameter behavior explanation, and example usage. While comprehensive, some sentences could be more concise, and the example usage section is quite detailed but necessary for clarity.

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

Completeness4/5

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

For a tool with 4 parameters, no annotations, and no output schema, the description provides substantial context: purpose, differentiation from siblings, behavioral details, output format explanations with examples, and parameter usage guidance. It covers what the tool does, how to use it, and what to expect, though it doesn't document the exact structure of returned data beyond format descriptions.

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 100%, so the schema already documents all parameters thoroughly. The description adds minimal parameter-specific information beyond the schema - it mentions YAML rule requirements ('must have id, language, rule fields') and provides example usage with parameters, but doesn't significantly enhance understanding of individual parameters. This meets the baseline for high schema coverage.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/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 code using ast-grep's YAML rule in a project folder.' It specifies the verb ('Find'), resource ('code'), and method ('using ast-grep's YAML rule'), and explicitly distinguishes it from the sibling tool 'find_code' by stating it's 'more powerful' and 'more advanced.'

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

Usage Guidelines5/5

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

The description provides explicit guidance on when to use this tool versus alternatives. It states it's 'more powerful than simple pattern' and 'a more advanced search tool than the simple `find_code`,' directly comparing it to a sibling tool. It also includes a tip for using relational rules and example usage scenarios.

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

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/ast-grep/ast-grep-mcp'

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