Files

91 lines
2.8 KiB
Python
Raw Permalink Normal View History

2025-08-26 01:31:42 +09:00
# GPT-Edit - OpenAI Image Editing MCP Server
2025-08-25 00:39:39 +09:00
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=[
2025-08-26 01:31:42 +09:00
logging.FileHandler('gpt-edit.log', encoding='utf-8'),
2025-08-25 00:39:39 +09:00
logging.StreamHandler(sys.stderr) # Changed to stderr to avoid interfering with stdout
]
)
logger = logging.getLogger(__name__)
async def main():
2025-08-26 01:31:42 +09:00
"""Main entry point for GPT-Edit MCP Server"""
2025-08-25 00:39:39 +09:00
try:
# Log to stderr to avoid interfering with JSON-RPC communication
logger.info("=" * 60)
2025-08-26 01:31:42 +09:00
logger.info("Starting GPT-Edit MCP Server")
2025-08-25 00:39:39 +09:00
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
2025-08-26 01:31:42 +09:00
logger.info("GPT-Edit MCP Server is running...")
2025-08-25 00:39:39 +09:00
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
2025-08-26 04:32:21 +09:00
from mcp.server.models import InitializationOptions
from mcp.server import NotificationOptions
2025-08-25 00:39:39 +09:00
async with stdio_server() as (read_stream, write_stream):
try:
await server.run(
read_stream,
write_stream,
2025-08-26 04:32:21 +09:00
InitializationOptions(
server_name="gpt-edit",
server_version="1.0.0",
capabilities=server.get_capabilities(
notification_options=NotificationOptions(),
experimental_capabilities={}
)
)
2025-08-25 00:39:39 +09:00
)
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)