Mesopotamian king lists begin with legendary kings of an antediluvian period who reigned for improbably long periods of several thousand years, and gradually move to quasi-historical figures such as Dumuzid and Gilgamesh, who represented a transition between purely legendary and historical figures. This sequencing is an example of how the Mesopotamian kings served as figures intermediate between gods and humans, with the kings standing in relation to other people as the gods stood in relation to the king. This meant that ideal kings were considered stronger, longer-lived, wiser, and more moral than ordinary people. The king was expected to increase the wealth of his kingdom and lead it to military victory, but also to glorify the gods and establish order and justice.
What made a ruler legitimate was not just tradition and heredity but a special relationship to the gods, which included carrying out the will of the gods and acting as a conduit for divine justice, having special care for the weak, widows, and orphans. That meant that an unjust king was not considered a legitimate ruler. The Ur-Nammu Code states as signs that Ur-Namu was a good king that the following was true under his rule:
The orphan was not delivered up to the rich man; the widow was not delivered up to the mighty man; the man of one shekel [a poor man] was not delivered up to the man of one mina [a rich man].
Similarly, the Code of Hammurabi describes the role of the king in terms of authority deriving from the gods and the obligation for a king to be just and improve the lives of his subjects:
[The gods] Anu and Bel called by name me, Hammurabi . . . to bring about the rule of righteousness in the land, to destroy the wicked and the evil-doers; so that the strong should not harm the weak . . . and enlighten the land, to further the well-being of mankind.
Although the king had absolute power over people, he served at the pleasure of the gods, and if he ceased to serve the gods, might lose authority and position.
The ideal ancient Mesopotamian king fulfilled a number of offices. First, he served as the representative of the gods to his people. Although ancient Mesopotamian kings were not necessarily considered to be divine (Gilgamesh, you may remember, was 2/3 god and 1/3 man), they were supposed to enforce the will of the gods on earth. This often involved a priestly function.
Part of serving as a divine agent meant preserving and enforcing justice in their kingdoms. Kings were the ultimate legal authority, and they appointed judges to help enforce the laws. Many kings--such as Hammurabi--drafted complex legal codes for their people to follow.
Finally, ancient Mesopotamian kings were supposed to be exemplary military commanders. Ancient kingdoms were fundamentally expansionist; they believed the only way to preserve their power was to continually increase their power by conquering their rivals. Thus, kings had to be skilled in the art of war. Poor military commanders would quickly be conquered by more powerful city-states.