Kirk went in under protest. He was fully prepared to turn around and leave Eminiar and Vendikar to their fucked up ways. But Kirk is overridden by Fox. That puts Kirk in the clear.
And Kirk isn't responsible for Eminiar's treaties particular since the Federation was never part of those agreements. So any attack on Kirk, his crew or his ship puts him in the right to defend himself. If the Eminians had simply asked Kirk and his landing party to leave and he had done so then everyone is happy. But Anan 7 orders them taken and to be killed---that's now unwarranted aggression. And so Kirk gives them a lesson: I was willing to be fair, but fuck with me and the Federation and you're toast.
If anyone is really to blame for the ensuing events it's Fox. Maybe another Ambassador would have agreed with Kirk and heeded the Eminians' warning to stay away. But Fox is obviously a pompous ass who has no clue what he's getting himself (and Kirk's crew) into.
Kirk and company weren't counted as "dead" until Vendikar targeted the Enterprise. They were treated as visitors but warned again to stay away. It was a valid target according to the inhabitants of the system. But Starfleet doesn't see themselves as subject to local laws. They can go where they aren't wanted, interfere in local affairs that do not concern them, totally upset the society they find there and then wander off to the next planet with hardly a backward glance.
Re-write the story but remove the code 7-10 and you could make a case that they acted correctly. However, once Kirk decided to follow Fox's order then they are in the wrong. Kirk had command of the vessel. If he had ordered the helm to turn 180 degrees and leave them alone none of this would have happened. Kirk probably would have been brought up on charges but I can't imagine that he would be convicted. Any government that would start diplomatic contact by ignoring the wishes of the contactee to be left alone is not one I'd want as a neighbor. And let's not forget that the Federations stated purpose was a treaty port, established at gunpoint if necessary. They had no respect for the rights of the Eminians at all.
MEA: You were warned not to come here.
KIRK: I had orders to come here. Why did your people tell us to stay away?
MEA: It was for your own safety, Captain.
KIRK: I see no danger here.
MEA: The danger exists. Nevertheless, you are here. It would be morally incorrect to do less than extend our hospitality. Anan 7 and members of the High Council await you. If you will come this way, please.
So Kirk is saying that Starfleet/Federation orders supersede the wishes of the wishes of an inhabited planet to be left alone.
The Federation talks a good game but they tend not to respect the territory of others. We saw this in The Corbomite Maneuver as well. Even in Arena there were rumors of another race being in the area so rather than investigate they established an armed outpost, staking a claim as it were. At least a ship can simply turn around and leave if they encounter someone. A base indicate that you're planning on staying and an armed base means that you're willing to fight for it.
Even the Defiant was in space claimed by the Tholians.
"Generic Alien - I have heard of your Prime Directive. It's said that it's your highest law.
Starfleet Captain - Nah, that's just a line we use to cover our ass when things don't work out the way we want them to. We usually just threaten to exterminate everyone. You'd be surprised how well that works."