|
1 |
| -# code-analysis-mcp-server |
2 |
| -MCP server for code analysis tools like ast-grep integration. Provides a standardized interface for AI assistants to perform structural code queries and transformations. |
| 1 | +# Code Analysis MCP Server |
| 2 | +A modular Model Context Protocol (MCP) server for code analysis tools, with a primary focus on ast-grep integration. This server provides a standardized interface for AI assistants and other clients to perform structural code queries and transformations. |
| 3 | + |
| 4 | +## Features |
| 5 | +- **Structural Code Analysis**: Find patterns in code using ast-grep's powerful pattern matching capabilities |
| 6 | + |
| 7 | +- **Code Transformations**: Replace patterns in code with new implementations |
| 8 | + |
| 9 | +- **YAML Rule Support**: Apply custom lint and transformation rules defined in YAML |
| 10 | + |
| 11 | +- **Project-Wide Scanning**: Scan entire projects for patterns or rule violations |
| 12 | + |
| 13 | +- **Containerized Deployment**: Easy deployment with Docker |
| 14 | + |
| 15 | +- **Modular Architecture**: Designed for easy extension with additional code analysis tools |
| 16 | + |
| 17 | +## Requirements |
| 18 | +- Python 3.12.8 |
| 19 | + |
| 20 | +- ast-grep CLI tool |
| 21 | + |
| 22 | +- Docker (for containerized deployment) |
| 23 | + |
| 24 | +## Installation |
| 25 | +**Using pip** |
| 26 | + |
| 27 | +```bash |
| 28 | +pip install code-analysis-mcp-server |
| 29 | +``` |
| 30 | + |
| 31 | +**From source** |
| 32 | +```bash |
| 33 | +git clone https://github.com/yourusername/code-analysis-mcp-server.git |
| 34 | +cd code-analysis-mcp-server |
| 35 | +pip install -e . |
| 36 | +``` |
| 37 | + |
| 38 | +**Using Docker** |
| 39 | +```bash |
| 40 | +docker pull yourusername/code-analysis-mcp-server |
| 41 | +# or build locally |
| 42 | +docker build -t code-analysis-mcp-server . |
| 43 | +``` |
| 44 | + |
| 45 | +## Usage |
| 46 | +### Starting the server |
| 47 | +```bash |
| 48 | +# Direct execution |
| 49 | +code-analysis-server |
| 50 | + |
| 51 | +# With Docker |
| 52 | +docker run -p 8000:8000 -v /path/to/your/project:/project code-analysis-mcp-server |
| 53 | +``` |
| 54 | + |
| 55 | +### Using with Docker Compose |
| 56 | +**Create a docker-compose.yml file:** |
| 57 | +```text |
| 58 | +version: '3' |
| 59 | +
|
| 60 | +services: |
| 61 | + code-analysis-mcp: |
| 62 | + image: yourusername/code-analysis-mcp-server |
| 63 | + ports: |
| 64 | + - "8000:8000" |
| 65 | + volumes: |
| 66 | + - ${PROJECT_DIR:-./sample_project}:/project |
| 67 | + environment: |
| 68 | + - PROJECT_PATH=/project |
| 69 | + restart: unless-stopped |
| 70 | +``` |
| 71 | +**Then run:** |
| 72 | +```bash |
| 73 | +PROJECT_DIR=/path/to/your/project docker-compose up -d |
| 74 | +``` |
| 75 | + |
| 76 | +**Client Example** |
| 77 | +```python |
| 78 | +import asyncio |
| 79 | +import json |
| 80 | +from fastmcp import Client |
| 81 | +from fastmcp.client.transports import SSETransport |
| 82 | + |
| 83 | +async def main(): |
| 84 | + async with Client(transport=SSETransport("http://localhost:8000/sse")) as client: |
| 85 | + # Set project path |
| 86 | + project_result = await client.call_tool( |
| 87 | + "ast_grep_set_project_path", |
| 88 | + { |
| 89 | + "project_path": "/project" |
| 90 | + } |
| 91 | + ) |
| 92 | + |
| 93 | + # Process the result correctly |
| 94 | + content_item = project_result[0] |
| 95 | + result_data = json.loads(content_item.text) |
| 96 | + print("Project path result:", result_data) |
| 97 | + |
| 98 | + # Find patterns in a file |
| 99 | + find_result = await client.call_tool( |
| 100 | + "ast_grep_find_pattern", |
| 101 | + { |
| 102 | + "file_path": "src/example.cs", |
| 103 | + "pattern": "public class $NAME" |
| 104 | + } |
| 105 | + ) |
| 106 | + |
| 107 | + # Process results |
| 108 | + content_item = find_result[0] |
| 109 | + result_data = json.loads(content_item.text) |
| 110 | + print(f"Found {result_data.get('count', 0)} matches") |
| 111 | + |
| 112 | +if __name__ == "__main__": |
| 113 | + asyncio.run(main()) |
| 114 | +``` |
| 115 | + |
| 116 | +## Available Tools |
| 117 | +The server provides the following tools: |
| 118 | +### ast-grep |
| 119 | +- **ast_grep_set_project_path:** Set the project path for subsequent operations |
| 120 | +- **ast_grep_parse_code:** Parse code into an AST |
| 121 | +- **ast_grep_find_pattern:** Find patterns in code files |
| 122 | +- **ast_grep_replace_pattern:** Replace patterns in code files |
| 123 | +- **ast_grep_run_yaml_rule:** Run custom YAML rules on code files |
| 124 | +- **ast_grep_scan_project:** Scan entire projects for patterns or rule violations |
| 125 | +- **ast_grep_initialize_project:** Initialize a new ast-grep project |
| 126 | +- **ast_grep_test_rule:** Test ast-grep rules |
| 127 | + |
| 128 | +## Supported Languages |
| 129 | +- Python |
| 130 | +- JavaScript |
| 131 | +- TypeScript |
| 132 | +- Rust |
| 133 | +- Go |
| 134 | +- Java |
| 135 | +- C |
| 136 | +- C++ |
| 137 | +- C# |
| 138 | + |
| 139 | +## Project Structure |
| 140 | +```text |
| 141 | +code-analysis-mcp-server/ |
| 142 | +├── src/ |
| 143 | +│ ├── __init__.py |
| 144 | +│ ├── server.py # Main MCP server setup |
| 145 | +│ ├── tools/ |
| 146 | +│ │ ├── __init__.py # Tool registration |
| 147 | +│ │ ├── ast_grep/ |
| 148 | +│ │ │ ├── __init__.py |
| 149 | +│ │ │ ├── tools.py # ast-grep tool implementations |
| 150 | +│ │ └── common/ |
| 151 | +│ │ ├── __init__.py |
| 152 | +│ │ └── utils.py # Shared utilities |
| 153 | +│ └── resources/ |
| 154 | +│ ├── __init__.py |
| 155 | +│ └── status.py # System status resource |
| 156 | +├── tests/ |
| 157 | +├── pyproject.toml |
| 158 | +├── setup.py |
| 159 | +├── requirements.txt |
| 160 | +├── Dockerfile |
| 161 | +└── README.md |
| 162 | +``` |
| 163 | + |
| 164 | +## Contributing |
| 165 | +Contributions are welcome! Please feel free to submit a Pull Request. |
| 166 | + |
| 167 | +## License |
| 168 | +This project is licensed under the MIT License - see the LICENSE file for details. |
| 169 | + |
| 170 | +```text |
| 171 | +MIT License |
| 172 | +
|
| 173 | +Copyright (c) 2025 Your Name |
| 174 | +
|
| 175 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 176 | +of this software and associated documentation files (the "Software"), to deal |
| 177 | +in the Software without restriction, including without limitation the rights |
| 178 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 179 | +copies of the Software, and to permit persons to whom the Software is |
| 180 | +furnished to do so, subject to the following conditions: |
| 181 | +
|
| 182 | +The above copyright notice and this permission notice shall be included in all |
| 183 | +copies or substantial portions of the Software. |
| 184 | +
|
| 185 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 186 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 187 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 188 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 189 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 190 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 191 | +SOFTWARE. |
| 192 | +``` |
0 commit comments