This paper presents an optimal management strategy, called Building Optimizer, based on a Model Predictive Control (MPC) approach with self-learning capabilities for buildings. This research is framed in the development of an agent-based architecture to provide demand response services in an Energy Community to optimise the management of renewable energy sources and provide grid stability. The proposed MPC is a key enabler of cooperative demand response strategies at community level, ensuring the allocation of an optimal demand profile at each participating member of the community according to an optimal consumption reference defined by a complementary agent at community level. The MPC calculates the optimal setpoints of the HVAC system's terminal units, considering the expected usage of the buildings and the outdoor conditions, and exploiting the building's thermal inertia. The models embedded in the MPC are grey-box models representing a thermal zone of the building. To reduce measurement and model uncertainties, these models incorporate self-learning capabilities implemented as Moving Horizon Estimators that perform a continuous calibration based on real-time operational measurements. This solution allows full automation for model calibration and management of the terminal units. This paper presents a case study in which a baseline MPC with fixed model parameters obtained by an offline calibration is compared to the Building Optimizer with selflearning capabilities. The Building Optimizer is able to track a requested power consumption providing up to 20% of flexibility compared to the reference consumption without demand management and guaranteeing thermal comfort, at least 98% of the time. For this scenario, the Building Optimizer proves more reliable in guaranteeing the thermal comfort and a better match to the requested consumption compared to the baseline MPC. Demand-side management by the MPC can be translated into up to 15% energy shift from peak hours to valley hours.