The thing to note first is that people were not literally roasting their neighbors for medical purposes. Medical cannibalism could mean taking a tincture made mummified bodies, or doctors/healers might prescribe taking powdered skull for various ailments. Point being, the body parts consumed were usually take in similar forms as herbal remedies. Incidentally the mummy thing was largely thought of as a kind of panacea.
The basis of a lot of this is not unlike homeopathy in contemporary times: the belief that “like cures like.” So let’s say you have a head wound: someone might apply a poultice (thick paste) with ground skull powder to heal the bone. And, worth noting that the medieval belief in the four humors was still somewhat pervasive through the 15th century, and somewhat into the 16th, so a lot of medicine at the time was rooted in the idea of balance and harmony. Another connection to point out is how blood itself was understood as having the power to contain a person’s soul; this is why people would drink blood- they thought it would give them the strength of the deceased person.
The best way to explain this is that our contemporary understanding of the division between magic/ritual and “science” was not so clear in the early modern period. In fact, you had people at the court of Rudolf II in Prague for example, who were astronomers and also interested in alchemy, and actively carried out experiments to try to turn iron into gold. This was not a frivolous hobby, but was a courtly pursuit only discussed among some of the most well-educated elite at the time. To us alchemy may seem ridiculous now, but at the time it was thought of as a possibility that just hadn’t been discovered yet, and if a person could finally achieve such a feat, it would surely bring about wealth, etc.