Effective energy management within microgrids is crucial, especially given system uncertainties. This study presents a novel Energy Management System (EMS) designed for microgrids with diverse energy sources, notably hydrogen and fuel cells. The EMS integrates artificial intelligence algorithms to predict and adapt to rapid changes, enhancing energy resource efficiency and minimizing wastage. Responsive in nature, the EMS enhances efficiency, reliability, and cost reduction in microgrid energy, dynamically responding to varying conditions for improved stability and environmental protection. Employing a random pattern approach, the EMS focuses on operational efficiency and reliability in supplying electrical and thermal loads. Addressing uncertainties in the electricity market and renewable production, a stochastic framework with scenario generation enables operation in grid-connected and island modes. Enhanced optimization through an improved particle swarm algorithm ensures cost-effectiveness and system reliability in dynamic environmental conditions. This research pioneers innovative microgrid energy management, emphasizing hydrogen and fuel cell technologies to elevate efficiency and performance reliability.