AI-Powered Python Refactoring & Code Quality Assistant - MCP Server with AST-based analysis
Project description
OHM-MCP
AI-Powered Python Refactoring & Code Quality Assistant
Works with GitHub Copilot, Cursor IDE, Cline, and any MCP-compatible AI assistant.
โจ Core Capabilities
๐๏ธ Architecture
๐ง Code Quality
|
๐ Type Safety
โก Performance
๐ค Automation
|
๐ Quick Start
Installation
pip install -e .
IDE Configuration
๐ต GitHub Copilot (VS Code)
Add to .vscode/mcp.json:
{
"servers": {
"ohm-mcp": {
"command": "python",
"args": ["${workspaceFolder}/mcp_server.py"]
}
},
"inputs": []
}
Usage:
- Open Copilot Chat
- Type
#and selectohm-mcptools - Ask: "Analyze this file and suggest refactorings"
๐ฃ Cursor IDE
Add to Cursor's MCP settings file (.cursorrules or MCP config):
{
"mcpServers": {
"ohm-mcp": {
"command": "/path/to/your/python",
"args": [
"${workspaceFolder}/ohm-mcp/mcp_server.py"
]
}
},
"inputs": []
}
Example with virtual environment:
{
"mcpServers": {
"ohm-mcp": {
"command": "/Users/username/projects/venv/bin/python",
"args": [
"${workspaceFolder}/ohm-mcp/mcp_server.py"
]
}
}
}
Usage:
- Open Cursor Chat (Cmd+L / Ctrl+L)
- Tools are automatically available
- Ask: "Use ohm-mcp to detect dead code"
๐ข Cline (VS Code Extension)
Add to Cline's MCP settings:
{
"mcpServers": {
"ohm-mcp": {
"command": "/path/to/your/python",
"args": ["mcp_server.py"],
"cwd": "/absolute/path/to/ohm-mcp"
}
}
}
Example with virtual environment:
{
"mcpServers": {
"ohm-mcp": {
"command": "/Users/username/projects/venv/bin/python",
"args": ["mcp_server.py"],
"cwd": "/Users/username/projects/ohm-mcp"
}
}
}
Note: Cline requires absolute paths for both command and cwd.
Usage:
- Open Cline panel
- Tools are available in agent context
- Ask: "Analyze type coverage and suggest improvements"
๐ง Other MCP-Compatible Clients
Any MCP-compatible client can use this server. General configuration:
{
"mcpServers": {
"ohm-mcp": {
"command": "<python-interpreter-path>",
"args": ["<path-to-mcp_server.py>"],
"cwd": "<project-directory>"
}
}
}
Finding your Python path:
# Unix/Mac
which python
# or
which python3
# Windows
where python
๐ฏ Key Tools
๐๏ธ Architecture & Design (4 tools)
| Tool | Purpose | Output |
|---|---|---|
analyze_architecture |
Detect God Objects, SOLID violations | Detailed issue report |
suggest_design_patterns |
Recommend patterns (Strategy, Factory, Observer) | Pattern suggestions + examples |
analyze_tight_coupling |
Find coupling issues | DI recommendations |
suggest_di_refactor |
Generate DI code | Before/after refactor |
๐ง Code Quality & Refactoring (6 tools)
| Tool | Purpose | Key Feature |
|---|---|---|
extract_method_ast |
Extract code into function | 100% AST-based accuracy |
suggest_extractable_methods |
Find extractable blocks | Cohesion scoring |
detect_dead_code |
Find unused code | 5 types of dead code |
refactor_imports |
Update imports project-wide | Safe module renaming |
rename_symbol |
Rename across codebase | Conflict detection |
detect_code_duplicates |
Find DRY violations | Exact + near duplicates |
Example - Extract Method:
# Input: Lines 45-60
result = extract_method_ast(code, 45, 60, "calculate_total")
# Output: Refactored code + patch + auto-detected params/returns
๐ Type Safety & Testing (4 tools)
| Tool | Purpose | Benefit |
|---|---|---|
analyze_type_hints |
Check type coverage | Migration plan |
generate_type_stub |
Create .pyi files | Gradual typing |
generate_characterization_tests |
Auto-generate tests | Safe refactoring |
generate_test_for_function |
Single function tests | Targeted testing |
โก Performance & Coverage (2 tools)
| Tool | Purpose | Detects |
|---|---|---|
analyze_performance |
Find bottlenecks | Nested loops, mutable defaults, O(nยฒ) |
prioritize_by_coverage |
Risk-based prioritization | High-risk uncovered code |
๐ค Automated Execution (3 tools)
graph LR
A[apply_refactoring] --> B{Dry Run?}
B -->|Yes| C[Show Preview]
B -->|No| D[Create Backup]
D --> E[Apply Changes]
E --> F{Run Tests}
F -->|Pass| G[Success]
F -->|Fail| H[Auto Rollback]
H --> I[rollback_refactoring]
Features:
- โ Automatic backup before changes
- โ Test execution validation
- โ Auto-rollback on failure
- โ
Full audit trail (
show_refactoring_history)
๐ Metrics & Reporting (1 tool)
generate_quality_report - Comprehensive dashboard in HTML/Markdown/JSON
Output Preview:
๐ Health Score: 85/100 (Good)
๐ Files: 47 | Lines: 12,450 | Tech Debt: 23 pts
๐ Type Coverage: 67%
๐๏ธ Dead Code: 8 imports, 12 variables, 3 functions
โก Performance: 4 nested loops, 2 mutable defaults
๐ Duplication: 3 exact, 5 near-duplicates
Visual Dashboard:
- ๐จ Circular health gauge
- ๐ Color-coded metrics (๐ข๐ก๐ด)
- ๐ Trend tracking ready
- ๐ CI/CD integration (JSON export)
๐ก Common Workflows
1๏ธโฃ Safe Refactoring
generate_characterization_tests โ pytest โ extract_method_ast โ pytest
2๏ธโฃ Eliminate Duplication
detect_code_duplicates โ review suggestions โ extract_method_ast
3๏ธโฃ Type Migration
analyze_type_hints โ follow migration plan โ generate_type_stub
4๏ธโฃ Performance Optimization
analyze_performance โ prioritize_by_coverage โ apply fixes
5๏ธโฃ Module Refactoring
refactor_imports(old="myapp.old", new="myapp.new") โ review patches
6๏ธโฃ Symbol Renaming
rename_symbol(old="calc", new="calculate", preview_only=True) โ apply
7๏ธโฃ Quality Tracking
generate_quality_report(format="html") โ open dashboard โ track trends
๐จ Visual Examples
Quality Dashboard Preview
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ ๐ Code Quality Dashboard โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ โ
โ โญโโโโโโโโโโฎ ๐ Overview โ
โ โ 85 โ Files: 47 โ
โ โ /100 โ Lines: 12,450 โ
โ โฐโโโโโโโโโโฏ Tech Debt: 23 โ
โ Health Score โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ ๐ Type Coverage โก Performance โ
โ โโโโโโโโโโ 67% ๐ด 4 nested loops โ
โ 120/180 typed ๐ก 2 mutable args โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ ๐๏ธ Dead Code ๐ Duplication โ
โ 8 imports 3 exact โ
โ 12 variables 5 near โ
โ 3 functions โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Symbol Rename Preview
# Before
- def calc(x, y):
- return x + y
- result = calc(5, 3)
# After
+ def calculate_sum(x, y):
+ return x + y
+ result = calculate_sum(5, 3)
โ
1 function renamed
โ
3 call sites updated
โ
0 conflicts detected
๐ง Design Principles
| Principle | Implementation |
|---|---|
| ๐งช Test-First | Auto-generate characterization tests before refactoring |
| โฉ๏ธ Reversible | Every change = backup + rollback capability |
| ๐ฏ AST-Driven | 100% accurate (no regex) |
| ๐ Risk-Aware | Coverage + complexity = prioritization |
| ๐๏ธ SOLID | Detect violations + concrete fixes |
| ๐ซ No Blindness | Analyze โ Plan โ Validate |
๐ IDE Compatibility
|
๐ค Any MCP Client โ Standard Protocol โ Easy Setup โ Works with all MCP-compatible AI assistants |
๐ Comparison
| Feature | OHM MCP | Traditional Tools |
|---|---|---|
| Accuracy | 100% AST | ~70% Regex |
| Safety | Auto backup/rollback | Manual |
| Testing | Auto-generates | Manual |
| Automation | Full | Suggestions only |
| Dashboard | HTML/JSON/MD | Text logs |
| IDE Support | Copilot/Cursor/Cline | Limited |
๐ฏ Use Cases
| ๐จโ๐ป Developers โข Refactor legacy code safely โข Find dead code โข Optimize performance |
๐ฅ Teams โข Track tech debt โข Enforce standards โข Design patterns |
๐ CI/CD โข Quality gates โข Trend tracking โข Block bad PRs |
๐ Metrics
โ
13 Advanced Capabilities
โ
30+ Static Checks
โ
100% AST Accuracy
โ
Zero Regex Patterns
โ
Automated Execution
โ
Beautiful Dashboards
โ
Universal MCP Compatibility
๐ ๏ธ Troubleshooting
MCP Connection Issues
-
Verify Python path:
which python # Unix/Mac where python # Windows
-
Test MCP server directly:
python mcp_server.py -
Check logs:
- VS Code: Check Output panel
- Cursor: Check Cursor logs
- Cline: Check Cline settings panel
-
Common issues:
- โ Relative paths in
commandโ Use absolute paths - โ Missing virtual environment โ Activate venv first
- โ Wrong
cwdfor Cline โ Must be absolute path
- โ Relative paths in
๐ค Contributing
Run before submitting:
./static_analyser.sh # Runs ruff, mypy, pylint, flake8
pytest # All tests must pass
๐ Credits
Built with Model Context Protocol โข Python AST โข Compatible with GitHub Copilot, Cursor IDE, Cline
Made with โค๏ธ for better code quality
โญ Star this repo if it helps you write cleaner code!
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ohm_mcp-0.1.1.tar.gz.
File metadata
- Download URL: ohm_mcp-0.1.1.tar.gz
- Upload date:
- Size: 75.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
56d1bc727afe2b077a83c8a843595e40ebff18fd203ad049aecf4b25ed8394e2
|
|
| MD5 |
348309353f56fec044e062220ee2dbfc
|
|
| BLAKE2b-256 |
076274d6d5c82855aec356339244eaca7f5f02f8b8debefb56272be9e0db8228
|
File details
Details for the file ohm_mcp-0.1.1-py3-none-any.whl.
File metadata
- Download URL: ohm_mcp-0.1.1-py3-none-any.whl
- Upload date:
- Size: 82.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2c553520de01aa6ff50071d7c2dcb043da4fdc69658317a883490022bef92d83
|
|
| MD5 |
53190302f0857115623ea6587d3a64df
|
|
| BLAKE2b-256 |
2ac047917b10a7c722468beaf2b601e297888539f65cd4d19b16542af125fe49
|