# Top level script for launching the MCP testing server from an installed
# copy of Wing.
#
# Copyright (c) 2000-2025, Archaeopteryx Software, Inc.  All rights reserved.

import sys
import os

import _winghome

# Set __file__ if not already set
try:
  __file__
except NameError:
  __file__ = sys.argv[0]

#----------------------------------------------------------------------
def NP_LoadModuleHere(modname):
  """Load the given module from same directory as this file
  without adding it to sys.modules"""

  sys.modules.pop(modname, None)

  sys.path.insert(0, os.path.dirname(__file__))
  mod = __import__(modname)
  sys.modules.pop(modname, None)

  sys.path = sys.path[1:]
  return mod

bootstrap_utils = NP_LoadModuleHere('bootstrap_utils')

# MCP server doesn't need updates
_winghome.kLoadUpdates = False

#-----------------------------------------------------------------------
def NP_Initialize():
  """Set up the Wing home module and meta importer for the MCP server."""

  # Find winghome based on location of this file (parent of bootstrap/)
  winghome = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  winghome, user_settings = bootstrap_utils.NP_SetupWingHomeModule(winghome, print_errors=True)

  # Load the meta importer with 'dbg' domain so imports resolve to
  # bin/dbg/src/ where the .wingcode files live
  bootstrap_utils.NP_CreateMetaImporter(winghome, user_settings,
                                        'dbg', logger=sys.stderr)

#-----------------------------------------------------------------------
if __name__ == '__main__':

  # Delete sys.path[0] because it refers to this file's directory
  del sys.path[0]

  # Initialize update manager; everything after this line can be updated
  NP_Initialize()

  # Start the MCP testing server
  from testing.aitool.mcpserver import CMCPTestingServer
  server = CMCPTestingServer()
  server.run()
