Skip to content

Flapi Bridge API Reference

Auto-generated API documentation for the Flapi Bridge module.

Overview

The Flapi Bridge provides communication between the external MCP server and FL Studio's internal Python environment using Flapi. Flapi uses virtual MIDI ports to forward Python API calls to FL Studio.

FLStudioBridge

FLStudioBridge()

Bridge for communicating with FL Studio via Flapi.

This class manages the Flapi connection and provides methods to execute FL Studio API calls through the Flapi bridge.

Initialize the FL Studio bridge.

Source code in src/fruityloops_mcp/flapi_bridge.py
def __init__(self) -> None:
    """Initialize the FL Studio bridge."""
    self._enabled = False
    self._connected = False

is_available property

Check if Flapi library is available.

is_enabled property

Check if Flapi is currently enabled.

is_connected property

Check if connected to FL Studio via Flapi.

enable()

Enable Flapi connection to FL Studio.

This must be called before any FL Studio API calls can be made. Flapi modifies the FL Studio API stubs to forward calls to FL Studio via MIDI.

Returns:

Type Description
bool

True if Flapi was enabled successfully, False otherwise.

Source code in src/fruityloops_mcp/flapi_bridge.py
def enable(self) -> bool:
    """Enable Flapi connection to FL Studio.

    This must be called before any FL Studio API calls can be made.
    Flapi modifies the FL Studio API stubs to forward calls to FL Studio
    via MIDI.

    Returns:
        True if Flapi was enabled successfully, False otherwise.
    """
    if not _flapi_available:
        logger.error("Cannot enable Flapi: library not installed")
        return False

    if self._enabled:
        logger.debug("Flapi already enabled")
        return True

    try:
        flapi.enable()
        self._enabled = True
        self._connected = True
        logger.info("Flapi enabled - FL Studio API calls will be forwarded to FL Studio")
        return True
    except Exception as e:
        logger.error(f"Failed to enable Flapi: {e}")
        self._enabled = False
        self._connected = False
        return False

disable()

Disable Flapi connection.

After calling this, FL Studio API calls will no longer be forwarded to FL Studio.

Source code in src/fruityloops_mcp/flapi_bridge.py
def disable(self) -> None:
    """Disable Flapi connection.

    After calling this, FL Studio API calls will no longer be forwarded
    to FL Studio.
    """
    if not _flapi_available:
        return

    if not self._enabled:
        return

    try:
        flapi.disable()
        self._enabled = False
        self._connected = False
        logger.info("Flapi disabled")
    except Exception as e:
        logger.error(f"Error disabling Flapi: {e}")

test_connection()

Test if the connection to FL Studio is working.

Returns:

Type Description
bool

True if connected and able to communicate with FL Studio.

Source code in src/fruityloops_mcp/flapi_bridge.py
def test_connection(self) -> bool:
    """Test if the connection to FL Studio is working.

    Returns:
        True if connected and able to communicate with FL Studio.
    """
    if not self._enabled:
        return False

    try:
        # Try to get FL Studio version as a connection test
        import general

        version = general.getVersion()
        logger.info(f"Connected to FL Studio version: {version}")
        return True
    except Exception as e:
        logger.warning(f"FL Studio connection test failed: {e}")
        self._connected = False
        return False

connection()

Context manager for Flapi connection.

Usage

with bridge.connection() as bridge: # FL Studio API calls here pass

Yields:

Type Description
FLStudioBridge

The FLStudioBridge instance.

Source code in src/fruityloops_mcp/flapi_bridge.py
@contextmanager
def connection(self) -> Generator["FLStudioBridge", None, None]:
    """Context manager for Flapi connection.

    Usage:
        with bridge.connection() as bridge:
            # FL Studio API calls here
            pass

    Yields:
        The FLStudioBridge instance.
    """
    try:
        self.enable()
        yield self
    finally:
        self.disable()

Helper Functions

get_bridge()

Get the global FLStudioBridge instance.

Returns:

Type Description
FLStudioBridge

The global FLStudioBridge instance.

Source code in src/fruityloops_mcp/flapi_bridge.py
def get_bridge() -> FLStudioBridge:
    """Get the global FLStudioBridge instance.

    Returns:
        The global FLStudioBridge instance.
    """
    global _bridge
    if _bridge is None:
        _bridge = FLStudioBridge()
    return _bridge

Usage Example

from fruityloops_mcp.flapi_bridge import get_bridge

# Get the global bridge instance
bridge = get_bridge()

# Enable Flapi connection
if bridge.enable():
    print("Connected to FL Studio!")

    # Control FL Studio
    print(bridge.transport_start())
    print(bridge.transport_get_bpm())

    # Disconnect when done
    bridge.disable()

Context Manager Usage

from fruityloops_mcp.flapi_bridge import FLStudioBridge

bridge = FLStudioBridge()

with bridge.connection():
    # FL Studio API calls here
    bridge.transport_start()
    bridge.mixer_set_track_volume(1, 0.8)

Transport Methods

  • transport_start() - Start playback
  • transport_stop() - Stop playback
  • transport_record() - Toggle recording
  • transport_get_song_pos() - Get current position
  • transport_set_song_pos(position) - Set position
  • transport_get_bpm() - Get tempo
  • transport_set_bpm(bpm) - Set tempo

Mixer Methods

  • mixer_get_track_volume(track_num) - Get volume
  • mixer_set_track_volume(track_num, volume) - Set volume
  • mixer_get_track_name(track_num) - Get name
  • mixer_set_track_name(track_num, name) - Set name
  • mixer_get_track_pan(track_num) - Get pan
  • mixer_set_track_pan(track_num, pan) - Set pan
  • mixer_mute_track(track_num, mute) - Mute/unmute
  • mixer_solo_track(track_num, solo) - Solo/unsolo

Channel Methods

  • channels_count() - Get channel count
  • channels_get_name(channel_num) - Get name
  • channels_set_volume(channel_num, volume) - Set volume
  • channels_mute(channel_num, mute) - Mute/unmute
  • channels_get_color(channel_num) - Get color
  • channels_set_color(channel_num, color) - Set color

Pattern Methods

  • patterns_count() - Get pattern count
  • patterns_get_name(pattern_num) - Get name
  • patterns_set_name(pattern_num, name) - Set name
  • patterns_get_length(pattern_num) - Get length
  • patterns_jump_to(pattern_num) - Jump to pattern

Playlist Methods

  • playlist_get_track_name(track_num) - Get track name
  • playlist_set_track_name(track_num, name) - Set track name

General Methods

  • general_get_version() - Get FL Studio version
  • general_get_project_title() - Get project title
  • general_save_project() - Save project
  • general_undo() - Undo last action

UI Methods

  • ui_show_window(window_id) - Show window
  • ui_get_visible(window_id) - Check visibility

Setup Requirements

  1. Install loopMIDI (Windows) and create ports:
  2. Flapi Request
  3. Flapi Response

  4. Install Flapi:

    pip install flapi
    flapi install
    

  5. Configure FL Studio MIDI settings for Flapi ports

  6. Restart FL Studio with Flapi script enabled

See Also