What is the connection between "nut" and the character? How was the phrase "are you nuts?" used at first?
Etymonline has this to offer:
So, in a similar since of being out of one's mind or being out of your head, nuts seems to have evolved past into its own idiom. This is further suggested by the common phrase, "out of one's gourd" which has the same meaning.
Etymology Online contends that nuts was influenced by the metaphoric application of nut to refer to one's head. To be off one's nut dates from 1861 as an expression for "to be insane". Similarly, one could say "to be out of mind" or "to be out of one's head". In British English, a crazy person is a nutter (possibly antiquated).
Also of note: nuts to mean "crazy" predates the usage of the same to mean testicles (1846 and 1915, respectively).
Dictionary Notice of 'Nuts'
The Random House Dictionary of American Slang (1997) distinguishes between nuts in the sense of infatuated, fascinated, or obsessed (which it dates to 1785) and nuts in the sense of insane (which it dates to 1908). As RHDAS notes, nuts appears in Francis Grose, A Classical Dictionary of the Vulgar Tongue, though the precise definition given for it in that dictionary is "very agreeable."
Notwithstanding the early date offered by RHDAS, the "fond, pleased" meaning of the term first appears in a much later edition of Grose—namely, Grose & "A Member of the Whip Club," Lexicon Balatronicum: A Dictionary of Buckish Slang, University Wit, and Pickpocket Eloquence (1811):
A related phrase, nuts upon it, meaning "very much pleased or gratified" appears in yet another version of Grose's dictionary, Grose & Egan, Grose's Classical Dictionary of the Vulgar Tongue, revised and corrected (1823):
RHDAS's first clearcut historical examples of nuts in the sense of insane are from 1908:
But RHDAS points out that a different nut-based phrase—"off [or out of] one's nut"—with the meaning "out of one's mind" goes back to 1860. Here is the instance that RHDAS cites from John Hotten, A Dictionary of Modern Slang, Cant, And, Vulgar Words, second edition (1860):
The 1874 edition of Hotten's Slang Dictionary makes the connection to actual or metaphorical insanity clearer:
'Nuts' in the Wild
The earliest match in a Google Books search for the phrase "are you nuts" appears in R. L. Gentles, "A Plea for the Players," in Hedderwick's Miscellany of Instructive and Entertaining Literature (November 22, 1862):
But this evidently uses nuts in the sense of extremely fond. The first unmistakable instance of "are you nuts" in the sense of "are you insane" that the search results yield is from Arthur Allen, "Black Water Dave," in Boys Life (March 1916):
An instance of "nuts on," in the sense of extremely desirous of, appears in the following stanza of light verse from the September 22, 1894 issue of Punch:
The first unmistakable occurrences of nuts in the sense of insane appear in the first decade of the twentieth century. But allied senses of nuts involving infatuation or being out of one's normal mind go back at least to the mid-19th century and perhaps (in the former case) to the late 18th century. I suspect that the intertwining meanings of nuts strongly influenced each other, which makes pinning down the exact date of emergence of the "insane" sense of the word quite difficult.
Alice Nutter was one of the 'witches' tried at the Pendle Witch Trials of 1612 in Pendle, Lancashire, England.
The word 'nutter' could be from this as she was seen to be 'crazy', therefore "are you nuts?"/"nut-case"/"off one's nut" could have evolved from this.
Just a hypothesis. The basis might be Latin de-mens/dementis meaning off one's mind. In the course of time demens might have been shortened via dements to ments and mets, and the unfamiliar form mets may have been transformed into some familiar word: nuts. Obviously the meaning crazy and nuts from a walnut tree have no logical relationship. But when unfamiliar wordforms are transformed into something familiar this happens without consideration of logic.