President Ghani recently appointed Hamdullah Mohib, a computer expert who had been Afghanistan’s ambassador in Washington but has little security experience, to be his new national security adviser, a cabinet position.

Mr. Mattis’s visit was his first to Afghanistan since March. He has been instrumental in persuading President Trump to resist calls by Republican Party populists to pull out of Afghanistan and instead give commanders the latitude to increase their troop levels here, which they have done on a modest scale.

There are now 14,000 American troops in Afghanistan, about 4,000 more than last year. During the peak of the American involvement during the Obama administration, there were 140,000 coalition troops there.

The defense secretary, in his remarks Wednesday, said the insurgents’ recent attacks were “not militarily significant other than the tragedy of killing innocent women and children.” He said there was increasing interest in reconciliation. “The State Department has put additional staff into the embassy with that sole effort. You’re seeing this now pick up traction,” Mr. Mattis said.

The insurgents last month overran the strategic city of Ghazni for six days, killing hundreds of Afghan soldiers and policemen in attacks throughout the country.

While the Taliban insurgents have said they will no longer deliberately target civilians, Islamic State extremists in the country have continued to do so. They have carried out series of deadly attacks in Kabul, most recently on Wednesday when a double bombing killed 25 people, including two journalists.