In September 1878 he was attached to the staff of a British mission to Kabul, Afghanistan, which the Afghans refused to accept on their soil. In May 1879, after the British-Indian forces had invaded Afghanistan, and the death of Afghan Emir Sher, Cavagnari negotiated and signed the Treaty of Gandamak with Sher Ali's son and successor, Yaqub Khan. With this treaty, the Afghans agreed to admit a British representative to Kabul. The post was conferred on Cavagnari. He took up his residence in July 1879. However, on 3 September 1879, Cavagnari and the other European members of the mission, along with their guards who were made up of soldiers of the Corps of Guides, were killed after Afghan troops mutinied against them.