ast-grep MCP Server (Rust)
Rust implementation of a Model Context Protocol (MCP) server that gives AI assistants structural code search via ast-grep.
Upstream Attribution
This repository is a Rust port of ast-grep/ast-grep-mcp. Original attribution and MIT license notice are preserved in LICENSE.
What This Server Provides
The server exposes four MCP tools:
dump_syntax_tree: Inspect syntax tree or pattern structure for debugging rules.test_match_code_rule: Test a YAML ast-grep rule against code from stdin.find_code: Search a project with an ast-grep pattern.find_code_by_rule: Search a project with a full YAML ast-grep rule.
find_code and find_code_by_rule support:
output_format:text(default) orjsonmax_results: optional positive limit for returned matches
Requirements
- Install ast-grep CLI (must be available in your
PATH)
# Choose one installation method from ast-grep docs
- Install Rust toolchain
|
- Use an MCP-compatible client (Cursor, Claude Desktop, etc.)
Installation
Option 1: Install from crates.io (recommended)
This installs the executable as:
ast-grep-mcp-server
On Linux/macOS, Cargo installs binaries to ~/.cargo/bin by default.
Make sure that directory is in your PATH.
Option 2: Download a prebuilt binary from GitHub Releases
- Open the Releases page: https://github.com/GodSpeedAI/ast-grep-mcp-rs/releases
- Download the archive for your platform.
- Extract it and place
ast-grep-mcp-server(orast-grep-mcp-server.exeon Windows) somewhere in yourPATH. - Verify checksums with
SHA256SUMS.txtfrom the same release.
Release assets are published with names like:
ast-grep-mcp-server-vX.Y.Z-x86_64-unknown-linux-gnu.tar.gzast-grep-mcp-server-vX.Y.Z-x86_64-apple-darwin.tar.gzast-grep-mcp-server-vX.Y.Z-aarch64-apple-darwin.tar.gzast-grep-mcp-server-vX.Y.Z-x86_64-pc-windows-msvc.zipSHA256SUMS.txt
Checksum verification example:
Quick Start (Installed Binary)
- Install ast-grep CLI and this server.
- Verify
ast-grep-mcp-serverruns from your shell. - Configure your MCP client to launch
ast-grep-mcp-server.
Quick verification command:
Run
Default transport is stdio:
With custom ast-grep config:
You can also set:
Transport Support
stdio: supported and recommendedsse: currently not implemented in this Rust port
If you run with --transport sse, the server exits with an error telling you to use stdio.
Client Configuration
If you installed via cargo install ast-grep-mcp or from GitHub Releases, use command name ast-grep-mcp-server directly.
Cursor
Add to your MCP settings:
Claude Desktop
Add to your Claude Desktop MCP config:
VS Code
Add to your VS Code MCP configuration:
Common VS Code user settings locations:
- Linux:
~/.config/Code/User/settings.json - macOS:
~/Library/Application Support/Code/User/settings.json - Windows:
%APPDATA%\\Code\\User\\settings.json
CLI Options
--config PATH: path tosgconfig.yaml--transport {stdio|sse}: default isstdio(sse not implemented)--port PORT: parsed but currently only relevant for future SSE support
Tool Behavior Notes
project_folderparameters must be absolute paths.test_match_code_rulereturns an error when no matches are found.- For relational rules (
inside,has), addstopBy: endto avoid incomplete traversal. - Text output is compact (
file:start-end+ matched snippet) to reduce token usage.
Example Queries for an MCP Client
- Find all Python function definitions in this repo.
- Find Rust
implblocks with pattern matching. - Test this ast-grep YAML rule against a code snippet.
- Dump the CST for this TypeScript snippet.
Contributing
Contributor and release-maintainer instructions are in CONTRIBUTING.md.
Supported Languages
Built-in language set includes:
- bash, c, cpp, csharp, css, elixir, go, haskell, html, java
- javascript, json, jsx, kotlin, lua, nix, php, python, ruby, rust
- scala, solidity, swift, tsx, typescript, yaml
You can extend language support with custom languages in sgconfig.yaml.
Troubleshooting
ast-grepcommand not found: install ast-grep and verify it is inPATH.- No matches for complex relational rules: add
stopBy: end. - Unexpected parse/match behavior: use
dump_syntax_treeto inspect CST/pattern representation. - Config path errors: ensure
--configorAST_GREP_CONFIGpoints to an existing file.
License
MIT. See LICENSE.