A three-dimensional potential energy surface is developed to describe the structure and dynamical behavior of the Mg +-H2 and Mg +-D 2 complexes. Ab initio points calculated using the RCCSD(T) method and aug-cc-pVQZ basis set (augmented by bond functions) are fitted using a reproducing kernel Hilbert space method [Ho and Rabitz, J. Chem. Phys. 104, 2584 (1996)] to generate an analytical representation of the potential energy surface. The calculations confirm that Mg +-H2 and Mg +-D2 essentially consist of a Mg + atomic cation attached, respectively, to a moderately perturbed H2 or D2 molecule in a T-shaped configuration with an intermolecular separation of 2.62 and a well depth of De = 842 cm -1. The barrier for internal rotation through the linear configuration is 689 cm -1. Interaction with the Mg + ion is predicted to increase the H2 molecules bond-length by 0.008 . Variational rovibrational energy level calculations using the new potential energy surface predict a dissociation energy of 614 cm -1 for Mg +-H2 and 716 cm -1 for Mg +-D2. The H-H and D-D stretch band centers are predicted to occur at 4059.4 and 2929.2 cm -1, respectively, overestimating measured values by 3.9 and 2.6 cm -1. For Mg +-H2 and Mg +-D2, the experimental B and C rotational constants exceed the calculated values by ∼1.3, suggesting that the calculated potential energy surface slightly overestimates the intermolecular separation. An ab initio dipole moment function is used to simulate the infrared spectra of both complexes.