Real-space approach to the calculation of magnetocrystalline anisotropy in metals