Mr. Trump’s mood had begun to sour even before the raids on his lawyer. People close to the White House said that over the weekend, the president engaged in few activities other than dinner at the Trump International Hotel. He tuned into Fox News, they said, watched reports about the so-called deep state looking to sink his presidency and became unglued.

Mr. Trump angrily told his advisers that people were trying to undermine him and that he wanted to get rid of three top Justice Department officials — Jeff Sessions, the attorney general; Rod J. Rosenstein, the deputy attorney general who appointed Mr. Mueller; and Christopher A. Wray, the F.B.I. director — according to two people familiar with what took place.

He eventually calmed down and the anger abated. But it was stoked anew on Monday, after the F.B.I. raids on Mr. Cohen. Mr. Rosenstein in particular was a source of Mr. Trump’s anger on Monday, and some aides believed the president was seriously considering firing him, to a degree he has not in the past.

Mr. Trump’s foul mood continued into Tuesday as he watched more coverage of Mr. Cohen’s problems. Mr. Trump told allies and advisers that Mr. Mueller had gone too far and that his actions would backfire, according to a person with knowledge of the president’s thinking.

The president indicated to some advisers that he had been proved right that Mr. Mueller was out to get him, after nearly a year of hearing from some of his lawyers that he should cooperate with the investigation and turn over everything that the special counsel requested. His participation in an interview with Mr. Mueller, which the special counsel has sought, seemed less likely, three people close to the president said.

Mr. Trump has long felt as if he has been unfairly hit by the Mueller investigation, and he has wanted to hit back. But there were few people on cable TV defending the president on Tuesday.

White House advisers were particularly alarmed by the president’s tirade in front of reporters on Monday, when he called the raids on Mr. Cohen “an attack on our country” in far angrier terms than he has ever referred to the Russian assault on the 2016 election.