Somewhere in the desert, a cyborg advances on Kahler-Mas, its orders to terminate. It tells Kahler-Mas to make peace with his gods, and his target says they were once the cyborg's gods as well. The cyborg dismisses his statement, and then shoots Kahler-Mas when he goes for his weapon. When the cyborg prepares to kill him, Kahler-Mas asks if he's the last one and his killer informs him that there is one more... the Doctor.
The Doctor, Rory, and Amy stand outside of the town sign of Mercy in the Old West. A "keep out" sign is prominently posted and there are lumps of stone and wood posted around the town border. The Doctor scans it, unaware that the cyborg is watching them, and then walks past the stone border. Amy and Rory reluctantly follow him into Mercy and they see the townsfolk nervously watching them from hiding. The Doctor notices electric streetlamps and points out that they're ten years too early.
The trio goes to the saloon and everyone stops and stares at them. The Doctor asks for tea from the bargirl, Sadie, and the Preacher asks who he is. When the Doctor gives his name, everyone stands up and the undertaker, Abraham, starts measuring him for a coffin. One of the townsfolk, Dockery, immediately asks the Doctor if he's an alien. As soon as the Doctor admits he is, the townsfolk pick him up and dump him outside of town. They all draw their guns but then the Preacher warns that "he" is coming.
The cyborg appears in the distance and the Preacher says the Lord's Prayer as the Doctor turns and sees who is coming. When he tries to run back into town, the town marshal, Isaac, orders the townspeople to let the Doctor across the line. As the Doctor crosses the stone-and-wood line, the cyborg draws nearer... and then disappears. Dockery says that the Doctor identified himself as an alien doctor, but Isaac says it's not the one they're expecting and walks away.
The Doctor, Amy, and Rory follow Isaac back to the jail. He explains that they know the cyborg as the Gunslinger, and he's been near Mercy for three weeks. No one has been able to leave town since and nothing has been able to penetrate the borderline, which just appeared one day. They were able to enter because they had no food. When the Doctor asks what happens to those who cross the border, Isaac shows him the hat that the Gunslinger shot off of someone's hat as a warning shot. The Gunslinger ordered them to turn over the alien doctor, and the Doctor figures that Isaac already knows where the alien doctor can be found. As soon as the Doctor says that, the sole prisoner in the jail tells Isaac that it's time to tell the truth. He steps forward and introduces himself as Kahler-Jex... the doctor.
The Doctor knows of the Kahler, one of the most ingenious alien races in the universe. Jex explains that his ship crashed outside of town and Isaac and the others rescued him and brought him in. He stayed there as the town doctor to repay his debt and helped them with an outbreak of cholera two years after he arrived. Jex also used his ship as a generator to rig up rudimentary head and light. The Doctor asks why the Gunslinger wants him but Isaac says that it doesn't matter. However, some of the townspeople think they should hand Jex over despite what he's done for them. The Doctor suggests that he go and get the TARDIS bring it in, and evacuate the town. Amy is surprised that he's not curious about the situation but the Doctor insists there's nothing to be curious about. When Isaac asks how he's going to get by the Gunslinger, the Doctor says he'll use sleight of hand.
Isaac and Rory run out of town and the Gunslinger spots them. However, its programming prohibits it from injuring Rory an innocent. Meanwhile, the Doctor borrows the Preacher's horse and rides out of town.
The Gunslinger tracks down Isaac and Rory and opens fire when it thinks it has a clear shot at Isaac.
At the jail, Amy offers to take Jex home but the alien explains that he's already given everything good to his people. In Mercy he can start afresh and help people. He realizes that Amy is a mother from the kindness, sadness, and ferocity in her eyes. Amy asks if he's a father and Jex says that he is in a way.
Rory and Isaac arrive at a rendezvous spot and wait for the Doctor to pick them up.
The Doctor stops to check something out in the desert. He examines the ground and finds a buried cable.
At the jail, Jex notices that the electrical lights are flickering.
The Doctor follows the cable to Jex's spaceship but finds no sign of danger.
The Gunslinger closes in on Rory and Isaac and spots them with its thermal sensors. It locks onto Isaac and prepares to fire.
The Doctor scans the spaceship, triggering a high-pitched sonic alarm that echoes across the landscape. The Gunslinger hears it and turns away. In Mercy, the townspeople hear the alarm and Jex nervously comments that the Doctor examining his ship wasn't part of the plan.
The spaceship opens and the Doctor enters the cockpit as the computer asks for the passcode to prevent self-destruct. The Doctor shuts it down just in time and accesses the personal files and discovers an entry about an experimental cyborg program with a list of deceased subjects. He opens the files and watches the video of the agonized subjects.
Amy returns to the jail and Jex aims a gun at him. He apologizes but says the Doctor should have followed the plan.
The Doctor emerges from the spaceship and finds the Gunslinger waiting for him. He says that he knows who both the Gunslinger and Jex are, and asks why the cyborg hasn't walked into Mercy and killed the doctor. It says that doing so would endanger innocents and refuses to hear any offer of a trial.
Jex explains that there are things in his past that even Isaac wouldn’t understand and takes Amy with him as a shield against the Gunslinger. However, Isaac and Rory return and the marshal asks what Jex is doing.
The Gunslinger tells the Doctor that he has fired his last warning shot and that the townspeople should send Jex out. Once he leaves, the Doctor goes back to Mercy and confronts Jex. He tells the others that Jex is a murderer and orders him to sit down. Jex insists that he's a war hero but the Doctor tells the others that the Gunslinger is a cyborg created by Jex and his team. They asked for volunteers and then experimented on them, programming them to kill. Jex explains that they had been at war for nine years and half their planet was decimated. He and his team built an army that routed the enemy and ended the war in a week. The Doctor asks how many people suffered on his operating table but Jex insists that the rules of peace don't apply in a time of war. When the war ended, the cyborgs were decommissioned but the Gunslinger went off-line due to damage and started hunting down the team that created it. Mas and Jex were the only survivors and fled, and their ships crashed on Earth.
Rory wonders what they should do with Jex and Isaac reminds them that Jex saved the townspeople from the cholera. Amy refuses to let the Gunslinger execute Jex and turns to the Doctor for support. He doesn't have an answer, and Jex says that the Doctor is just like him except that he lacks the nerve to do what has to be done. Furious, the Doctor grabs him and drags him down the street to the town borders. Isaac and Rory hold Amy back and then go after him.
As the townspeople gather, the Doctor throws Jex over the border, grabs a gun, and prepares to shoot the alien doctor when he tries to come back. When Jex says he wouldn't do it, the Doctor admits that he doesn't know. Amy grabs a gun of her own and fires into the air, threatening to shoot the Doctor. He points out that she wouldn't shoot but she says that if he's changed, maybe she has as well. The Doctor says that they can save everyone by sacrificing Jex, and that for once he is going to think of the victims first. Amy says that they have to be better and the Doctor has traveled too long on his own to remember that. The Doctor considers and then says they'll think of something else.
As the Gunslinger approaches, the Doctor tells Jex to come back over the line. Before he can move, the Gunslinger arrives and tells him to make peace with his gods. Jex says that he can remember his name even now and asks for mercy, promising not to harm anyone ever again. The Gunslinger ignores him and fires, but Isaac leaps in the way and takes the shot. With his dying breath, Isaac asks the Doctor to protect Jex and the town. The Doctor takes his badge and pins it on, and then tells the townspeople to take Jex to his cell. He then tells the Gunslinger that it's gone on far enough. The cyborg says that they have until noon the next day to turn Jex over or he will kill them all.
That night, the Doctor sits in the jail and contemplates Jex. The Preacher comes in and tells the Doctor that he needs to come out and to bring the marshal's gun with him. The townspeople have gathered outside and Dockery suggests that the Doctor leave the keys and walk away. The Doctor reminds them that he promised Isaac that he would protect Jex and refuses to turn him over. Dockery draws his gun and the Doctor reminds them that Isaac didn't want lynch mobs. He explains that violence just extends violence and doesn't think that Dockery wants to become a killer. The Doctor promises to save everyone and is willing to take the risk... for Dockery and the others. After a moment, the townspeople walk away.
Back in the jail, Abraham brings the Doctor coffee and says that if Isaac trusted him then he does as well... and measures the Doctor for a coffin. Once he leaves, Jex suggests the Doctor do exactly what the townspeople want. However, the Doctor refuses to let Isaac die for nothing and asks what Jex wants. Jex says that it would be simpler for the Doctor if he were just a mad scientist or a healing doctor, not both, but the Doctor warns that he doesn't get to decide how his debt is paid. After a moment, the doctor explains that when he dies, he believes he will have to pay for his sins. He tells the Doctor that morality is his prison and the Doctor gets an idea.
The next day at noon, the Doctor goes out on the street while the townspeople gather in the church and pray. The Gunslinger enters Mercy and aims its gun... and the Doctor draws his sonic screwdriver and emits a sonic beam, shattering the windows and distracting the Gunslinger. It fires wildly as the Doctor runs for cover and quickly recovers.
Amy unlocks Jex's cell.
Rory and Dockery, both having applied marks similar to the markings on Jex's face, move out. The Gunslinger sees them but is unable to confirm that they're Jex. Realizing that it's a trick, he disengages. As the Gunslinger walks past the church, a young girl knocks over some books and it bursts inside. Jex hears the commotion and hesitates, and the Doctor tells him to go so he can save them. The Kahler runs off while the Doctor runs down the street.
The Gunslinger sees the young girl, hesitates, and then walks away.
Jex enters his ship and deactivates the self-destruct.
Realizing that his targeting sensors are useless, the Gunslinger goes to manual and closes in on the Doctor as he hides. The Doctor tells him that Jex has left, but Jex speaks over the intercom system in the electrical lights. He asks the Gunslinger if he will go back to Kahler when he is done and the cyborg says that he can't because he's a monster. Jex admits that he is as well and the Gunslinger vows that he will find him no matter what. However, he refuses to face the Gunslinger and reactivates the self-destruct. Jex thanks the Doctor and says that he must face the souls of those he wronged... and hopes that they will be kind.
The Gunslinger sees the explosion and admits that Jex died with honor in the end. The Doctor offers to take him back to Kahler to aid in the reconstruction, but the Gunslinger says that as a machine of war he has no role in the peace. As he starts to leave, the Doctor suggests that he could protect the peace.
Later, the Doctor is ready to take Rory and Amy on another trip. They ask for a break so that the temporal difference between them and their friends won't grow too great. Dockery and the Doctor jokingly play-shoot at each other and then he leaves with his companions. The townspeople start to walk away and the young girl goes to the edge of the town. Watching over the town is the Gunslinger... the new marshal.