#!/usr/bin/env python3
"""Development script for the Image Gen MCP Server."""
import argparse
import os
import subprocess
import sys
from pathlib import Path
def setup_env():
"""Set up development environment."""
print("๐ง Setting up development environment...")
# Create .env file if it doesn't exist
env_file = Path(".env")
if not env_file.exists():
print("๐ Creating .env file from example...")
example_file = Path(".env.example")
if example_file.exists():
env_file.write_text(example_file.read_text())
print("โ
Created .env file. Please edit it with your OpenAI API key.")
else:
env_file.write_text("PROVIDERS__OPENAI__API_KEY=your-api-key-here\n")
print("โ
Created basic .env file. Please add your OpenAI API key.")
# Create storage directories
storage_path = Path("storage")
storage_path.mkdir(exist_ok=True)
(storage_path / "images").mkdir(exist_ok=True)
(storage_path / "cache").mkdir(exist_ok=True)
(storage_path / "logs").mkdir(exist_ok=True)
print("โ
Created storage directories")
def run_tests(extra_args=None):
"""Run the test suite."""
print("๐งช Running tests...")
if extra_args is None:
extra_args = []
try:
# Add progress indicator by default and show output in real-time
cmd = ["uv", "run", "pytest", "tests/", "-v", "--tb=short"] + extra_args
result = subprocess.run(cmd, check=False)
return result.returncode == 0
except FileNotFoundError:
print("โ pytest not found. Install with: uv add --dev pytest")
return False
def run_server():
"""Run the MCP server in development mode."""
print("๐ Starting Image Gen MCP Server...")
# Check for OpenAI API key
if not os.getenv("PROVIDERS__OPENAI__API_KEY"):
print("โ PROVIDERS__OPENAI__API_KEY environment variable not set")
print(" Please set it in your .env file or environment")
return False
try:
subprocess.run(
["uv", "run", "python", "-m", "image_gen_mcp.server"],
check=True,
)
return True
except KeyboardInterrupt:
print("\n๐ Server stopped")
return True
except subprocess.CalledProcessError as e:
print(f"โ Server failed to start: {e}")
return False
def run_example():
"""Run the basic usage example."""
print("๐ Running basic usage example...")
if not os.getenv("PROVIDERS__OPENAI__API_KEY"):
print("โ PROVIDERS__OPENAI__API_KEY environment variable not set")
print(" Please set it in your .env file or environment")
return False
try:
subprocess.run(["uv", "run", "python", "examples/basic_usage.py"], check=True)
return True
except subprocess.CalledProcessError as e:
print(f"โ Example failed: {e}")
return False
def lint_code():
"""Run code linting."""
print("๐ Running code linting...")
# Run ruff
try:
result = subprocess.run(
["uv", "run", "ruff", "check", "image_gen_mcp/"],
check=False,
capture_output=True,
text=True,
)
if result.stdout:
print(result.stdout)
if result.stderr:
print(result.stderr)
if result.returncode == 0:
print("โ
No linting issues found")
else:
print(f"โ ๏ธ Found {result.returncode} linting issues")
return result.returncode == 0
except FileNotFoundError:
print("โ ruff not found. Install with: uv add --dev ruff")
return False
def format_code():
"""Format code with black."""
print("๐จ Formatting code...")
try:
subprocess.run(
["uv", "run", "black", "image_gen_mcp/", "tests/", "examples/"], check=True
)
print("โ
Code formatted successfully")
return True
except FileNotFoundError:
print("โ black not found. Install with: uv add --dev black")
return False
except subprocess.CalledProcessError as e:
print(f"โ Formatting failed: {e}")
return False
def check_dependencies():
"""Check if all dependencies are installed."""
print("๐ฆ Checking dependencies...")
try:
result = subprocess.run(
["uv", "pip", "check"], check=False, capture_output=True, text=True
)
if result.returncode == 0:
print("โ
All dependencies are satisfied")
else:
print("โ Dependency issues found:")
print(result.stdout)
print(result.stderr)
return result.returncode == 0
except FileNotFoundError:
print("โ uv not found. Please install uv first")
return False
def main():
"""Main development script."""
parser = argparse.ArgumentParser(
description="Image Gen MCP Server Development Tools"
)
parser.add_argument(
"command",
choices=["setup", "test", "server", "example", "lint", "format", "check"],
help="Command to run",
)
parser.add_argument(
"extra_args",
nargs=argparse.REMAINDER,
help="Extra arguments to pass to the underlying tool",
)
args = parser.parse_args()
if args.command == "setup":
success = True
success &= check_dependencies()
setup_env()
elif args.command == "test":
success = run_tests(args.extra_args)
elif args.command == "server":
success = run_server()
elif args.command == "example":
success = run_example()
elif args.command == "lint":
success = lint_code()
elif args.command == "format":
success = format_code()
elif args.command == "check":
success = check_dependencies()
sys.exit(0 if success else 1)
if __name__ == "__main__":
main()