ast-grep MCP Server
Enables AST-aware pattern matching, search, and safe refactoring for C++ code using ast-grep, supporting structural code manipulation across files.
Enables AST-aware pattern matching, search, and safe refactoring for CSS code using ast-grep, supporting structural code manipulation across files.
Enables AST-aware pattern matching, search, and safe refactoring for Dart code using ast-grep, supporting structural code manipulation across files.
Enables AST-aware pattern matching, search, and safe refactoring for JavaScript code using ast-grep, supporting structural code manipulation across files.
Enables AST-aware pattern matching, search, and safe refactoring for Kotlin code using ast-grep, supporting structural code manipulation across files.
Enables AST-aware pattern matching, search, and safe refactoring for Lua code using ast-grep, supporting structural code manipulation across files.
Enables AST-aware pattern matching, search, and safe refactoring for PHP code using ast-grep, supporting structural code manipulation across files.
Enables AST-aware pattern matching, search, and safe refactoring for Python code using ast-grep, supporting structural code manipulation across files.
Enables AST-aware pattern matching, search, and safe refactoring for Ruby code using ast-grep, supporting structural code manipulation across files.
Enables AST-aware pattern matching, search, and safe refactoring for Rust code using ast-grep, supporting structural code manipulation across files.
Enables AST-aware pattern matching, search, and safe refactoring for Scala code using ast-grep, supporting structural code manipulation across files.
Enables AST-aware pattern matching, search, and safe refactoring for Swift code using ast-grep, supporting structural code manipulation across files.
Enables AST-aware pattern matching, search, and safe refactoring for TypeScript code using ast-grep, supporting structural code manipulation across files.
Enables AST-aware pattern matching, search, and safe refactoring for YAML code using ast-grep, supporting structural code manipulation across files.
Click on "Deploy Server".
Wait a few minutes for the server to deploy. Once ready, it will show a "Started" state.
In the chat, type
@followed by the MCP server name and your instructions, e.g., "@ast-grep MCP Server find all console.log calls and replace with logger.info"
That's it! The server will respond to your query, and you can continue using it as needed.
Here is a step-by-step guide with screenshots.
MCP ast-grep Server
A Model Context Protocol (MCP) server that provides ast-grep integration for Claude Code, enabling powerful structural code search and refactoring across 20+ programming languages.
Features
AST-aware pattern matching: Search and replace based on code structure, not just text
Multi-language support: JavaScript, TypeScript, Python, Rust, Go, Java, C/C++, and more
Safe refactoring: Dry-run mode by default for replacements
Flexible filtering: Support for file globs and language-specific parsing
Rich output: Contextual matches with diff previews for replacements
Related MCP server: XRAY MCP
Prerequisites
Node.js (>= 18.0.0)
ast-grep CLI: Install globally with
npm install -g @ast-grep/cliClaude Code with MCP support
Installation
Quick Install (Recommended)
Install both prerequisites and configure Claude Code in one go:
# Install ast-grep CLI
npm install -g @ast-grep/cli
# Clone and build the MCP server
git clone <repository-url>
cd mcp-ast-grep
npm install
npm run build
# Add to Claude Code using the MCP CLI tool
claude mcp install ast-grep node ./dist/index.jsManual Installation
Option 1: npm install (if published)
npm install -g mcp-ast-grep
claude mcp install ast-grep mcp-ast-grepOption 2: Build from source
git clone <repository-url>
cd mcp-ast-grep
npm install
npm run buildConfiguration
Using Claude MCP CLI (Recommended)
The easiest way to add this server to Claude Code is using the claude mcp CLI tool:
# Add the server (from built source)
claude mcp install ast-grep node /path/to/mcp-ast-grep/dist/index.js
# Or if installed globally
claude mcp install ast-grep mcp-ast-grep
# Verify installation
claude mcp list
# Remove if needed
claude mcp remove ast-grepManual Configuration
If you prefer manual configuration, add the server to your Claude Code MCP settings:
macOS
~/Library/Application Support/Claude/claude_desktop_config.json
Windows
%APPDATA%/Claude/claude_desktop_config.json
Linux
~/.config/claude/claude_desktop_config.json
Add this configuration:
{
"mcpServers": {
"ast-grep": {
"command": "mcp-ast-grep",
"args": []
}
}
}Or if building from source:
{
"mcpServers": {
"ast-grep": {
"command": "node",
"args": ["/path/to/mcp-ast-grep/dist/index.js"]
}
}
}Restart Claude Code after adding the configuration.
Usage
Once installed, Claude Code will have access to the ast_grep tool. Here are some examples:
Basic Search
// Find all console.log calls
ast_grep({
pattern: "console.log($msg)"
})Search with File Filtering
// Find console.log only in JavaScript files
ast_grep({
pattern: "console.log($msg)",
glob: "**/*.js",
language: "javascript"
})Replace with Preview (Safe)
// Preview replacing console.log with logger.info
ast_grep({
pattern: "console.log($msg)",
replacement: "logger.info($msg)",
dry_run: true // default
})Apply Changes
// Actually apply the replacement
ast_grep({
pattern: "console.log($msg)",
replacement: "logger.info($msg)",
dry_run: false
})Complex Patterns
// Convert function declarations to arrow functions
ast_grep({
pattern: "function $name($args) { $$$body }",
replacement: "const $name = ($args) => { $$$body }",
language: "javascript"
})Count Matches
// Count occurrences
ast_grep({
pattern: "console.log($msg)",
mode: "count"
})Parameters
Parameter | Type | Description | Required |
| string | AST pattern to search for | ✅ |
| string | Replacement pattern (enables replace mode) | ❌ |
| string | File or directory to search (default: cwd) | ❌ |
| string | File glob pattern (e.g., | ❌ |
| string | Target language for parsing | ❌ |
| "search" | "replace" | "count" | Operation mode | ❌ |
| number | Lines of context around matches (0-20) | ❌ |
| boolean | Preview mode (default: true for replacements) | ❌ |
| number | Limit results to first N matches (1-1000) | ❌ |
Supported Languages
JavaScript, TypeScript, Python, Rust, Go, Java, C, C++, C#, HTML, CSS, JSON, YAML, Bash, Lua, PHP, Ruby, Swift, Kotlin, Dart, Scala
Pattern Syntax
ast-grep uses powerful pattern matching syntax:
$VAR- matches any single AST node$$$VAR- matches multiple AST nodes (like function body)console.log($msg)- matches console.log with any argumentfunction $name($args) { $$$body }- matches any function declaration
For more details, see the ast-grep documentation.
Error Handling
The server provides helpful error messages for common issues:
ast-grep not found: Install with
npm install -g @ast-grep/cliInvalid pattern: Check ast-grep pattern syntax
File not found: Verify the path parameter
Language not supported: Use one of the supported languages listed above
Development
# Install dependencies
npm install
# Run in development mode
npm run dev
# Build for production
npm run build
# Start production server
npm startTroubleshooting
Tool not appearing in Claude Code
Verify installation:
claude mcp list(should show "ast-grep")Check that ast-grep CLI is installed:
ast-grep --versionRestart Claude Code after configuration changes
If using manual config, verify the JSON syntax is correct
Check Claude Code logs for MCP connection errors
Using the claude mcp CLI
# List all installed MCP servers
claude mcp list
# Check if ast-grep is installed
claude mcp status ast-grep
# Reinstall if there are issues
claude mcp remove ast-grep
claude mcp install ast-grep node /path/to/mcp-ast-grep/dist/index.js
# View Claude Code MCP configuration
claude mcp configPermission errors
Make sure the server has read/write access to the target files and directories.
Pattern not matching
Verify the pattern syntax using ast-grep CLI directly
Check that the correct language is specified
Use the playground at https://ast-grep.github.io/playground.html
License
MIT
Contributing
Contributions welcome! Please open issues for bugs or feature requests.
Available Tools
1 toolast_grepB
A powerful AST-based code search and refactoring tool that understands code structure across 20+ programming languages. Performs syntax-aware pattern matching and transformations.
Inputs: pattern (required), glob, mode, path, +5 more.
Tool Schema Changelog
Recent tool additions, removals, and schema changes observed during successful MCP inspections.
Loading tool schema changelog…
TDQS
Scored across 1 tool
With only one tool, there is no risk of confusion or overlapping purposes. The tool's description is clear and distinct by default.
A single tool name provides no opportunity for inconsistency. The naming is trivially consistent.
One tool is borderline for a server that claims to handle both search and refactoring across many languages. While the tool may be comprehensive, a single tool feels thin compared to typical servers with 3-15 tools.
The tool covers search and transformations, which are the core operations. However, there may be missing capabilities like pattern management or language-specific configurations, leading to notable gaps.
Maintenance
Related MCP Connectors
Code intelligence for coding agents: semantic, AST, graph, and full-text search. 279+ languages.
Code intelligence platform for AI agents. 20 tools for architecture, security & impact analysis.
AI-powered codebase analysis — call graphs, security, dead code, complexity. 150+ tools.
Ask a codebase what calls what: search, blast radius, paths between symbols, and diffs.
Related MCP Servers
- AlicenseBqualityDmaintenanceAdvanced code search and transformation MCP server for AI assistants. Combines ugrep's speed with intelligent replace capabilities, dry-run previews, and language-aware refactoring across 11 tools.110MIT
- AlicenseAqualityCmaintenanceEnables AI assistants to understand and navigate codebases through structural analysis. Provides code mapping, symbol search, and impact analysis using ast-grep for accurate parsing of Python, JavaScript, TypeScript, and Go projects.452MIT
- AlicenseAqualityBmaintenanceEnables AI assistants to search and analyze codebases using Abstract Syntax Tree (AST) pattern matching with ast-grep. Supports structural code search, pattern testing, and AST visualization across multiple programming languages.4489MIT
- AlicenseAqualityCmaintenanceProvides AI assistants with progressive code intelligence to explore repository structure, find symbols, and assess the impact of changes using ast-grep.3MIT