The U.S.S. Enterprise is chosen to be the test ship for the new M-5 multitronic computer system, a computer meant to be able to run a starship without human intervention. Also aboard for the test is Dr. Richard Daystrom, the inventor of the M-5 and an obsessive and unstable individual.
Initially the M-5 performs well, but when it decides to destroy a robot freighter, Kirk orders the test canceled. The M-5, however, protects itself and makes it impossible for it to be disconnected. The computer becomes increasingly erratic, a result of Dr. Daystrom's decision to impress his engram onto the computer as part of its programming. Starting a scheduled war games drill, M-5 uses the full arsenal of the U.S.S. Enterprise to attack four other Federation starships.
In a last-ditch appeal to the M-5, Kirk makes the computer realize that it has committed the sin of murder. Since Dr. Daystrom would be ethically abhorred at such an act, the M-5 is equally penitent and tries to commit suicide by leaving the U.S.S. Enterprise defenseless against a counter-attack by the remaining other starships. At the last moment, Spock and Scott are able to finish disconnecting the M-5 unit. Kirk keeps the shields down, gambling successfully that the attacking ships would not fire on an undefended vessel. Restoring communications next, the fleet is called off.