KABUL — Acting defense secretary Patrick Shanahan met with Afghan leaders in Kabul on Monday as the Trump administration made an intensified diplomatic push to end the United States’ longest war.

Shanahan, a former Boeing executive who has served in a senior Defense Department role since 2017, made his first visit to Afghanistan six weeks after he became Pentagon chief following the resignation of retired Marine Gen. Jim Mattis.

Shanahan is under consideration to be nominated as Trump’s second defense secretary at a moment when the president is taking steps to end the counterterrorism wars that have dominated Pentagon activities since the 9/11 attacks.

While military leaders are proceeding with a plan to pull American troops out of Syria, ending a ground mission against the Islamic State, officials say a decision has not been reached to withdraw from Afghanistan, where U.S. and NATO forces support local forces battling the Taliban and other militants.

Confusion has surrounded what appeared to be a decision in December to withdraw as many as half the 14,000 U.S. troops in Afghanistan, an indication of the contradictory messages that have often characterized Trump’s foreign policy and internal debates about a war that even military leaders characterize as a stalemate.

Speaking to reporters ahead of his arrival, Shanahan said he had not received orders to withdraw.

“I think the presence we want in Afghanistan is what assures our homeland defense and supports regional stability, and that any type of sizing is done in a coordinated and disciplined manner,” he said.

Shanahan’s inaugural overseas trip as acting defense secretary comes as he seeks to prove his foreign policy credentials and establish a more public profile after 1½ years as deputy defense secretary under Mattis.

Talk of any substantial troop reduction appears to be on hold for now as the White House tries to give diplomats a chance to foster peace talks. In recent weeks, Zalmay Khalilzad, a former U.S. ambassador to Iraq and Afghanistan, announced that his team of negotiators had reached agreement in principle on core issues between the United States and the Taliban, a step diplomats hope will set the stage for broader peace discussions that include the Afghan government.

In Kabul, Shanahan held talks at the presidential compound with Afghan President Ashraf Ghani, national security adviser Hamdullah Mohib and other officials. Earlier in the day, he visited an Afghan commando training site and received an update on the war from Gen. Austin “Scott” Miller, the commander of U.S. and NATO forces in Afghanistan.

Miller, who previously commanded the elite Joint Special Operations Command, has overseen an increase in the pace of strikes and raids against militant targets, which officials hope will give diplomats leverage in their effort to establish negotiations. So far the Taliban has refused to hold direct negotiations with the Afghan government, which it considers illegitimate.

At the same time, U.S. and NATO forces continue their effort to ensure that Afghanistan’s military, which has taken heavy casualties and remains reliant on its small cadre of elite commandos for offensive operations, can fend off Taliban attacks.

A December Pentagon assessment described the battlefield situation as a continued impasse but said “the combination of military escalation and diplomatic initiative have made a favorable political settlement more likely than at any time in recent memory.”

Some senior Afghan officials are worried about possible U.S. concessions to the Taliban and the Kabul government’s exclusion from the recent discussions, which have taken place in the gulf state of Qatar. But the Afghan leader, welcoming Shanahan to his 19th-century palace in central Kabul, made no mention of that to Shanahan in introductory remarks, which were witnessed by reporters.

According to a statement issued later by Cmdr. Sean Robertson, a military spokesman, the talks highlighted the need for a political settlement “that ensures Afghanistan is never again used as a safe haven from which terrorists can plan and launch terrorist attacks against the United States, our interests, and our allies.”

For years, Pentagon leaders viewed the idea of striking a deal with the Taliban with skepticism, adding to an array of obstacles that scuttled past attempts. More recently, as the massive military effort has failed to yield a lasting defeat of the militants, they have embraced that goal, even as they hope to retain a small foothold to ensure that hardcore militant groups such as al-Qaeda and the regional Islamic State branch don’t regain strength.

Speaking at the U.S. Institute of Peace in Washington on Friday, Khalilzad said Trump had been clear about his desire to end what he characterized as “endless wars.”

“But I think the president also would like Afghanistan to not become a threat to the United States again, for it not to be a platform for terror, and he’s determined to protect the U.S. national security interests, regardless of whether there is an agreement or the Talibs do not agree or if they decide to go an alternative route,” he said.

During his visit, Shanahan also visited a site housing an elite U.S. counterterrorism mission against the Islamic State and al-Qaeda, an effort that many officials hope could continue even after the departure of most American troops under a peace deal.

But Jason H. Campbell, an Afghanistan scholar at the Rand Corp., said securing that kind of a longer-term presence might not be possible if it is not accompanied by a larger effort to continue to fund and support Afghan forces.

“In short, the U.S. may find it difficult to convince Afghans to permit a heavily counterterrorism or even counterterrorism-only mission without making other concessions and at least agreeing to sustain funding for the Afghan security forces,” he said.

Shanahan described Khalilzad as the “quarterback” of the American effort in Afghanistan but said the Pentagon would also play an important role in the peace effort. Military officials have taken part in some of the meetings with the Taliban.

The wild card is Trump, who has long voiced a desire to leave Afghanistan but in 2017 approved a recommitment to the military effort at the urging of his advisers. The president has repeatedly surprised even his senior advisers by unexpectedly announcing major decisions on Twitter. That uncertainly creates complications for negotiators and for military officials as they seek to reassure Afghans they will not leave overnight.

Pamela Constable in Islamabad, Pakistan, contributed to this report.