| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- """
- -----------------------------------------------------------------------------------------
- INTERSEL - 4 cité d'Hauteville - 75010 PARIS
- RCS PARIS 488 379 660 - NAF 721Z
- File : core/context/context_manager.py
- Object : Context manager for the MCP server
- -----------------------------------------------------------------------------------------
- Modifications :
- - 2026/01/16 - Damien Lugagne-Delpon - V1.0.0- Creation
- -----------------------------------------------------------------------------------------
- @copyright : Intersel 2026
- @author : Damien Lugagne-Delpon - damien.lugagne-delpon@livinweb.fr
- @version : 1.0.0 (to be updated to the current_version of the developed software if file is touched)
- -----------------------------------------------------------------------------------------
- """
- from mcp_server.core.constants import ModxType, actionType
- from mcp_server.core.context.context_schema import DEFAULT_CONTEXT
- from mcp_server.config.logger import LOGGER as logger
- class ContextManager:
- """Context manager for the MCP server."""
-
- def __init__(self):
- """Initialize the context manager."""
- self.context = DEFAULT_CONTEXT.copy()
-
- def add_to_context(self, modx_type, items):
- """Add items of a given MODX type to the context."""
- if modx_type in self.context:
- for element_id, actions in items.items():
- filtered_actions = {}
- for action, value in actions.items():
- if action not in actionType.__members__.values():
- logger.warning(f"ContextManager - add_to_context - Unknown action type '{action}' for MODX type '{modx_type}'. Skipping this action.")
- else:
- filtered_actions[action] = value
- self.context[modx_type][element_id] = filtered_actions
- logger.debug(f"ContextManager - add_to_context - Items added to context.")
- else:
- logger.warning(f"ContextManager - add_to_context - MODX type '{modx_type}' not in context. Cannot add items to context.")
-
- def remove_from_context(self, modx_type, item_ids):
- """Remove items of a given MODX type from the context by their IDs."""
- if modx_type in self.context:
- for item_id in item_ids:
- if item_id in self.context[modx_type]:
- del self.context[modx_type][item_id]
- logger.debug(f"ContextManager - remove_from_context - Items removed from context.")
- else:
- logger.warning(f"ContextManager - remove_from_context - MODX type '{modx_type}' not in context. Cannot remove items from context.")
- def clear_context(self):
- """Clear the entire context."""
- self.context = DEFAULT_CONTEXT.copy()
- logger.debug("ContextManager - clear_context - Context cleared.")
- def get_context(self):
- """Obtenir le contexte complet."""
- return self.context
|