WASHINGTON — Mitch McConnell, the Senate majority leader, went to bed Wednesday night cautiously optimistic that a shutdown crisis that had stretched back to December had finally ended.

Then President Trump awoke in a rage Thursday, feeling cornered into accepting a bipartisan funding deal struck earlier in the week that would deprive him once again of money for his long-promised wall along the southwestern border. Conservative commentators who had been cajoled into accepting the deal Wednesday were breaking their silence on Thursday.

By midmorning, after a particularly unpleasant meeting with the secretary of homeland security, Kirstjen Nielsen, the president was threatening to torpedo the deal, according to two people briefed on the exchange. Several hours and several phone calls later, Mr. McConnell had persuaded Mr. Trump to once again agree to sign the bill to avert another government shutdown looming at midnight Friday.

But persuasion came at a price: The president would declare a national emergency to try to secure wall funding without congressional approval, he told the majority leader — and Mr. McConnell would have to back him.