I am not sure if it is correct to say that this episode "deals with mental health" the way the OP asks.
- The M5's behavior suggests that its implementation of human engrams was faulty, as it did not sufficiently balance the concepts of good and evil with the concept of self preservation.
- During Daystrom's "breakdown" he describes anger and deep frustration with the treatment his research has received and lack of personal credit, though I am not sure if the in-universe explanation links that frustration with M5's profound invulnerability.
In the TOS episode The Ultimate Computer the famous scientist Dr. Richard Daystrom (namesake of the Daystron Institude) has a mental breakdown after the M5, closely modeled after Daystrom's own mind, had committed what Daystrom felt to be murder by intentionally destroying a Starfleet ship as part of an exercise gone wrong. At the end, Daystrom needed to be taken to "a total rehabilitation facility".
Incredible performance by William Marshall (alternate source)
Daystrom: You are great, I am great.
Twenty years of groping to prove the things I’ve done before are not accidents.
Seminars and lectures to rows of fools who couldn’t begin to understand my systems.
“Colleagues.” Colleagues laughing behind my back at the boy wonder. Becoming famous building on my work. Building on my work!
Bones: Jim he’s on the edge of a nervous breakdown if not insanity.
[...]In the briefing room, the senior staff collaborates on a plan to gain back control by focusing on a certain relay unit between the M-5 and the bridge. McCoy goes to Daystrom to convince him to shut off the M-5. Daystrom, on the other hand, defends the M-5, saying it's learning, and further, that the advance the M-5 represents would liberate man from hazardous duties, saving life. McCoy notes later to Kirk that Daystrom reacts toward the computer as a father would to his child. Even if the child went anti-social and killed a person, a father would protect the child.
[...]Now that the M-5 has committed murder, Kirk confronts Daystrom, convincing him that the M-5 is doing more than originally designed. He demands that Daystrom attempt to reason with M-5, as Daystrom admits it is his engrams that he imprinted on the machine. However, he goes mad in the effort, realizing his reputation is at stake. In his delirium, he lashes out at Kirk, but is subdued by a Vulcan nerve pinch from Spock.
McCoy hauls him off to sickbay, and Spock notes the self-preservation that the M-5 is displaying is probably a consequence of Daystrom's engram imprinting.
[...]Dr. Daystrom, meanwhile, is cared for in sickbay under sedation and heavy restraint to await transfer to a total rehabilitation facility, under McCoy's recommendation. Kirk orders that Sulu plot a return course to Starbase 6.
Doctor Richard Daystrom was one of the most influential Human scientists of the 23rd century. Born in 2219, Daystrom was considered a genius in his day, and was compared to Albert Einstein, Kazanga, and Sitar of Vulcan. He was the inventor of the comptronic and duotronic computer systems.