U.S. military advisers began moving into position here Monday, hours after Washington and Baghdad signed an immunity agreement, as Secretary of State John F. Kerry said the United States will protect its national security interests even if Iraqis cannot bridge their widening sectarian and political divides.

“If there is evidence that requires some kind of action” before Iraq forms a new government, President Obama “maintains the prerogative of making that decision,” Kerry said, following crisis talks here with Iraqi leaders.

As it prepares for possible U.S. air attacks against Sunni militants of the Islamic State of Iraq and Syria (ISIS), the administration has left open the door for strikes on both sides of the border.

Senior officials in Washington have refused to rule out strikes against ISIS forces in Syria as well as Iraq and have said that the administration’s goal is to preserve some flexibility while awaiting assessments from the newly positioned U.S. military advisers.

One official said any consideration of strikes on ISIS across the Syrian border was still hypothetical and no legal basis for them had yet been determined. Iraq’s request for assistance would provide the legal framework for any U.S. action here. Under administration guidelines, lethal action in countries where no request has been made depends on assessments of threats to Americans or the U.S. homeland.

Kerry arrived here as ISIS forces captured a border crossing between Iraq and Jordan late Sunday, following their takeover of three more towns in western Iraq’s Anbar province over the weekend. The militants have continued a rapid offensive that seeks to erase the border between Iraq and Syria and dissolve modern Iraq.

The Obama administration is committed under existing strategic agreements to helping Iraq, Kerry said at a news conference after meetings with Prime Minister Nouri al-Maliki, a Shiite, and leading Sunni and Kurdish officials.

But he repeated Obama’s insistence that only the rapid resolution of Iraq’s sectarian divide, with the formation of a new government, offers a chance of stability. “Iraq faces an existential threat, and Iraq’s leaders have to meet that threat with the incredible urgency that it demands,” Kerry said.

“The very future of Iraq depends on choices that will be made in the next days and weeks,” he said. “And the future of Iraq depends primarily on the ability of Iraq’s leaders to come together and take a united stand . . . not next week, not next month, but now.”

In the meantime, he said, “President Obama has stated repeatedly that he will do what is necessary and what is in our national interest to confront ISIL [as ISIS is also known] and the threat that it poses to the security of the region and to our security in the long run.”

“None of us should have to be reminded that a threat left unattended far beyond our shores can have grave, tragic consequences.”

Maliki, who has largely defined himself as a nationalist unwilling to take orders from the United States, has asked for airstrikes to stop the rebels’ march toward Baghdad.

If Obama does decide to move against ISIS, it will be in defense of American, regional and Iraqi interests and not in support of any individual Iraqi leader, Kerry said.

Iraq has had a caretaker government since parliamentary elections this spring. Kerry implored political leaders to quickly form a new government, a process that in the past has involved months of horse-trading.

Kerry said Maliki and the other leaders pledged to meet a July 1 deadline to convene the new parliament, elected in April and certified last week, and begin the process of selecting a cabinet, president and, finally, a prime minister.

The Obama administration has done little to dampen speculation that it would support Maliki’s ouster as prime minister, although Kerry was careful to say that the choice is not up to Washington.

Maliki’s political rivals are maneuvering to replace him, and he has lost the kind of robust public support that once was common from U.S. officials. Arab neighbors including Saudi Arabia are openly calling for his replacement.

He and Kerry met for about 90 minutes Monday. Kerry also saw Foreign Minister Hoshyar Zebari, a Kurd, and Parliament Speaker Osama al-Nujaifi, one of Iraq’s highest-ranking Sunni officials.

Officials in Washington have been trying to balance the need to push Iraq toward an inclusive government with preparations to take action against ISIS if it crosses an unspecified security line, which one senior U.S. official said they would know when they see it. Administration officials spoke on the condition of anonymity about closed-door planning.

While the U.S. military has prepared airstrike options, those alternatives have not reached the operational planning stage. Instead, the administration is awaiting assessments from U.S. forces on the ground.

As the Iraq crisis began early this month, Obama authorized up to 275 U.S. troops to be sent to Baghdad to protect the American Embassy and other American installations, adding to several hundred troops already there as part of a defense cooperation group. About 170 of those additional protective troops are now in place, operating under diplomatic immunity as part of the embassy.

The question of immunity for U.S. troops from Iraqi prosecution is a sensitive one. Iraqi refusal to grant immunity protection to American forces was the reason the United States withdrew all of its forces from Iraq at the end of 2011, instead of leaving a planned residual force of thousands of troops.

The immunity question was reopened when Obama announced last week that he would send up to 300 more U.S. “advisers” to assist the Iraqi military. U.S. officials worked throughout the weekend on a deal, which was finalized with Iraq on Monday morning.

It provides immunity for an unspecified number of military personnel, under the authority of the U.S. Central Command, “who are going to be temporarily present in Iraq in connection with the current crisis.”

The first of the 300, two units of about a dozen soldiers each, were taken from the existing embassy defense group, retasked to the adviser mission and placed under the new immunity agreement. The rest of the adviser troops are expected to arrive from outside Iraq in the coming days. In addition to providing advice, their mission is to assess ISIS advances and the state of the foundering Iraqi army.

A joint command center is being established, Kerry said Monday, and assessments made by the Americans “will help define strategy on the security front.”

“But make no mistake,” he said. Obama “has been gaining each day the assurances he needs with respect to potential targeting, and he has reserved the right to himself, as he should, to make a decision at any point in time if he deems it necessary strategically.”

Kerry arrived in Baghdad as U.S. officials said the insurgent advance toward the capital had slowed.

A senior State Department official, previewing Kerry’s agenda Sunday, said there is still concern that ISIS insurgents could attack a revered Shiite shrine in Samarra. A bombing there in 2006 marked the start of the worst violence of what quickly became a sectarian war.

In an interview with CBS News, Obama said one risk of the current crisis is that fighting could spread to “allies like Jordan.”

Jordanian military sources said the capture of the border crossing has prompted a threefold increase of armored patrols and border brigades. Government spokesman Mohammed Momani confirmed the military buildup, adding in a statement Monday that Jordan’s armed forces were “prepared to take any necessary step” to ensure the country’s security.

DeYoung reported from Washington. Abigail Hauslohner in Kirkuk, Iraq, and Taylor Luck in Amman, Jordan, contributed to this report.