Provides AI-powered Python refactoring and code quality analysis tools through MCP, including architecture analysis, dead code detection, type safety checks, and automated refactoring with rollback capabilities.
Generates code quality reports and documentation in Markdown format for easy reading and integration with documentation systems.
Includes Mermaid diagrams for visualizing refactoring workflows and automated execution processes.
Integrates with pytest for automated test validation during refactoring operations, enabling safe code transformations with automatic rollback on test failures.
Offers comprehensive Python code analysis and refactoring capabilities including AST-based method extraction, SOLID violation detection, type hint analysis, performance optimization, and automated test generation.
Uses Ruff as part of the static analysis toolchain for code quality checks before submitting contributions.
Click on "Install 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., "@OHM-MCPanalyze this Python file for SOLID violations and dead code"
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.
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
God Object Detection
SOLID Violation Analysis
Design Pattern Suggestions
Dependency Injection Refactoring
๐ง Code Quality
AST Extract Method (100% accurate)
Dead Code Elimination
Import Refactoring
Symbol Renaming (project-wide)
Duplication Detection
๐ Type Safety
Type Coverage Analysis
Type Stub Generation
Auto Test Generation
โก Performance
O(nยฒ) Pattern Detection
Hotspot Analysis
Coverage-Driven Prioritization
๐ค Automation
Auto-Apply with Rollback
Operation History
Quality Dashboard
๐ Quick Start
Installation
๐ฆ Recommended: NPM Method (Auto-installs dependencies)
No installation required! Use NPX to run the latest version automatically:
This is the easiest way to get started. Just add the configuration to your AI assistant (see IDE Configuration below).
๐ Alternative: PyPI Method (For local development)
Use this method when you need to:
Develop locally with the Python package
Use a specific Python virtual environment
Install from source for customization
Install from PyPI:
Install from source (for development):
IDE Configuration
๐ฆ Option 1: NPX (Recommended - Auto-installs dependencies)
After publishing to npm:
For local development:
Usage:
Open Copilot Chat
Type
#and selectohm-mcptoolsAsk: "Analyze this file and suggest refactorings"
Global (After publishing to npm):
For local development:
Usage:
Open Cursor Chat (Cmd+L / Ctrl+L)
Tools are automatically available
Ask: "Use ohm-mcp to detect dead code"
Global (After publishing to npm):
For local development:
Usage:
Open Cline panel
Tools are available in agent context
Ask: "Analyze type coverage and suggest improvements"
Global (After publishing to npm):
Usage:
Tools are automatically available in Antigravity
Ask: "Use ohm-mcp to analyze architecture"
Global (After publishing to npm):
Usage:
Open Roo Code panel
Tools are available in agent context
Ask: "Use ohm-mcp to detect duplicates"
Configuration (config.toml):
Usage:
Open Codex panel
Tools are automatically available
Ask: "Use ohm-mcp to suggest design patterns"
Local (For local development with virtual environment):
Global (After publishing to npm):
Usage:
Open Kilo Code panel
Tools are available in agent context
Ask: "Use ohm-mcp for refactoring"
๐ Option 2: Direct Python (Manual setup)
First install: pip install ohm-mcp
Then add to .vscode/mcp.json:
Usage:
Open Copilot Chat
Type
#and selectohm-mcptoolsAsk: "Analyze this file and suggest refactorings"
First install: pip install ohm-mcp
Then add to Cursor's MCP settings file (.cursorrules or MCP config):
Example with virtual environment:
Usage:
Open Cursor Chat (Cmd+L / Ctrl+L)
Tools are automatically available
Ask: "Use ohm-mcp to detect dead code"
First install: pip install ohm-mcp
Then add to Cline's MCP settings:
Example with virtual environment:
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"
Any MCP-compatible client can use this server. General configuration:
Finding your Python path:
๐ก Quick Usage Examples
Once configured, simply reference tools in your AI assistant chat using the format: use #ohm-mcp.tool_name on the current file or @file_name.py
๐๏ธ Find Dead Code
This will detect:
โ Unused imports (import statements never referenced)
โ Unused variables (assigned but never read)
โ Unreachable code (after return/raise/break/continue)
โ Unused functions (defined but never called)
โ Shadowed variables (inner scope hides outer scope variable)
๐ Code Duplication Detection
Finds:
โ Exact duplicates (100% identical code blocks)
โ Near duplicates (90%+ similarity)
โ Duplicate functions (same structure, different names)
โ Provides refactoring suggestions to eliminate duplication
๐๏ธ Architecture Analysis
Detects:
โ God Objects (classes doing too much)
โ SOLID principle violations
โ Circular dependencies
โ High coupling issues
โ๏ธ Extract Method Refactoring
Automatically:
โ Detects required parameters
โ Identifies return values
โ Generates refactored code
โ Creates unified diff patch
๐ Safe Symbol Renaming
Features:
โ AST-based (100% accurate)
โ Detects naming conflicts
โ Shows all occurrences before applying
โ Updates docstrings and comments
๐ Type Coverage Analysis
Provides:
โ Coverage percentage and grade
โ Functions missing type hints
โ Suggested type annotations
โ Migration plan with priorities
โก Performance Optimization
Detects:
โ Nested loops (O(nยฒ) complexity)
โ Quadratic list operations
โ Repeated function calls (missing caching)
โ Mutable default arguments
โ Inefficient string concatenation
๐งช Auto-Generate Tests
Creates:
โ Happy path test cases
โ Edge cases (None, zero, negative, empty)
โ Ready-to-run pytest code
โ Preserves current behavior before refactoring
๐จ Design Pattern Suggestions
Recommends:
โ Strategy pattern for long if/elif chains
โ Factory pattern for repetitive object creation
โ Observer pattern for callback hell
โ Decorator pattern for cross-cutting concerns
๐ง Import Refactoring
Handles:
โ Direct imports (
import old.module)โ From imports (
from old.module import X)โ Submodule imports
โ Import aliases
๐ฏ Key Tools (30 Total)
Tool | Purpose | Output |
| Comprehensive code analysis | Issues + refactoring plan |
| Function-level refactor planning | Detailed refactor proposal |
| Explain refactoring patterns | Educational guidance |
| Generate unified diff patches | Patch file |
Tool | Purpose | Output |
| Detect God Objects, SOLID violations | Detailed issue report |
| Recommend patterns (Strategy, Factory, Observer) | Pattern suggestions + examples |
| Find coupling issues | DI recommendations |
| Generate DI code | Before/after refactor |
Tool | Purpose | Key Feature |
| Extract code into function | 100% AST-based accuracy |
| Find extractable blocks | Cohesion scoring |
| Find unused code | 5 types of dead code |
| Update imports project-wide | Safe module renaming |
| Refactor imports in one file | Single file focus |
| Find wildcard imports | Explicit replacements |
| Rename across codebase | Conflict detection |
| Find DRY violations | Exact + near duplicates |
| Extract single function code | Code extraction utility |
| Apply function-level refactor | Direct code modification |
Example - Extract Method:
Tool | Purpose | Benefit |
| Check type coverage | Migration plan |
| Create .pyi files | Gradual typing |
| Auto-generate tests | Safe refactoring |
| Single function tests | Targeted testing |
| Suggest test strategies | Test planning |
Tool | Purpose | Detects |
| Find bottlenecks | Nested loops, mutable defaults, O(nยฒ) |
| Risk-based prioritization | High-risk uncovered code |
Tool | Purpose |
| Auto-apply refactoring with safety checks |
| Rollback previous refactoring |
| View refactoring audit trail |
| Clean up old backup files |
Features:
โ Automatic backup before changes
โ Test execution validation
โ Auto-rollback on failure
โ Full audit trail with history
โ Automatic backup cleanup
generate_quality_report - Comprehensive dashboard in HTML/Markdown/JSON
Output Preview:
Visual Dashboard:
๐จ Circular health gauge
๐ Color-coded metrics (๐ข๐ก๐ด)
๐ Trend tracking ready
๐ CI/CD integration (JSON export)
๐ก Common Workflows
1๏ธโฃ Safe Refactoring
2๏ธโฃ Eliminate Duplication
3๏ธโฃ Type Migration
4๏ธโฃ Performance Optimization
5๏ธโฃ Module Refactoring
6๏ธโฃ Symbol Renaming
7๏ธโฃ Quality Tracking
๐จ Visual Examples
Quality Dashboard Preview
Symbol Rename Preview
๐ง 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
๐ 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
๐ Metrics
๐ ๏ธ Troubleshooting
Verify Python path:
which python # Unix/Mac where python # WindowsTest MCP server directly:
python -m ohm_mcp.serverCheck 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
๐ค Contributing
Run before submitting:
๐ 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!