"""
MCP Server Setup and Global State Management
===========================================
Manages the MCP server instance and global shell manager state.
"""
import os
from typing import Optional
from mcp.server.fastmcp import FastMCP
from .shell_manager import OdooShellManager
# Create MCP server
mcp = FastMCP("Odoo Shell Server")
# Global shell manager
shell_manager: Optional[OdooShellManager] = None
def get_shell_manager() -> OdooShellManager:
"""
Get or create the shell manager.
Implements a singleton pattern to ensure only one shell manager
instance exists. Creates a new manager with configuration from
environment variables if one doesn't exist.
:return: The global shell manager instance
:rtype: OdooShellManager
"""
global shell_manager
if shell_manager is None:
# Get configuration from environment variables
odoo_bin = os.getenv('ODOO_BIN_PATH', '/usr/bin/odoo-bin')
addons_path = os.getenv('ODOO_ADDONS_PATH', '/odoo/addons')
db_name = os.getenv('ODOO_DATABASE', 'odoo')
config_file = os.getenv('ODOO_CONFIG_FILE')
shell_manager = OdooShellManager(odoo_bin, addons_path, db_name, config_file)
return shell_manager
def reset_shell_manager() -> str:
"""
Reset the shell manager (restart the shell process).
Terminates the current shell process and clears the global shell manager,
which will cause a new shell to be started on the next access.
:return: Success message or error description
:rtype: str
"""
global shell_manager
try:
if shell_manager:
shell_manager.stop()
shell_manager = None
return "Odoo shell session reset successfully"
except Exception as e:
return f"Error resetting shell: {str(e)}"