#!/usr/bin/env python3
"""Launcher for the AbletonMCP-AI FastMCP server."""
import builtins
import os
import sys
from pathlib import Path

WRAPPER_DIR = Path(__file__).resolve().parent
MCP_SERVER_DIR = WRAPPER_DIR / "AbletonMCP_AI" / "mcp_server"

# Add the mcp_server directory to path so "from server import mcp" works
if str(MCP_SERVER_DIR) not in sys.path:
    sys.path.insert(0, str(MCP_SERVER_DIR))
if str(WRAPPER_DIR / "AbletonMCP_AI") not in sys.path:
    sys.path.insert(0, str(WRAPPER_DIR / "AbletonMCP_AI"))

os.environ["PYTHONUNBUFFERED"] = "1"
os.environ["PYTHONIOENCODING"] = "utf-8"

# Protect the MCP stdio channel from accidental prints inside engines/modules.
_original_print = builtins.print


def _stderr_print(*args, **kwargs):
    if "file" not in kwargs or kwargs["file"] is None:
        kwargs["file"] = sys.stderr
    if "flush" not in kwargs:
        kwargs["flush"] = True
    return _original_print(*args, **kwargs)


builtins.print = _stderr_print

# Import mcp instance from server.py
from server import mcp

mcp.run(transport="stdio")
