Labsco
ast-grep logo

ast-grep MCP

โ˜… 426

from ast-grep

An experimental MCP server that uses the ast-grep CLI for code structural search, linting, and rewriting.

๐Ÿ”ฅ๐Ÿ”ฅ๐Ÿ”ฅ๐Ÿ”ฅโœ“ VerifiedFreeQuick setup

ast-grep MCP Server

An experimental Model Context Protocol (MCP) server that provides AI assistants with powerful structural code search capabilities using ast-grep.

Overview

This MCP server enables AI assistants (like Cursor, Claude Desktop, etc.) to search and analyze codebases using Abstract Syntax Tree (AST) pattern matching rather than simple text-based search. By leveraging ast-grep's structural search capabilities, AI can:

  • Find code patterns based on syntax structure, not just text matching
  • Search for specific programming constructs (functions, classes, imports, etc.)
  • Write and test complex search rules using YAML configuration
  • Debug and visualize AST structures for better pattern development

Features

The server provides four main tools for code analysis:

๐Ÿ” dump_syntax_tree

Visualize the Abstract Syntax Tree structure of code snippets. Essential for understanding how to write effective search patterns.

Use cases:

  • Debug why a pattern isn't matching
  • Understand the AST structure of target code
  • Learn ast-grep pattern syntax

๐Ÿงช test_match_code_rule

Test ast-grep YAML rules against code snippets before applying them to larger codebases.

Use cases:

  • Validate rules work as expected
  • Iterate on rule development
  • Debug complex matching logic

๐ŸŽฏ find_code

Search codebases using simple ast-grep patterns for straightforward structural matches.

Parameters:

  • max_results: Limit number of complete matches returned (default: unlimited)
  • output_format: Choose between "text" (default, ~75% fewer tokens) or "json" (full metadata)

Text Output Format:

Found 2 matches:

path/to/file.py:10-15
def example_function():
    # function body
    return result

path/to/file.py:20-22
def another_function():
    pass

Use cases:

  • Find function calls with specific patterns
  • Locate variable declarations
  • Search for simple code constructs

๐Ÿš€ find_code_by_rule

Advanced codebase search using complex YAML rules that can express sophisticated matching criteria.

Parameters:

  • max_results: Limit number of complete matches returned (default: unlimited)
  • output_format: Choose between "text" (default, ~75% fewer tokens) or "json" (full metadata)

Use cases:

  • Find nested code structures
  • Search with relational constraints (inside, has, precedes, follows)
  • Complex multi-condition searches

Supported Languages

ast-grep supports many programming languages including:

  • JavaScript/TypeScript
  • Python
  • Rust
  • Go
  • Java
  • C/C++
  • C#
  • And many more...

For a complete list of built-in supported languages, see the ast-grep language support documentation.

You can also add support for custom languages through the sgconfig.yaml configuration file. See the custom language guide for details.

Contributing

This is an experimental project. Issues and pull requests are welcome!

Related Projects

  • ast-grep - The core structural search tool
  • Model Context Protocol - The protocol this server implements
  • FastMCP - The Python MCP framework used
  • Codemod MCP - Gives AI assistants tools like tree-sitter AST and node types, ast-grep instructions (YAML and JS ast-grep), and Codemod CLI commands to easily build, publish, and run ast-grep based codemods.

MseeP.ai Security Assessment Badge