Kenya and Haiti signed a security deal on Friday intended to remove the last major obstacle to the deployment of 1,000 Kenyan police officers to the gang-ravaged Caribbean nation.

President William Ruto of Kenya said that after months of delays the agreement would “enable the fast-tracking” of a security mission that has stirred cautious hopes in Haiti, where violence is escalating, but has drawn sharp criticism from Kenyan rights groups.

The mission, backed by the United Nations and largely financed by the United States, has been on hold since January when a Kenyan court ruled the deployment illegal because Kenya and Haiti had not signed a formal reciprocal agreement.

On Thursday, Prime Minister Ariel Henry of Haiti flew to Kenya to finalize such an agreement — even as violent gangs were rampaging through the streets of his own capital, Port-au-Prince, pressing for his ouster.

The benefits to Kenya of leading a security mission to Haiti, one of the world’s most violent and chaotic nations, are unclear. Kenya’s own police have a poor human rights record and the country faces major security challenges on its own borders, in countries like Somalia and Ethiopia.

But in a statement accompanying Friday’s announcement, Mr. Ruto insisted his country had a “historic duty” to press ahead because “peace in Haiti is good for the world as a whole.”

He did not provide the text of the agreement, nor did he indicate how quickly Kenyan police officers might deploy to Haiti. There was no immediate reaction from Kenya’s High Court, which had insisted on an agreement in its ruling on the deployment.

In Haiti itself, a new round of destabilizing violence highlighted why the United States and other nations are clamoring for an international mission to rescue the island nation from the violent gangs that dominate it — even if many of those nations do not themselves want to commit troops to the effort.

On Thursday, the armed gangs that control much of Haiti’s capital, Port-au-Prince, launched coordinated attacks they said were intended to overthrow the government while Mr. Henry was traveling in Africa.

At least four police officers were killed in violence that included attacks on the international airport and several police stations. Businesses, government offices and schools were forced to close. Gang members later boasted on social media about the police casualties they had inflicted.

Jimmy Chérizier, a gang leader known as “Barbecue,” said he wanted to prevent Mr. Henry from returning to Haiti. “With our guns and with the Haitian people, we will free the country,” he said in a video message.

Mr. Henry tried to shrug off the threats. Speaking at a university in Nairobi on Friday, he vowed to hold elections as soon as possible to stabilize the country. “We need democratic governance in order to have people to come and invest in Haiti,” he said.

But back home, Haitian gangs appeared to be sending an “intimidation message” to show that they had formed a united front and “can strike simultaneously,” said Diego Da Rin, a Haiti expert with the International Crisis Group.

Kenya announced the mission to Haiti last summer and by October it had been authorized by both the United Nations and the Kenyan parliament. The United States is providing at least $200 million for the mission, and several Caribbean and African nations have pledged troops to bolster the Kenya-led effort.

But Mr. Ruto is proceeding in the face of staunch opposition from Kenyan critics who say the plan is unconstitutional and inappropriate. Human rights groups say the Kenyan police have a long record of human rights abuses and a poor record in fighting terrorism, which they say does not augur well for the Haiti deployment.

But many analysts say the deployment appears to be part of a drive by Mr. Ruto to burnish his global credentials, and to strengthen his alliance with the United States, even as he faces mounting discontent at home over a severe economic downturn.

Mr. Henry came to power following the assassination of President Jovenel Moïse three years ago, which plunged the country into extraordinary levels of chaos. It also prompted many Haitians to abandon their homeland, contributing to a surge of migrants into the United States.

Since Kenya announced it was willing to send security forces last summer, the situation has grown more chaotic. At least 230 people were killed in February alone as gangs battled for control of territory. The rule of law has largely collapsed: Last month, a 13-year-old girl and her father, a voodoo priest, were killed with machetes by gang members.

Days later, a gang opened fire on a crowded minibus after the driver refused to comply with orders at a checkpoint known as a place where criminals seek to extort civilians.

More broadly, the country is on the verge of collapse. Most basic services are not functioning, under pressure from gang violence and a succession of natural disasters. About 45 percent of the population needs food aid to survive, according to the United Nations, which this week appealed for $674 million in humanitarian aid for Haiti.

“I’ve seen things here that I’ve never seen in my life,” Ulrika Richardson, the U.N. chief humanitarian coordinator in Haiti, told reporters on Wednesday.

With Haitian police unable to quell the unrest, Mr. Henry called for international help. Alongside Kenya, the Bahamas, Bangladesh, Barbados, Chad and Benin have pledged troops for the mission, U.N. spokesman Stéphane Dujarric told reporters on Thursday.

But Mr. Henry’s own legitimacy is shaky, and he has faced regular claims to resign or hold new elections. In such tenuous circumstances, it is unclear how much a Kenya-led force might achieve.

While the United States has provided the bulk of the money for the international security mission, a U.N. trust fund for other contributions has received a lackluster response, with about $11 million committed so far, and another $78 million expected soon, Mr. Dujarric said.

Reporting was contributed by David C. Adams in Florida, Emiliano Rodríguez Mega in Mexico City and Andre Paultre in Port-au-Prince, Haiti.