Context. The complexes HMgNC, MgNC, and MgCN were previously detected in the outer shells of the star IRC+10216 and were thought to be formed through radiative association of Mg+ and cyanopolyynes followed by dissociative recombination. The presence of these species in the outer shells of IRC+10216 consequently provoked a question regarding whether HMgNC could be an astronomical reservoir of MgNC and MgCN. Aims. The aim of this study is to investigate the evolution of the lowest singlet and triplet excited states for HMgNC and HMgCN along the MgH, MgC, and MgN stretching coordinates. Additionally, the vertical excitation energies, photoabsorption cross section, and oscillator strengths of the lowest electronic singlet states are calculated. Methods. Coupled cluster quantum chemistry methods were used to calculate the equilibrium electronic structure. Multi-reference methods were then used to characterize the photochemical pathways of the excited electronic states and simulate the electronic absorption spectrum. Results. HMgNC and HMgCN are thermodynamically stable relative to the first dissociation limit and photostable in the near-ultraviolet–visible region (λ > 220 nm). Both isomers strongly absorb in the 170 nm < λ < 200 nm range and may produce MgNC and MgCN in their first excited state (A2Π). This process competes with the photoproduction of MgH and CN. The photodissociation to produce MgNC and MgCN in their ground states is not a plausible mechanism.