Because all of his other agents are busy handling security for the World Peace Conference, Brigadier M assigns Bond, James Doctor and Miss Jo Peel to look into the fantastic new Keller Device that prog-rock-loving scientist Doctor Keller has devised to solve the problem of crime.
Just like in that one Avengers episode, the machine isn't actually a machine at all; it's a mind-control parasite in a box. This one works by eating hateful or evil impulses, instead of impulsive behavior, but that works just as well for turning criminals nice. The only downside is that they now can't stand to listen to Beethoven, and lovely Ludwig Von was their favorite.
The parasite thing can also barf up those impulses telepathically to turn other people hateful and evil, or to fill their minds with their greatest fear, or just to straight up kill them. Pretty versatile parasite.
Anyway, it turns out that Keller is actually the Mastermind from that other Avengers episode, and he's repeating his trick of using a mind control device to steal a nerve gas missile so he can hold a peace conference ransom. Hey, wonder if it'll be the same peace conference that all of UNIT is busy doing security for?
By the way, where exactly did Missy Delgado get this parasite? Did he bring it along as a backup plan just in case the whole Auton thing failed, and he somehow got stuck on Earth but didn't get killed or captured or anything? Oh well, back to our suspension of disbelief.
Of course he would be the Mattress if he didn't have at least two convoluted plans at once. He's also going to use his device to kill the Chinese delegation, to cause the conference to fall apart to ruin the chances for world peace. At the same time as he holds that same conference hostage with his missile. I can't see how that could possibly be a problem. (He's also going to kill some of the American delegates, but as far as I can tell, that's more just because fuck Americans.)
The way he's going to sneak his machine into the Chinese delegation is simple: His lab assistant is a young Chinese lady (because Don Houghton got a young Chinese lady to marry him by promising to put her on British TV), and he has a telepathic device to control her. And, because all Chinese people know each other, she can just walk in.
Jo gets captured and escapes, the Doctor gets captured and escapes, the two of them get captured and escape together, then they get captured again, and that advances us far enough into episode 4 that the plot can start moving again. Along the way, the Mattress tests the fear setting on the Doctor, and we learn that his greatest fears include Cybermen, War Machines, Silurians, and not having the rights to show the Daleks onscreen.
But it turns out that the Mattress can't actually control his parasite very well. First it turns the whole prison hostile, then it starts showing the Mattress himself his greatest fear (which is a 10-foot-tall Doctor laughing at him). So the Mattress runs off and decides to steal the missile a different way: just set a bunch of the prisoners free in exchange for helping him hijack the missile. Which it seems like he could have just done from the start, which would have been a lot simpler than either of his two conflicting original plans. But then he also had a whole bunch of evil mercenaries working for him in the first four episodes, and just having them steal the missile would have been even easier.
For some reason, Mike Yates is leading the British Army troops guarding the missile, so the prisoners make sure to only shoot him in the arm while murdering all the other soldiers, because they can tell that the Mattress wants some private time with Yates, probably involving leatherwear and tight straps. They capture Yates before he can call the The Brig, but that turns out not to matter, because the Brig miraculously guesses what's going on with the prison and the missile anyway.
Meanwhile, the parasite has grown even more powerful, and now it can for some reason teleport, so it does that. So the Mattress has to work with the Doctor to build a machine to subdue it. Then the Brig's raid happens, so the Mattress runs off with the missile while UNIT kills all the bad guys and the Doctor struggles with his machine to contain the parasite's machine. Eventually, the Doctor's machine fails. But fortunately, just at that moment, Barnham, one of the prisoners who had all the evil in his mind eaten by the parasite, shows up. Since he's now a perfect goody-two-shoes, that somehow starves the parasite into a coma. Which gives the Doctor an idea for a plan.
The Doctor still has the Mattress's TARDIS's dematerialisation circuit from last time. He calls up and offers to trade it for the missile. But he brings along Barnham and the machine, and Barnham turns the machine on, which somehow now works even though Barnham is there, so it attacks the Mattress again. But he steals a van and runs away, stopping to knock over Barnham with his van.
Fortunately, the Doctor is able to wire the missile to blow up, taking the parasite with it, and wounded-but-probably-not-mortally-so Barnham too, harmlessly spreading its nerve gas all over some place in the middle of England right near a major highway, a prison, and an airport.
Jo is sad that Barnham had to die, but the Doctor says "I feel guilty too, but at least I didn't lose the Mattress's dematerialisation circuit, so really, I killed that innocent man for the greater good". And then he realises he dropped the circuit. D'oh! The Mattress calls him to say "Nya nya, look what I found", and the Doctor is furious. Then he realises that while the Mattress is free to travel around time and space, he has to hang out with the Brig, which makes him even more furious. And he says this right in front of the Brig, too.
The traditional opinion is that this is like a season 7 story (that's Pertwee's and UNIT's good season) but with Delgado added in, which is the best of both worlds.
That's a crock of shit. It's the most season-8 story of all of season 8. The good is all season 8: Delgado being Delgado as fuck, the fight scenes being Havoc as fuck, and they've figured out the 4+2 structure well enough that you don't notice the padding too much unless you binge it. The bad is also all season 8: the Doctor is a patronising ass, Jo is useless, UNIT are Keystone Kops, and none of the plot makes any sense.
The production values are pretty impressive for Doctor Who—until they run out of budget and get cut off, which just makes the cheap scenes (like the Chinese dragon) more painful by contrast. For a long time, it was only available in low-res black and white, which probably helped, but nowadays you can see it restored to colour.
It's not bad. Besides the Delgado and Havoc goodness, the scenes with the Doctor's and Master's greatest fears are pretty cool, and Pertwee does some of his best acting when he's helpless before a thug with a shotgun instead of an alien or a supervillain. If you like the Master season, you'll like this one. Just don't let anyone convince you that it's somehow the standout story of the year, a throwback to glorious season 7; it's not.