ast-grep MCP Server

ast-grep MCP Server

Enables structural code search and refactoring across 20+ programming languages using ast-grep patterns, with safe dry-run mode and flexible filtering.

Category
访问服务器

README

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

Prerequisites

  1. Node.js (>= 18.0.0)
  2. ast-grep CLI: Install globally with npm install -g @ast-grep/cli
  3. Claude 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.js

Manual Installation

Option 1: npm install (if published)

npm install -g mcp-ast-grep
claude mcp install ast-grep mcp-ast-grep

Option 2: Build from source

git clone <repository-url>
cd mcp-ast-grep
npm install
npm run build

Configuration

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-grep

Manual 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
pattern string AST pattern to search for
replacement string Replacement pattern (enables replace mode)
path string File or directory to search (default: cwd)
glob string File glob pattern (e.g., **/*.js)
language string Target language for parsing
mode "search" | "replace" | "count" Operation mode
context number Lines of context around matches (0-20)
dry_run boolean Preview mode (default: true for replacements)
head_limit 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 argument
  • function $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/cli
  • Invalid 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 start

Troubleshooting

Tool not appearing in Claude Code

  1. Verify installation: claude mcp list (should show "ast-grep")
  2. Check that ast-grep CLI is installed: ast-grep --version
  3. Restart Claude Code after configuration changes
  4. If using manual config, verify the JSON syntax is correct
  5. 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 config

Permission 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.

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选