An optimal control for a dynamical system optimizes a certain objective function. Here, we consider the construction of an optimal control for a stochastic dynamical system with a random structure, Poisson perturbations and random jumps, which makes the system stable in probability. Sufficient conditions of the stability in probability are obtained, using the second Lyapunov method, in which the construction of the corresponding functions plays an important role. Here, we provide a solution to the problem of optimal stabilization in a general case. For a linear system with a quadratic quality function, we give a method of synthesis of optimal control based on the solution of Riccati equations. Finally, in an autonomous case, a system of differential equations was constructed to obtain unknown matrices that are used for the construction of an optimal control. The method using a small parameter is justified for the algorithmic search of an optimal control. This approach brings a novel solution to the problem of optimal stabilization for a stochastic dynamical system with a random structure, Markov switches and Poisson perturbations.