mcp-flag-sweeper

hbg/mcp-flag-sweeper

3.3

If you are the rightful owner of mcp-flag-sweeper and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to henry@mcphub.com.

Flag Sweeper is a Model Context Protocol (MCP) server designed to automate the cleanup of feature flags in codebases, reducing technical debt and improving code maintainability.

Tools
2
Resources
0
Prompts
0
Screenshot 2025-09-09 at 7 46 55โ€ฏPM

Flag Sweeper ๐Ÿšฉ

The Problem

Feature flags are essential for modern software development, but they create a hidden technical debt crisis:

  • ๐Ÿดโ€โ˜ ๏ธ Dead flags remain in production code for months/years
  • ๐Ÿ”€ Branching complexity makes code harder to understand and maintain
  • ๐Ÿ› Bug risk from outdated conditional logic
  • ๐Ÿš€ Performance overhead from unnecessary runtime checks
  • ๐Ÿ˜ค Developer frustration navigating flag-polluted codebases

The reality: Most feature flags should be temporary, but manual cleanup is time-consuming and error-prone.

The Solution

A Model Context Protocol (MCP) server that uses Polyglot Piranha to automatically clean up feature flags in your codebase. Transform feature flag calls into their final values based on flag states defined in flags.json.

๐ŸŽฏ Vision: Connect to live feature flag systems (LaunchDarkly, Split.io, custom experimentation MCP) to automatically identify and safely remove stable flags from your codebase.

โœจ What This Does

Before (Feature Flag Hell):

if isFeatureEnabled("beta_ui") {
    renderBetaUI()
} else {
    renderOldUI()
}

if client.GetString("new_checkout") == "true" {
    processNewCheckout()
} else {
    processLegacyCheckout()
}

After (Clean, Maintainable Code):

if true {
    renderBetaUI()
} else {
    renderOldUI()
}

if false {
    processNewCheckout()
} else {
    processLegacyCheckout()
}

Next step: Let your compiler/optimizer remove the dead branches entirely!

๐Ÿš€ Quick Start

  1. Install dependencies:

    pip install fastmcp polyglot-piranha
    
  2. Configure in Cursor: Add to ~/.cursor/mcp.json:

    {
      "mcpServers": {
        "mcp-piranha": {
          "command": "python3",
          "args": ["-m", "mcp-piranha"],
          "cwd": "/path/to/your/project",
          "env": {
            "PYTHONPATH": "/path/to/your/project"
          }
        }
      }
    }
    
  3. Create flags.json in your project:

    {
      "functions": [
        "isFeatureEnabled",
        "client.GetString",
        "isEnabled",
        "getFlag",
        "is_feature_enabled"
      ],
      "flags": {
        "beta_ui": {
          "value": true,
          "description": "Enables the new beta user interface",
          "replace_with": true
        },
        "new_checkout": {
          "value": false,
          "description": "New payment processing flow",
          "replace_with": false
        }
      }
    }
    

๐Ÿ› ๏ธ MCP Tools

list_flags

Loads and parses feature flags from flags.json in your project directory.

Parameters:

  • working_directory (optional): Directory to search for flags.json

Returns:

  • flags: List of flag names
  • flag_details: Detailed flag information
  • global_patterns: Function patterns for detection
  • source_file: Path to the loaded flags.json

apply_rewrite

Transforms code by replacing feature flag calls with their final values.

Parameters:

  • code: Source code to transform
  • language: Programming language (go, java, python, etc.)
  • flag_name (optional): Specific flag to clean up
  • rules (optional): Custom Piranha rules
  • edges (optional): Custom rule edges

Returns:

  • transformed_code: The transformed code
  • message: Status message

๐Ÿ“ flags.json Format

JSON format for defining feature flags and detection patterns:

{
  "functions": ["function1", "function2", "function3"],
  "flags": {
    "flag_name": {
      "value": true,
      "description": "Flag description",
      "replace_with": true
    }
  }
}

Format Details

Functions Array:

  • Array of function names to detect
  • Examples: "isFeatureEnabled", "client.GetString", "isEnabled"
  • Supports any function that takes the flag name as a string parameter

Flags Object:

  • Object with flag names as keys
  • Each flag contains:
    • value: Current state (true/false)
    • description: Human-readable description
    • replace_with: What to replace with when cleaning up

Example

{
  "functions": [
    "isFeatureEnabled",
    "client.GetString",
    "isEnabled",
    "getFlag",
    "is_feature_enabled",
    "get_flag",
    "flag_manager.is_feature_enabled",
    "config.getBoolean",
    "settings.isEnabled"
  ],
  "flags": {
    "beta_ui": {
      "value": true,
      "description": "Enables the new beta user interface with modern design elements",
      "replace_with": true
    },
    "new_checkout_flow": {
      "value": false,
      "description": "New payment processing flow with improved UX",
      "replace_with": false
    },
    "feature_flag": {
      "value": true,
      "description": "Generic feature flag for testing purposes",
      "replace_with": true
    },
    "legacy_auth": {
      "value": false,
      "description": "Legacy authentication system (deprecated)",
      "replace_with": false
    },
    "debug_mode": {
      "value": true,
      "description": "Enables debug logging and additional error information",
      "replace_with": true
    }
  }
}

๐Ÿ”ง How It Works

  1. Pattern Detection: Uses global function patterns to find feature flag calls
  2. Flexible Matching: Supports various function signatures:
    • isFeatureEnabled("flag_name")
    • isFeatureEnabled("flag_name", arg2)
    • isFeatureEnabled(arg1, "flag_name")
    • isFeatureEnabled(arg1, "flag_name", arg3)
  3. Rule Generation: Creates Piranha rules for each function pattern
  4. Code Transformation: Replaces flag calls with their final values

๐Ÿงช Testing

Run tests in the tests/ directory:

python3 tests/test_multilayered.py

๐Ÿ“ Project Structure

mcp-piranha/
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ””โ”€โ”€ server.py          # Main MCP server
โ”œโ”€โ”€ tests/
โ”‚   โ”œโ”€โ”€ test_multilayered.py
โ”‚   โ””โ”€โ”€ test_*.py          # Additional test files
โ”œโ”€โ”€ __main__.py            # Entry point
โ”œโ”€โ”€ flags.json             # Feature flag definitions
โ”œโ”€โ”€ motivation.png         # The problem we're solving
โ””โ”€โ”€ README.md

๐ŸŽฏ Supported Languages

  • Go - isFeatureEnabled(), client.GetString(), custom patterns
  • Java - config.getBoolean(), featureManager.isEnabled()
  • Python - is_feature_enabled(), get_flag()
  • JavaScript/TypeScript - Various flag checking patterns
  • C# - Config-based flag patterns
  • And more - Extensible via Polyglot Piranha's rule system

๐Ÿ” Real-World Impact

Code Complexity Reduction

Before: Navigating nested flag conditions

if is_feature_enabled("new_dashboard"):
    if is_feature_enabled("advanced_analytics"):
        render_advanced_dashboard()
    else:
        render_basic_new_dashboard()
else:
    render_legacy_dashboard()

After: Clear, linear logic

if true:
    if true:
        render_advanced_dashboard()
    else:
        render_basic_new_dashboard()
else:
    render_legacy_dashboard()

Performance Benefits

  • โŒ Runtime flag evaluation (database/API calls)
  • โœ… Compile-time constants (zero runtime overhead)
  • ๐Ÿš€ Dead code elimination by compilers/bundlers

๐Ÿ”ฎ Future Vision: Enterprise Integration

graph LR
    A[Feature Flag System<br/>LaunchDarkly/Split.io] --> B[Safety Analysis<br/>Rollout % / Stability]
    B --> C[MCP Piranha<br/>Code Transformation]
    C --> D[Clean Codebase<br/>Zero Technical Debt]

Coming Soon:

  • ๐Ÿ”Œ Live flag system integration - Connect directly to LaunchDarkly, Split.io, Unleash
  • ๐Ÿ›ก๏ธ Safety analysis - Only remove flags with 95%+ rollout and 30+ day stability
  • ๐Ÿ”„ Automated workflows - CI/CD integration with approval processes
  • ๐Ÿ“Š Impact analysis - Preview changes before applying
  • ๐ŸŽฏ Bulk operations - Clean entire codebases safely

๐Ÿšจ Troubleshooting

  • "No transformations applied": Check that flags.json exists and contains the flag
  • "Connection closed": Restart Claude Code or the MCP server
  • "Invalid tree-sitter query": Ensure function patterns are valid identifiers

๐Ÿ’ก Why This Matters

"Feature flags are a powerful tool, but without proper cleanup, they become technical debt that compounds over time. This project automates the tedious but critical task of flag removal, keeping codebases clean and maintainable."

The goal: Make feature flag cleanup so easy and safe that it becomes a standard part of every development workflow.