The U.S. and Iraqi government have hammered out a controversial deal granting U.S. troops some immunity from prosecution in the fledgling Iraqi court system, clearing the way for up to 300 special operators to begin deploying to Iraq, a defense official said Monday.

President Obama last week said he would send up to 300 military advisers into Iraq to collect intelligence and assist the Iraqi security forces in their fight against extremist militias aligned with the Islamic State of Iraq and the Levant, or ISIL, an al-Qaida offshoot.

But those deployment plans stalled temporarily amid negotiations between Washington and Baghdad over who gets legal jurisdiction in the event of alleged misconduct by a U.S. service member, a defense official said.

The U.S. troops deploying to Iraq will be armed and authorized to use lethal force in self-defense. U.S. officials want to ensure that any alleged misconduct by American service members would be handled under the Uniform Code of Military Justice.

“Iraq has provided acceptable assurances on the issue of protections for these personnel via the exchange of diplomatic note,” Rear Adm. John Kirby, a Pentagon spokesman, said Monday in a statement.

“We believe these protections are adequate to the short-term assessment and advisory mission our troops will be performing in Iraq. With this agreement, we will be able to start establishing the first few assessment teams,” Kirby said.

The deal is not a status of forces agreement and is limited to the 300 personnel Obama authorized for deployment June 19, Kirby said. The protections will be similar to those provided for diplomatic and military personnel working inside the U.S. embassy, Kirby said.

The politically sensitive talks leading up to the deal echoed similar ones from 2011 that ultimately led to the complete withdrawal of U.S. troops at the close of the eight-year war.

In 2011, U.S. military officials said Iraq’s refusal to approve a proper status of forces agreement that included legal protections for service members was the primary reason for the complete withdrawal of U.S. troops. At the time, the U.S. insisted that legal protections under a status of forces agreement should be approved by the Iraq parliament. But the Iraqi legislature refused to approve such a deal, in part because many Iraqis opposed the continued U.S. troop presence.

A White House spokesman said the current situation is different from 2011.

“It’s different in a couple of ways,” said White House spokesman Josh Earnest. “The first is that, you know, we’re dealing with an emergency situation. ... There is an urgent need for these advisers to be able to do their work on the ground in Iraq.

“There’s also a difference between the small number of advisers we’re talking about, 300 [now] and the few thousand troops” that were under discussion in 2011, Earnest said.

“Given those differences ... the assurances that we’ve received [from Iraq] are sufficient, in the mind of this administration and in the mind of the commander in chief, to assure their security as they do their work,” Earnest said.

Legal experts say seeking approval from the Iraqi parliament in 2011 made sense because the legal system there is unreliable and the government’s leadership could change at any time.

“I would want the legislative buy in. I can see why they’d want that,” said John Altenburg Jr., a retired Army judge advocate and two-star general who is now an attorney in private practice and has represented clients involved in the Iraqi legal system.

Incidents involving alleged misconduct by U.S. troops could easily become politicized inside Iraq and parliamentary approval could make it less likely that Iraq would simply ignore a deal.

Approval by Iraqi lawmakers would “give you some confidence that the government will back it because you’re getting more people to buy into it. It’s not just the [prime minister], who in that country could be gone in a couple of days,” Altenburg said.

If the U.S. seeks approval from the Iraqi parliament, that could take weeks and may get entangled in Iraqi politics.

The ISIL has continued to gain ground in its advance against the Iraqi army. Over the weekend, the ISIL fighters seized the Syrian border town of Qaim in Anbar province, giving the extremist militia control over most of the desert border between Iraq and Syria.

The 300 military advisers slated to deploy to Iraq to work alongside the Iraqi security forces. Their primary mission will be to gather intelligence about the conditions on the ground and the state of the Iraqi security forces, which have suffered a devastating wave of desertions and battlefield defeats.

Pentagon officials emphasized that the small teams of advisers will not be on a combat mission.

“There is no intent for these advisers to be engaged in direct combat,” Army Col. Steve Warren, a Pentagon spokesman, said Monday.

“They don’t have an offensive role. They are strictly there as advisers, so they should not, as a matter of routine, come into direct contact with the enemy,” Warren said.