A composite of hollow structure CoS2/nitrogen-doped carbon spheres derived from a Co-based metal-organic framework has been fabricated through a facile synthetic route and delivered superior rate capability and long-term cycling performance as an anode for lithium- and sodium-ion batteries.