Environmental pollution has attracted much attention in recent years. Many protocols have concluded between the major countries emitting these gases to reduce its consequences. Hence, governments and countries have considered various strategies to reduce it. One of the most important of these is the use of clean energies in the format of small networks called microgrids and nanogrids. So, this paper presents the optimal energy planning and management of the proposed test system, including microgrid, nanogrid, and the main grid. The whole grids are connected by transmission lines, enabling them to work in connected or islanding mode. Diverse scenarios are considered to investigate and cover all aspects of this. Thus, at first, the operation of each network is studied separately in the islanding mode, then the behavior of the proposed system in the connected mode is discussed. Moreover, uncertain parameters are involved in the programming to perform stochastic planning. The economic-ecological analysis is assumed as the objective function. The problem is modeled as MILP and solved simultaneously by augmented epsilon constrain (AEC) and LP-metric methods in GAMS software. The results show the effectiveness of the proposed planning to coordinate the function of the micro–nanogrid combination scheme.