Files
imagen4/run_mcp_safe.py

67 lines
1.8 KiB
Python
Raw Permalink Normal View History

2025-08-26 02:40:23 +09:00
#!/usr/bin/env python3
"""
MCP Protocol Compatible UTF-8 Runner for imagen4
This script ensures proper UTF-8 handling without interfering with MCP protocol
"""
import os
import sys
import subprocess
import locale
def setup_utf8_environment():
"""Setup UTF-8 environment variables without stdout interference"""
# Set UTF-8 environment variables
env = os.environ.copy()
env['PYTHONIOENCODING'] = 'utf-8'
env['PYTHONUTF8'] = '1'
env['LC_ALL'] = 'C.UTF-8'
# Windows-specific setup
if sys.platform.startswith('win'):
# Set console code page to UTF-8 (suppress output)
try:
os.system('chcp 65001 >nul 2>&1')
except Exception:
pass
# Set locale
try:
locale.setlocale(locale.LC_ALL, 'C.UTF-8')
except locale.Error:
try:
locale.setlocale(locale.LC_ALL, '')
except locale.Error:
pass
return env
def main():
"""Main function - run imagen4 MCP server with proper UTF-8 setup"""
# Setup UTF-8 environment
env = setup_utf8_environment()
# Get the directory of this script
script_dir = os.path.dirname(os.path.abspath(__file__))
main_py = os.path.join(script_dir, 'main.py')
# Run main.py with proper environment
try:
# Use subprocess to run with clean environment
result = subprocess.run([
sys.executable, main_py
], env=env, cwd=script_dir)
return result.returncode
except KeyboardInterrupt:
print("Server stopped by user", file=sys.stderr)
return 0
except Exception as e:
print(f"Error running server: {e}", file=sys.stderr)
return 1
if __name__ == "__main__":
sys.exit(main())