Mr. Trump returns to a deteriorating domestic situation, facing months of investigations into whether people in campaign colluded with Russia and what appears to be the decreasing likelihood that a deeply divided Senate majority can quickly pass a repeal of the Affordable Care Act.

But he’ll always have Warsaw. The apex of the trip from the president’s perspective — and one of the most personally satisfying episodes of his term so far — was Mr. Trump’s powerful invocation of Western exceptionalism to a crowd of like-minded Polish nationalists on Thursday.

The speech was a blunt expression of Western defiance in the face of radical Islamic terrorism. It cast Mr. Trump as a modern-day inheritor of that struggle, a willing warrior in a clash of civilizations.

“The fundamental question of our time is whether the West has the will to survive,” said Mr. Trump, dwarfed by a huge monument to ragtag Polish partisan army soldiers wearing helmets taken from dead German soldiers. “Do we have the confidence in our values to defend them at any cost? Do we have enough respect for our citizens to protect our borders? Do we have the desire and the courage to preserve our civilization in the face of those who would subvert and destroy it?”

Mr. Trump labored for hours on the wording in the address — widely praised as one of his most ideologically coherent by the conservative press — on the flight over with his chief speechwriter, Stephen Miller, and his national security adviser, Lt. Gen. H. R. McMaster.

General McMaster dealt with the fine print of security issues, and helped draft language challenging Russia to stop its “destabilizing” behavior in Ukraine and elsewhere. Mr. Miller, an architect of the president’s controversial travel ban, saw Poland, with its large population of religious conservatives and center-right government, as an extension of Mr. Trump’s base back home. The speech had the sledgehammer directness of a Trump campaign address.