This work is focused on optimization problems within the predictive control framework for determining and engaging
the flexibility of a microgrid in grid-microgrid energy exchange. The microgrid has a controllable battery storage
and other components represented with a residual power flow. All major economic constituents of the grid-connected
microgrid operation are considered: day-ahead, intra-day, peak power and battery degradation costs, as well as
rewards and penalties for providing flexibility. The problems are posed as linear worst-case minimization programs
in which all flexibility activation scenarios are taken into account. An analysis is conducted for various combinations
of flexibility reservation and activation prices that can be bid to a grid entity. The technical and economic feasibility
of the flexibility provision is confirmed by the use of an online model predictive controller that optimally meets the
requirements of grid flexibility according to the declared reserve in the environment of online occurring disturbances
and events