Myotsu-ji, a Shingon temple, is said by JNTO to have been built in 806 by Emperor Kanmu to pray for the success of Shogun Sakanoue no Tamuramaro. But according to the current (2016) temple signage, Myotsuji was built by the Shogun himself. At that time the Shoguns were the real, military, rulers and the Emperors were only the theoretical, civilian, rulers of Japan. So if a Shogun had a vision of "let's build a temple here," it is likely that the Heian court would rubber-stamp it. And that, I guess, is just what might have happened.
The temple's present buildings are Kamakura period and later. Its main hall and three-story pagoda (following pages) are National Treasures; several of its wooden statues were created in the Heian period, and are designated Important Cultural Properties.
The steep approach to the temple is seen here.