WASHINGTON (AP) — The top United Nations envoy to Somalia warned Tuesday that the U.N. and other foreign diplomats may have to withdraw from the war-ravaged nation if they continue to be attacked.

U.N. Special Representative Nicholas Kay said attacks that cause "significant losses" would likely force international officials to leave or, at least, pare down their missions in the Somali capital of Mogadishu.

"I am deeply conscious that if we make a mistake in our security presence and posture, and suffer a significant attack, particularly on the U.N., this is likely to mean to us withdrawing from Somalia," Kay said at the U.S. Institute of Peace in Washington.

"There are scenarios in which if we take further significant losses, then that would have a strategic effect on our mission," Kay said.

Western diplomats began increasing ties with Mogadishu after Somali civil activist Hassan Sheikh Mohamud was elected president in September 2012. At the time, the West cautiously predicted improvements in Somalia's security, given the expected stability Mohamud's government would bring to the failed state and the ouster of militant network al-Shabab from Mogadishu the year earlier.

But al-Shabab has continued its drumbeat of deadly attacks against diplomats, aid workers and the Somali government.

On Tuesday, two al-Shabab gunmen killed a Somali legislator as he left his home in Mogadishu, marking the second fatal attack on a member of parliament in as many days. Earlier this month, a gunman at an airport in Somalia's Puntland region shot and killed two consultants working for the U.N. Office on Drugs and Crime.

And in June 2013, the U.N. compound in Mogadishu was the scene of a deadly suicide attack staged by al-Shabab, which has called the U.N. "a merchant of death" in Somalia.

The violence has slowed U.S. enthusiasm for stepping up its Somali mission, which is currently based in neighboring Kenya due to the security threats. It's unlikely that the U.S. will establish an embassy in Mogadishu for at least another several years.

A handful of counties — including Great Britain, Turkey, Sudan, Libya and Yemen — have embassies in Mogadishu. The European Union also has an office there.

As recently as last week, officials announced the deployment of about 400 Ugandan troops to Somalia under a new United Nations guard unit charged with protecting U.N. staff and installations. It's part of an effort that Kay described Tuesday as the U.N. re-bolstering its presence in Mogadishu after pulling back to Kenya following last June's attack on its compound.

"We have to measure our presence," Kay said, adding that he has encouraged more U.N. member states to open or expand programs in Somalia.

However, he described a perilous balance between "believing that it is right that we should be there" and facing the risk of doing so.

"I'm also deeply conscious there are risks," Kay said. "And if we got hit very badly, it might have an impact."

———

Associated Press Writers Abdi Guled in Mogadishu, Somalia, and Jason Straziuso in Nairobi, Kenya, contributed to this report.

———

Follow Lara Jakes on Twitter at: https://twitter.com/larajakesAP