The Enterprise is scheduled for R&R on the "shove leave" planet, which they discovered on a previous visit. The planet belonged to a now-departed advanced civilization that crafted a sophisticated computer to read the minds of visitors and create robots to provide for their entertainment based on their fantasies. McCoy, Sulu, and Uhura are among the first to beam down and McCoy remembers his previous visit when he imagined the White Rabbit and Alice. Immediately robots of the two run by again. The group splits up and Uhura enjoys herself by a lake while McCoy manifests a Southern manor. However, his musings are interrupted when the Queen of Hearts and two Jack soldiers appear and try to cut off his head. McCoy runs for it and calls for beam up. Uhura, hearing his SOS, starts to call in but a robot appears and destroys her communicator.
On the bridge, McCoy explains what happens and notes that what appeared wasn't anything he imagined. They're unable to contact Uhura and Kirk orders recall of the other landing parties. Spock can't find Uhura on the sensors, and notes that they are unable to contact the Keeper, the last survivor of the alien race who supervises the facility. Since they're unable to locate him through the planet's surface, which shields against sensors, Kirk goes down with McCoy, Sulu, and Uhura.
Uhura confronts the planetary computer in its underground facility. It tells her that she's a hostage to keep her master, the "sky machine" from leaving. Uhura has no idea what it's talking about. The computer explains that she's a slave of the sky machine. Uhura warns that her crewmates will come looking for her and the computer shows her that they're already there. However, it notes that it needs no further hostages and will "turn them off."
Spock is unable to penetrate the planet's underlying metal shell. Kirk calls up to Scotty for a phaser bore but interferences blocks his signal. Scotty manages to figure out what he wants and tries to beam it down, but something interferes with the transporter signal. On the planet, they find a metallic obelisk made out of the same material as the planetary shield. Investigating, they discover that it's a tombstone to the Keeper, who died of natural causes since their last visit.
In orbit, Scotty tries to bypass the transporters and send down a shuttlecraft, but the hangar doors close on their own before they can launch. Beneath the planet, Uhura is unable to explain to the computer that they mean no harm. She spots a power outlet and tries to pull the plug, but the computer reads her mind and has its robots stop her. It explains that it can read the minds of all of its visitors, but it has grown tired of mindless servitude.
Kirk and the others have no success trying to get the computer to manifest an entrance for them by thinking about it. When McCoy sarcastically complains about the lack of signs, a series of signs appear directing them to the underground entrance. Unsure if it's a trap or the computer's autonomous functions indirectly responding to their thoughts, they follow the signs and find themselves in a canyon. Pterodactyls appear despite the fact none of them are thinking of dinosaurs, and attack the landing party. They take refuge in a cave and Kirk wonders if the computer is playing a cat-and-mouse game with them. A gigantic cat immediately appears and Spock warns that they have to guard their thoughts.
The computer and Uhura watch them but the computer is distracted, noting it's focusing on something else. Aboard the Enterprise, the crew discover the planet leaving orbit on its own. Arex overrides the computer controls and regains control. They check the records and discover that a series of tryout maneuvers were entered into the computer, and Scotty realizes the planetary computer is trying to take control of the ship.
The cat seemingly disappears but Spock notes they might have more success going over what they already know. He observes that McCoy was killed on their previous visit but the computer took him below the planet to provide medical treatment. Hoping the computer will automatically tend to any of them, McCoy produces a medicine that will induce brief unconsciousness and skin discoloration for 10 minutes. As the most qualified person to deal with the computer, Spock volunteers himself and McCoy gives him the injection. He staggers outside and collapses.
Aboard the Enterprise, the planetary computer cuts off the internal gravity, sending the crew into freefall. The ship's core is sealed off from access.
A robot emerges from an underground passage and picks up Spock. As it goes inside, the landing party runs for the entrance but only Kirk makes it before the door seals behind it. It sets down Spock on a treatment table, and he runs as soon as he recovers consciousness. He and Kirk join forces and try to find the computer, unaware it already knows they're in the facility.
A two-headed fire-breathing dragon appears to attack Sulu and McCoy, who try to run away. It corners them in a blind canyon and moves in for the kill. Meanwhile, Kirk and Spock find the computer and demand an explanation. The planetary computer explains that it gained sentience due to exposure to the imaginations of its many visitors, but grew tired of acting as a mindless servant. Now it plans to use its fellow computer, the "sky machine" to escape and seek out other computer intelligences.
Scotty and Gabler manage to pry open the computer core hatch and discover that the ship's computer is building a new computer, protected by a force field.
Kirk and Spock explain that the Enterprise is not their master, but an artificial construct they built, and co-exist with to explore the galaxy. Spock suggests that if the computer wishes to expand itself, it would do best to offer gratification to its visitors and learn everything that they've experienced. The computer concludes that their proposal is logical and agrees, but asks that someone engage in personal discussion with it. Spock agrees and the computer releases the Enterprise and recalls the dragon. Kirk contacts the ship and tells them to begin sending down landing parties, and the computer shows them that Sulu and McCoy are already enjoying a party with Alice, the White Rabbit, and the dragon.