The Enterprise is on an exploratory mission to investigate an asteroid with life-forms located in a little known sector of the galaxy. A humanoid population is detected, but they have highly erratic brain waves. A landing party of Kirk, Spock, McCoy and geologist Lieutenant Freyer, beam down and discover a city with peaceful built-up communities alongside pockets of utter devastation. The Enterprise men meet and calmly talk to a group of local inhabitants for a few minutes, and Freyer tastes a soil sample. Suddenly, McCoy proclaims after using his tricorder, "Hold it, Jim... Their brain waves have suddenly gone wild!"
Kirk and his men are attacked by the now violent locals and they dash for cover when weapons are brandished. They observe continued fighting and then stun several locals when there are threatened with a flame thrower.
To avoid more conflict, Kirk in his team seek refuge in a cave, leaving Freyer outside on guard. McCoy states that his tricorder senses that the locals have again become calm, but Freyer storms in with his phaser set to disintegrate. His brain waves have become erratic! Kirk tackles him to allow Spock to apply the Vulcan neck pinch from behind. Fearing their own sanity, the four been up to the Enterprise.
Back aboard the mighty starship, McCoy tells Kirk that Freyer has had a remission. Apparently he and the inhabitants have Akwoods Syndrome caused by a virus lodged in the brain - it produces temporary insanity. Freyer must have gotten the virus from his tasting the soil sample. When Kirk asks McCoy, "Is there a cure, bones?" the doctor replies, "Yes, this serum I synthesized... but it's not working as it should be!" Even with a maximum dose, Freyer's brainwaves indicate that the virus is still active. Just then, Spock reports that the ship's sensors detect activity below the asteroid's surface. When the sensor image is relayed to the screen in sickbay, McCoy is amazed, saying, "that's the virus, Jim... grown to millions of times its normal size!"
The starship captain and his chief medical officer rush to the bridge to conduct further analysis. When they arrive they are met with an even stranger image on the main viewscreen - a huge brain! Spock explains, "That brain is a living, thinking entity at the asteroids core! like a massive radio transmitter, it emanates its energy to all living things on that world!" It's the brain that is infected with the virus, not the people, and their periodic insanity only occurs when the giant brain is disrupted by the virus.
This had been going on for 107,600 years. The senior staff discusses the situation and McCoy determines the only possible course of action, "... We've got to destroy the giant virus! Otherwise the people of the asteroid will be at its mercy until they annihilate themselves!" But Spock states that such action is a violation of the Prime Directive. They argue over what to do, when sickbay summons them suddenly - Freyer has had to be put in restraints because he grew murderously violent.
Kirk makes the command decision to go and try to kill the virus in the asteroid brain. Freyer, who acts normal due to his being given a tranquilizer, is allowed to come along. In the transporter room, as Kirk, McCoy and Freyer prepare to beam down, Spock tells them that he has determined that the brain functions on energy and not blood or other liquids.
The trio materializes inside the massive brain and they quickly set out to find the one part of the brain that McCoy says the virus tends to concentrate in. As they trudge along, they hear buzzing which turns out to be antibodies, which look like fuzzy grapefruit-sized balls with several sharp spikes. The antibodies menace them, and Kirk takes out his phaser. McCoy warns, "No, Jim! If we disable them even for an instant, we'll leave this brain vulnerable to every disease in the universe! We've got to stay clear of them long enough to complete our mission!"
They rush along, when McCoy discovers that the nearness of the antibodies has thrown his tricorder askew - preventing them from finding the lair of the virus. But Freyer, who seems to have a sensitivity to the germ, leads them to a crevasse. Freyer jumps down amid the purple hairy-looking lumps. They shamble towards him as McCoy readies his sprayer which is filled with anti-viral serum. With the antibodies massing around them, McCoy leans over and sprays the viruses. Then when McCoy and Kirk go down into the pit to assess if the viruses are dead, they find that they are, and so is Freyer. Apparently they engulfed him before succumbing themselves.
After Kirk and McCoy beam back aboard the Enterprise, Spock reports that the computer readout indicates the virus is destroyed. Kirk states that he'll recommend that the Federation admit the asteroid to membership. McCoy mocks Spock for arguing that the Prime Directive dictated their non-involvement, and when Spock quotes that the actions were technically allowed in accordance with an obscure special circumstance, Kirk and McCoy begin to laugh. Spock asks, "I fail to see what everyone finds so amusing!"