82 lines
2.4 KiB
Python
82 lines
2.4 KiB
Python
# GPTEdit - OpenAI Image Editing MCP Server
|
|
|
|
import asyncio
|
|
import logging
|
|
import sys
|
|
import json
|
|
from pathlib import Path
|
|
|
|
# Add project root to path
|
|
sys.path.insert(0, str(Path(__file__).parent))
|
|
|
|
from src.connector.config import Config
|
|
from src.server.mcp_server import GPTEditMCPServer
|
|
|
|
# Configure logging to stderr for debugging
|
|
logging.basicConfig(
|
|
level=logging.INFO,
|
|
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
|
handlers=[
|
|
logging.FileHandler('gptedit.log', encoding='utf-8'),
|
|
logging.StreamHandler(sys.stderr) # Changed to stderr to avoid interfering with stdout
|
|
]
|
|
)
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
async def main():
|
|
"""Main entry point for GPTEdit MCP Server"""
|
|
try:
|
|
# Log to stderr to avoid interfering with JSON-RPC communication
|
|
logger.info("=" * 60)
|
|
logger.info("Starting GPTEdit MCP Server")
|
|
logger.info("=" * 60)
|
|
|
|
# Load configuration
|
|
config = Config()
|
|
if not config.validate():
|
|
logger.error("Configuration validation failed")
|
|
return 1
|
|
|
|
# Create and start server
|
|
mcp_server = GPTEditMCPServer(config)
|
|
server = mcp_server.get_server()
|
|
|
|
# Log server info
|
|
logger.info("GPTEdit MCP Server is running...")
|
|
logger.info("Ready to process image editing requests")
|
|
logger.info(f"Available tools: edit_image, edit_with_mask, batch_edit, validate_image, create_mask_from_alpha")
|
|
|
|
# Use stdio transport with proper stream handling
|
|
from mcp.server.stdio import stdio_server
|
|
|
|
async with stdio_server() as (read_stream, write_stream):
|
|
try:
|
|
await server.run(
|
|
read_stream,
|
|
write_stream,
|
|
server.create_initialization_options()
|
|
)
|
|
except Exception as e:
|
|
logger.error(f"Server error: {e}", exc_info=True)
|
|
raise
|
|
|
|
except KeyboardInterrupt:
|
|
logger.info("Server shutdown requested")
|
|
return 0
|
|
except Exception as e:
|
|
logger.error(f"Fatal error: {e}", exc_info=True)
|
|
return 1
|
|
|
|
|
|
if __name__ == "__main__":
|
|
# Ensure clean exit
|
|
try:
|
|
exit_code = asyncio.run(main())
|
|
except Exception as e:
|
|
logger.error(f"Unhandled exception: {e}", exc_info=True)
|
|
exit_code = 1
|
|
|
|
sys.exit(exit_code)
|