Mechamaru didn't attend the same school as Jujutsu Kaisen’s main protagonist Yuji Itadori. He went to the rival school Kyoto Jujutsu High.
In the Kyoto Goodwill Event Arc, he wasn't one of the Kyoto students actively trying to kill.
However, the readers still considered him a pseudo-enemy already. Gege Akutami’s choice to make Kokichi the spy was a strange one but it worked out in the best of ways as we got to know his backstory.
So was Mechamaru secretly a hero or just a double agent looking out for himself?
Mechamaru’s Past and Betrayal
Kokichi was born with a sickly and frail body due to heavenly restrictions. He used Ultimate Mechamaru as a proxy to attend school.
Because of his skin's sensitivity, Kokichi always felt as if he was being stabbed with needles, which prevented him from stepping outside because natural light would harm him even more.
As a result, he spent most of his time alone, interacting with humans mainly through cursed corpse puppets like the Mechamaru, which he controlled remotely.
Poor Kokichi was further handicapped by the fact that he lacked a right arm and was paralyzed from the waist down.
Kokichi was a shy and reclusive guy who despised the fact that he was born without a future outside of jujutsu.
He proudly claimed that he would gladly return every ounce of cursed energy in exchange for a regular, normal human body.
Out of desperation, Kokichi gets into a Binding Vow with the adversary in order to have a healthy physique to realize his dream of hanging out with his friends with his real self.
He shared information with them but stipulated in the binding vow that they could not harm his fellow Kyoto students.
Does Mechamaru Die?
Before the events of the Shibuya arc unfold, Kokichi meets with Pseudo-Geto and Mahito.
He complains to them about how they weren't supposed to attack the Kyoto students during the exchange event but they simply pass the blame on Hanami.
Mahito suggests that they kill Kokichi, but Geto reminds him that they must first heal him or something bad will happen to them.
Mahito then performs his Idle Transfiguration on Kokichi, restoring his body to its original state.
Kokichi and Mahito get ready to fight once Kokichi has been healed. During the battle, all Kokichi could think of was being with his friends in his healed body.
However, he never got the chance to do so because Mahito killed him despite his meticulous planning.
Is Mechamaru a Tragic Villain?
It would be a stretch to call Kokichi a villain, or even a true antihero. Sure, his actions led to severe harm to the Jujutsu Sorcerers.
However, his actions stemmed out of a pure and genuine desire to live a normal life amongst people he loved.
Moreover, as an act of redemption, Kokichi also planned ahead in case he got
killed. A machine of his with some of his consciousness managed to find Yuji Itadori during the events of the Shibuya Arc and was very crucial in helping our heroes survive and plan for battle.
Miwa was in possession of Kokichi's final machine. He begs her to turn back as she and the other Kyoto students make their way to Shibuya.
Miwa asks, tearfully, if she was so weak that he had to act in secret to ensure they wouldn't be available on the 31st, only for Kokichi to respond that he was actually weak for making these errors.
As he begins to shut down, Kokichi tells Miwa that he loved her and wanted to protect her from harm, even if she didn't want it.
Before fading away, he bids her farewell and tells her to live a happy life, since it was his only wish.
To me, Kokichi was just a victim of cruel and unfortunate circumstances beyond his control who ended up hurting a lot of people, including himself.