President Trump said on Thursday that he would not give the American military a direct role in helping stabilize war-ravaged Libya, rejecting years of pleading by Italy for more assistance in stemming African migrant traffic into Europe.

Mr. Trump’s comments came during a White House news conference with Prime Minister Paolo Gentiloni of Italy, who implored the United States to step up its “critical” involvement in Libya, a former Italian colony.

“We need a stable and unified Libya,” Mr. Gentiloni, who has been in office since November, said, discussing a conflict that has sent thousands of asylum seekers across the Mediterranean to Italy and other European countries. “A divided country, and in conflict, would make civility worse.”

In his scripted opening remarks, Mr. Trump thanked Italy’s leaders “for your leadership on seeking stabilization in Libya, and for your crucial efforts to deny ISIS a foothold in the Mediterranean,” adding, “You fought hard.” Ansar al-Shariah, an affiliate of ISIS — the Islamic State extremist group, based in Syria and Iraq — has been operating in Libya since 2012.