An inversion method is presented to discover the stratification of the vector magnetic fields in the upper solar atmosphere by Stokes profile analysis. It opens a way, independent of the line formation depth theory, for the location of the inferred parameters to be automatically executed. The method is established on the basis, specified in detail in the text, that the transfer problem can be numerically solved. It consists of four operations with different actions on the Stokes profiles alternately carried out downwards from the surface. The main assumption made is that the height gradients of all parameters influencing the Stokes profiles are not so large that they cause rapid shifts with depth of the positions of peaks or valleys in the profiles. By the inversion of synthetically simulated profiles of Mg i b 5172.7A, the feasibility of the method is shown in that the present method can recover the quasi-static structure of the vector magnetic fields, especially of the field strengths, from the surface to the lower layers in the chromosphere, if the spectral resolution and purity are good enough.