context_manager.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. """
  2. -----------------------------------------------------------------------------------------
  3. INTERSEL - 4 cité d'Hauteville - 75010 PARIS
  4. RCS PARIS 488 379 660 - NAF 721Z
  5. File : core/context/context_manager.py
  6. Object : Context manager for the MCP server
  7. -----------------------------------------------------------------------------------------
  8. Modifications :
  9. - 2026/01/16 - Damien Lugagne-Delpon - V1.0.0- Creation
  10. -----------------------------------------------------------------------------------------
  11. @copyright : Intersel 2026
  12. @author : Damien Lugagne-Delpon - damien.lugagne-delpon@livinweb.fr
  13. @version : 1.0.0 (to be updated to the current_version of the developed software if file is touched)
  14. -----------------------------------------------------------------------------------------
  15. """
  16. from mcp_server.core.constants import ModxType, actionType
  17. from mcp_server.core.context.context_schema import DEFAULT_CONTEXT
  18. from mcp_server.config.logger import LOGGER as logger
  19. class ContextManager:
  20. """Context manager for the MCP server."""
  21. def __init__(self):
  22. """Initialize the context manager."""
  23. self.context = DEFAULT_CONTEXT.copy()
  24. def add_to_context(self, modx_type, items):
  25. """Add items of a given MODX type to the context."""
  26. if modx_type in self.context:
  27. for element_id, actions in items.items():
  28. filtered_actions = {}
  29. for action, value in actions.items():
  30. if action not in actionType.__members__.values():
  31. logger.warning(f"ContextManager - add_to_context - Unknown action type '{action}' for MODX type '{modx_type}'. Skipping this action.")
  32. else:
  33. filtered_actions[action] = value
  34. self.context[modx_type][element_id] = filtered_actions
  35. logger.debug(f"ContextManager - add_to_context - Items added to context.")
  36. else:
  37. logger.warning(f"ContextManager - add_to_context - MODX type '{modx_type}' not in context. Cannot add items to context.")
  38. def remove_from_context(self, modx_type, item_ids):
  39. """Remove items of a given MODX type from the context by their IDs."""
  40. if modx_type in self.context:
  41. for item_id in item_ids:
  42. if item_id in self.context[modx_type]:
  43. del self.context[modx_type][item_id]
  44. logger.debug(f"ContextManager - remove_from_context - Items removed from context.")
  45. else:
  46. logger.warning(f"ContextManager - remove_from_context - MODX type '{modx_type}' not in context. Cannot remove items from context.")
  47. def clear_context(self):
  48. """Clear the entire context."""
  49. self.context = DEFAULT_CONTEXT.copy()
  50. logger.debug("ContextManager - clear_context - Context cleared.")
  51. def get_context(self):
  52. """Obtenir le contexte complet."""
  53. return self.context