#!/usr/bin/env python3
"""
MIRA MCP Launcher - Wrapper to properly launch the MCP server
Ensures all paths and environment are correctly configured
"""

import sys
import os
from pathlib import Path

# Set up MIRA root paths
mira_root = Path(__file__).parent.parent
src_path = mira_root / "src"

# Configure Python path before any imports
sys.path.insert(0, str(mira_root))
sys.path.insert(0, str(src_path))
sys.path.insert(0, str(mira_root / "mira_mcp"))

# Set PYTHONPATH environment variable
os.environ['PYTHONPATH'] = f"{src_path}:{mira_root}:{os.environ.get('PYTHONPATH', '')}"

# Now we can import and run the MCP server
try:
    # Try to import the full MCP server
    from mcp_stdio_server import main
    import asyncio
    
    if __name__ == "__main__":
        # Run the full MCP server
        asyncio.run(main())
        
except ImportError as e:
    # If imports fail, fall back to simple server
    print(f"Import error in full MCP server: {e}", file=sys.stderr)
    print("Falling back to simple MCP server...", file=sys.stderr)
    
    try:
        from mcp_simple_server import main
        import asyncio
        
        if __name__ == "__main__":
            # Run the simple MCP server
            asyncio.run(main())
            
    except Exception as fallback_error:
        print(f"Fatal error: Could not start any MCP server: {fallback_error}", file=sys.stderr)
        sys.exit(1)