A Better Man Than I Am- The Enterprise is in the R'Ha system, a part of the Albani Imperium a leading power in the sector, where Kirk and McCoy are attending a betrothal ceremony of an old friend from their days on the Farragut.*
The friend is Major K'dhar from the Albani Imperial Army. His bride to be is Deja, a member of the R'Hanee aristocracy. The Major was part of an officer exchange program with the Federation a decade ago. While they are glad to see their own friend they are shocked at disparity of R'hanee society. At the top are the Albani military and the wealthy R'hanee royalty. At the bottom are the Meks, robots who perform all menial tasks.
The Albani and the R'Hanee assure Kirk and McCoy that the Meks are just simple machines. They have no feelings or thoughts, they just serve. McCoy isn't so sure. He observes what appears to be sentient behavior in Meks, including Green, a Mek who serves the Regiment K'dhar is attached to, The Starfleet officers continue to participate in the betrothal celebrations, but their enthusiasm is somewhat dampered.
Out in space, the Enterprise under the command of Mr. Spock patrols Federation territory along the border it shares with the Albani Imperium and its regional rival the Gealish Republic. Noting a build up arms along the Albani-Gealish border, Spock in forms Starfleet and is instructed to retreat further into Federation territory to avoid inflaming either side. This effectively strands Kirk and McCoy on R'Ha.
As the Imperium gears up for war, fear and paranoia increase as R'Ha is closes to the border. Some of which is taken out on the Meks. McCoy and Kirk accompany K'dhar on a patrol with his regiment, which includes Green. They are ambushed by Gaelish advance forces who have set up a base in the mountains of R'ha's Northern Province. McCoy, K'dhar and Green are captured. Kirk and survivors make their way back to R'ha's capital, where Kirk is greeted by an anguished Deja. Albani patrols fan out but can find no trace of the Gealish troops or their base.
At the Gealish base, K'dhar is repeatedly beaten and tortured. McCoy, who was injured in the fire fight, is thus far spared. Possibly because he is a Federation officer. Green cares for both men, tending their wounds as best he can. McCoy, jokes that Green should have been a doctor. Green, responds with some sadness that is not possible for a Mek. K'dhar is astonished by the care and compassion Green is exhibiting.
Back at the capital Kirk and Deja are desperate to find McCoy and K'dhar. Kirk is scanning on all know Federation and Starfleet frequencies, but without luck. He also tries to contact the Enterprise and Starfleet, but can't get through. The Gealish have blockaded R'Ha and cut off communication. From their hidden base, the Gealish commandos conduct a series of hit and run operations on Albani strategic targets on B'ha.
On the Enterprise, Spock has requested permission to cross the Gealish line and retrieve Kirk and McCoy. The Federation refuses and informs Spock that the Federation Ambassadors to the Gealish Republic and the Albani Imperium are in active talks to ascertain the status of the Captain and doctor.
At the capital, a Gealish strike team conducts an assault on royal compound, Kirk and Deja fight off the strike team with the aid of several household Meks. Which puzzles Deja as it is not part of their programming.
In the prison cell at the hidden base Green comes up with a plan to signal the Albani forces. Using components found in Green's body, the prisoners construct a makeshift comm device, one that can only be used for a single short burst. They send out the signal, but in doing so Green is destroyed from the overload. Something Green knew that would happen.
Albani forces receive the signal and pinpoint the Gealish base. They take the base and free McCoy and K'dhar. In space, the Federation finally decides to intervene and helps the Albani forces break the blockade.
In the aftermath, Kirk and McCoy say their good byes to K'dhar and Deja who have to reconsider the role of Meks in their society.
*This episode establishes that is where they met.