As is often the case with these sorts of things, we have an early (in this case, late eighteenth-century) example of the rhyme, which includes little by way of context or elaboration. The popularity of the rhyme, which subsequently appeared in various versions, then attracted speculation and folk explanations as to the original meaning. Some of these then have been passed off as "the real truth behind...", something that cannot be verified but is often taken to be more concrete than the very speculation it, in fact, is.
It is possible (but let's concede that this, too, is speculation) that the rhyme was originally meant as a riddle, the rhyme would be presented followed by the question, "what am I?" The answer, according to this scenario would "an egg," an answer that became so well known that the riddle could no longer function in that capacity: there is no sense to a riddle when everyone knows the answer.
According to this explanation, "Humpty Dumpty" subsequently became a nonsensical rhyme of popular culture and was often grouped together with other "nursery rhymes," ditties relegated to children as the appropriate audience.
According to this explanation, "egg" was the intended association from the very start. If this explanation is wrong, then I think we need to understand that in popular culture, there was an early assumption that the rhyme referred to an egg, although we must concede that some early depictions were of a boy or a man on the wall. The popular Broadway play of the same name by George L. Fox (1825–1877) running from 1868 to 1869, depicts Humpty Dumpty as a man with a bald head, but it is generally assumed that the audience would think of the character as an anthropomorphize egg.
It appears that the first well-recognized illustration of Humpty Dumpty as an actual egg speared as a line drawing in Lewis Carroll’s novel, Through the Looking-Glass, first published in late 1871. An illustration for this book depict the character clearly as an egg. From that point, that was usually the way the character was depicted.