An MCP (Model Context Protocol) server that provides code structure and semantic analysis capabilities through Abstract Syntax Trees (AST) and Abstract Semantic Graphs (ASG).
- Parse code into Abstract Syntax Trees (AST)
- Generate Abstract Semantic Graphs (ASG) from code
- Analyze code structure and complexity
- Support for multiple programming languages (Python, JavaScript)
- Compatible with Claude Desktop and other MCP clients
- Incremental parsing for faster processing of large files
- Enhanced scope handling and more complete semantic analysis
- AST diffing to identify changes between code versions
- Clone this repository:
git clone https://github.com/yourusername/ast-mcp-server.git
cd ast-mcp-server
- Set up the environment using
uv
:
# Install uv if you don't have it already
# pip install uv
# Create and activate virtual environment
uv venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
# Install dependencies
uv pip install -r requirements.txt
- Build the parsers:
uv run build_parsers.py
- Configure Claude Desktop to use the server. The easiest way is to use the provided
claude_desktop_config.json
file:
{
"mcpServers": {
"AstAnalyzer": {
"command": "uv",
"args": [
"--directory", "/absolute/path/to/ast-mcp-server",
"run", "server.py"
]
}
}
}
-
Make sure to replace
/absolute/path/to/ast-mcp-server
with the actual absolute path on your system. -
Add this configuration to your Claude Desktop config:
- On macOS:
~/Library/Application Support/claude-desktop/claude_desktop_config.json
- On Linux:
~/.config/claude-desktop/claude_desktop_config.json
- On Windows:
%APPDATA%\claude-desktop\claude_desktop_config.json
- On macOS:
-
Restart Claude Desktop to load the new MCP server.
-
In Claude Desktop, you can now use the AST-based code analysis tools.
The AST MCP Server is working correctly. Here's a summary of the features I've verified:
Basic AST Parsing ✓
Successfully parsed Python code into a detailed abstract syntax tree The structure shows proper node hierarchy with types, positions, and content
ASG Generation ✓
Generated an Abstract Semantic Graph with nodes and edges The graph correctly shows relationships between components
Code Analysis ✓
Successfully analyzed code structure, identifying:
Functions (with names, locations, and parameters) Classes Complexity metrics
Resource Caching ✓
The parse_and_cache function worked correctly A URI was generated for retrieving the parsed AST later
Multi-language Support ✓
Successfully parsed both Python and JavaScript code The supported_languages tool confirmed Python and JavaScript are available
The server is fully operational and all key functionality works as expected. When integrated with Claude Desktop using the updated configuration in claude_desktop_config.json, it will provide powerful code analysis capabilities.
To run the server in development mode with the MCP inspector:
# Using the included script
./dev_server.sh
# Or manually
uv run -m mcp dev server.py
The server provides the following tools:
parse_to_ast
: Parse code into an Abstract Syntax Treegenerate_asg
: Generate an Abstract Semantic Graph from codeanalyze_code
: Analyze code structure and complexitysupported_languages
: Get the list of supported programming languagesparse_and_cache
: Parse code into an AST and cache it for resource accessgenerate_and_cache_asg
: Generate an ASG and cache it for resource accessanalyze_and_cache
: Analyze code and cache the results for resource access
parse_to_ast_incremental
: Parse code with incremental support for faster processinggenerate_enhanced_asg
: Generate an enhanced ASG with better scope handlingdiff_ast
: Find differences between two versions of codefind_node_at_position
: Locate a specific node at a given line and columnparse_and_cache_incremental
: Parse code incrementally and cache the resultsgenerate_and_cache_enhanced_asg
: Generate an enhanced ASG and cache itast_diff_and_cache
: Generate an AST diff and cache it
To add support for additional languages:
- Install the corresponding tree-sitter language package:
uv pip install tree-sitter-<language>
-
Update the
LANGUAGE_MODULES
dictionary inbuild_parsers.py
andast_mcp_server/tools.py
. -
Run
uv run build_parsers.py
to initialize the new language.
The AST MCP Server connects with Claude Desktop through the Model Context Protocol (MCP). When launched:
- Claude Desktop starts the server using
uv run
with the appropriate working directory - The server loads tree-sitter language modules for parsing various programming languages
- It registers tools and resources with the MCP protocol
- Claude can then access these tools to analyze code you share in the chat
All tool execution happens locally on your machine, with results returned to Claude for interpretation.
MIT