What is the connection between "nut" and the character? How was the phrase "are you nuts?" used at first?
Etymonline has this to offer:
"crazy," 1846, from earlier be nutts upon "be very fond of" (1785), which is possibly from nuts (n., pl.) "any source of pleasure" (1610s), from nut (q.v.). Sense influenced probably by metaphoric application of nut to "head" (1846, e.g. to be off one's nut "be insane," 1860).
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).
Alice Nutter was one of the 'witches' tried at the Pendle Witch Trials of 1612 in Pendle, Lancashire, England.
The trials of the Pendle witches in 1612 are among the most famous witch trials in English history, and some of the best recorded of the 17th century. The twelve accused lived in the area around Pendle Hill in Lancashire, and were charged with the murders of ten people by the use of witchcraft. (...) Alice Nutter was unusual among the accused in being comparatively wealthy, the widow of a tenant yeoman farmer. She made no statement either before or during her trial, except to enter her plea of not guilty to the charge of murdering Henry Mitton by witchcraft.
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.
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):
NUTS. Fond ; pleased. She's nuts upon her cull ; she's pleased with her cully. The cove's nutting th blowen ; the man is trying to please the girl.
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):
NUTS UPON IT. To be very much pleased or gratified with any object, adventure, or overture ; so a person who conceives a strong inclination for another of the opposite sex, is said to be quite nutty, or nuts upon him or her.
RHDAS's first clearcut historical examples of nuts in the sense of insane are from 1908:
1908 in H.C. Fisher A. Mutt 23: What struck him? He must be nuts. 1908 Hopper & Bechdolt 9009 85: What's got into me?...am I going nuts? 1908 W.G. Davenport Butte & Montana 209: An experience that almost drove him nuts.
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):
NUT, to be " off one's nut," to be in liquor, or "all mops and brooms."
The 1874 edition of Hotten's Slang Dictionary makes the connection to actual or metaphorical insanity clearer:
Nut, the head, in pugilistic slang. Used as an exclamation at a fight, it means to strike on the head. In tossing it is a direction to hide the head ; to be " off one's NUT," to be crazed or idiotic.
'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):
'How do you know all this?' asks Light Hair.
'Well, in general conversation in this room. But hang your impertinence, Harvey; what do you mean by cross- questioning me in that manner? Are you nuts on the girl?
'I don't wish to cross-question you; hut doesn't it strike you, Barclay, in the light of mean and cowardly, to retail tap-room scandal in the manner you have done. It does me. I think you are libelling the girl grossly; and you are all the more to blame, because she is not present to defend herself. I happen to know her intimately, and can testify that what you have said is foul scandal. She is as pure and respectable as her best friends could wish her to be.'
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):
There was a note of defiance in his voice that gave Mose a start. "What's this, you poor ninny, are you nuts? What difference does it make where you kill 'em so long as you get 'em. You're crazy, boy; you're stark mad."
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:
Thus war, for instance. Fancy shuts
Both eyes and vainly labours
To grssp the news that he is nuts
On blowing up his neighbours.
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.
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.