Skip to main content

AI-Powered Python Refactoring & Code Quality Assistant - MCP Server with AST-based analysis

Project description

OHM-MCP

OHM-MCP

AI-Powered Python Refactoring & Code Quality Assistant

Works with GitHub Copilot, Cursor IDE, Cline, and any MCP-compatible AI assistant.

Python 3.8+ MCP AST-Based License


โœจ 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

pip install -e .

IDE Configuration

๐Ÿ“ฆ Option 1: NPX (Recommended - Auto-installs dependencies)

๐Ÿ”ต GitHub Copilot (VS Code) with NPX

After publishing to npm:

{
  "mcpServers": {
    "ohm-mcp": {
      "command": "npx",
      "args": ["ohm-mcp@latest"]
    }
  }
}

For local development:

{
  "mcpServers": {
    "ohm-mcp": {
      "command": "npx",
      "args": ["--package", "/path/to/ohm-mcp-npm", "ohm-mcp"]
    }
  }
}

Usage:

  • Open Copilot Chat
  • Type # and select ohm-mcp tools
  • Ask: "Analyze this file and suggest refactorings"
๐ŸŸฃ Cursor IDE with NPX

After publishing to npm:

{
  "mcpServers": {
    "ohm-mcp": {
      "command": "npx",
      "args": ["ohm-mcp@latest"]
    }
  }
}

For local development:

{
  "mcpServers": {
    "ohm-mcp": {
      "command": "npx",
      "args": ["--package", "/path/to/ohm-mcp-npm", "ohm-mcp"]
    }
  }
}

Usage:

  • Open Cursor Chat (Cmd+L / Ctrl+L)
  • Tools are automatically available
  • Ask: "Use ohm-mcp to detect dead code"
๐ŸŸข Cline (VS Code Extension) with NPX

After publishing to npm:

{
  "mcpServers": {
    "ohm-mcp": {
      "command": "npx",
      "args": ["ohm-mcp@latest"]
    }
  }
}

For local development:

{
  "mcpServers": {
    "ohm-mcp": {
      "command": "npx",
      "args": ["--package", "/path/to/ohm-mcp-npm", "ohm-mcp"]
    }
  }
}

Usage:

  • Open Cline panel
  • Tools are available in agent context
  • Ask: "Analyze type coverage and suggest improvements"

๐Ÿ Option 2: Direct Python (Manual setup)

๐Ÿ”ต GitHub Copilot (VS Code) with Python

First install: pip install ohm-mcp

Then add to .vscode/mcp.json:

{
  "servers": {
    "ohm-mcp": {
      "command": "python",
      "args": ["-m", "ohm_mcp.server"]
    }
  },
  "inputs": []
}

Usage:

  • Open Copilot Chat
  • Type # and select ohm-mcp tools
  • Ask: "Analyze this file and suggest refactorings"
๐ŸŸฃ Cursor IDE with Python

First install: pip install ohm-mcp

Then add to Cursor's MCP settings file (.cursorrules or MCP config):

{
  "mcpServers": {
    "ohm-mcp": {
      "command": "python",
      "args": ["-m", "ohm_mcp.server"]
    }
  },
  "inputs": []
}

Example with virtual environment:

{
  "mcpServers": {
    "ohm-mcp": {
      "command": "/Users/username/projects/venv/bin/python",
      "args": ["-m", "ohm_mcp.server"]
    }
  }
}

Usage:

  • Open Cursor Chat (Cmd+L / Ctrl+L)
  • Tools are automatically available
  • Ask: "Use ohm-mcp to detect dead code"
๐ŸŸข Cline (VS Code Extension) with Python

First install: pip install ohm-mcp

Then add to Cline's MCP settings:

{
  "mcpServers": {
    "ohm-mcp": {
      "command": "python",
      "args": ["-m", "ohm_mcp.server"]
    }
  }
}

Example with virtual environment:

{
  "mcpServers": {
    "ohm-mcp": {
      "command": "/Users/username/projects/venv/bin/python",
      "args": ["-m", "ohm_mcp.server"]
    }
  }
}

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
  1. Verify Python path:

    which python  # Unix/Mac
    where python  # Windows
    
  2. Test MCP server directly:

    python mcp_server.py
    
  3. Check logs:

    • VS Code: Check Output panel
    • Cursor: Check Cursor logs
    • Cline: Check Cline settings panel
  4. Common issues:

    • โŒ Relative paths in command โ†’ Use absolute paths
    • โŒ Missing virtual environment โ†’ Activate venv first
    • โŒ Wrong cwd for Cline โ†’ Must be absolute path

๐Ÿค 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!

Documentation

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ohm_mcp-0.1.3.tar.gz (75.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ohm_mcp-0.1.3-py3-none-any.whl (82.3 kB view details)

Uploaded Python 3

File details

Details for the file ohm_mcp-0.1.3.tar.gz.

File metadata

  • Download URL: ohm_mcp-0.1.3.tar.gz
  • Upload date:
  • Size: 75.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for ohm_mcp-0.1.3.tar.gz
Algorithm Hash digest
SHA256 e767dd4d44cfe3348aafc995ba298ba26fb00ff817f951f757cbd65a4c3b11ae
MD5 02c329a366c48a967f9ba22bcddcb73f
BLAKE2b-256 8ac2d3090543b7f0d3e72738402a78d13b86eee9a11d2f80e5b1a46fbf14d403

See more details on using hashes here.

File details

Details for the file ohm_mcp-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: ohm_mcp-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 82.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for ohm_mcp-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 b711f7d8f0574c22900ca314d93c56b69445e3aa0cab184aff6dd5703d5bc84c
MD5 03dbcd875400a4ef07841e5fcaad6880
BLAKE2b-256 7b9bad54acb3d93ec793baf96b9811c6e075836a1cb76f581fcf6ea4d62e3123

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page