The engineered spin structures recently built and measured in scanning tunneling microscope experiments are calculated using density functional theory. By determining the precise local structure around the surface impurities, we find the Mn atoms can form molecular structures with the binding surface, behaving like surface molecular magnets. The spin structures are confirmed to be antiferromagnetic, and the exchange couplings are calculated within 8% of the experimental values simply by collinear-spin GGA+U calculations. We can also explain why the exchange couplings significantly change with different impurity binding sites from the determined local structure. The bond polarity is studied by calculating the atomic charges with and without the Mn adatoms.