main.py
 1  #!/usr/bin/env python3
 2  """
 3  Cinema 4D MCP Server - Main entry point script
 4  
 5  This script starts the Cinema 4D MCP server either directly or through
 6  package imports, allowing it to be run both as a script and as a module.
 7  """
 8  
 9  import sys
10  import os
11  import socket
12  import logging
13  import traceback
14  
15  # Configure logging to stderr
16  logging.basicConfig(
17      level=logging.DEBUG,
18      format="%(asctime)s [%(levelname)s] %(message)s",
19      handlers=[logging.StreamHandler(sys.stderr)],
20  )
21  
22  logger = logging.getLogger("cinema4d-mcp")
23  
24  # Add the src directory to the Python path
25  src_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "src")
26  if os.path.exists(src_path):
27      sys.path.insert(0, src_path)
28  
29  # Add the project root to Python path
30  sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
31  
32  
33  def log_to_stderr(message):
34      """Log a message to stderr for Claude Desktop to capture."""
35      print(message, file=sys.stderr, flush=True)
36  
37  
38  def main():
39      """Main entry point function."""
40      log_to_stderr("========== CINEMA 4D MCP SERVER STARTING ==========")
41      log_to_stderr(f"Python version: {sys.version}")
42      log_to_stderr(f"Current directory: {os.getcwd()}")
43      log_to_stderr(f"Python path: {sys.path}")
44  
45      # Check if Cinema 4D socket is available
46      c4d_host = os.environ.get("C4D_HOST", "127.0.0.1")
47      c4d_port = int(os.environ.get("C4D_PORT", 5555))
48  
49      log_to_stderr(f"Checking connection to Cinema 4D on {c4d_host}:{c4d_port}")
50      try:
51          test_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
52          test_socket.settimeout(5)  # Set 5 second timeout
53          test_socket.connect((c4d_host, c4d_port))
54          test_socket.close()
55          log_to_stderr("✅ Successfully connected to Cinema 4D socket!")
56      except Exception as e:
57          log_to_stderr(f"❌ Could not connect to Cinema 4D socket: {e}")
58          log_to_stderr(
59              "   The server will still start, but Cinema 4D integration won't work!"
60          )
61  
62      try:
63          log_to_stderr("Importing cinema4d_mcp...")
64          from cinema4d_mcp import main as package_main
65  
66          log_to_stderr("🚀 Starting Cinema 4D MCP Server...")
67          package_main()
68      except Exception as e:
69          log_to_stderr(f"❌ Error starting server: {e}")
70          log_to_stderr(traceback.format_exc())
71          sys.exit(1)
72  
73  
74  if __name__ == "__main__":
75      main()