For a time, President Trump was reluctant to select William P. Barr as his attorney general. The veteran Justice Department official from the George H.W. Bush administration was not a longtime Trump loyalist, and the president wondered whether one of his own political allies might serve better as a shield, people familiar with the matter said.

But Trump was ultimately persuaded — in part because his lawyers and advisers told him Barr was a strong supporter of presidential power and unafraid of taking on critics. This week, the president has been thrilled with his choice, particularly after Barr sparred so vigorously with Democrats on the Senate Judiciary Committee on Wednesday that some were left wondering whether he viewed himself as the president’s defense attorney, according to people familiar with the matter, who like others spoke on the condition of anonymity to discuss internal deliberations.

In Barr’s first three months in the job, his actions have served to protect Trump, though his motive is up for debate. Barr’s defenders note that the attorney general has long advocated strengthening the power of the executive branch, and the attorney general has told other lawyers that he is more interested in protecting the presidency than the man in the job.

But critics say that Barr has emerged as the partisan champion Trump always wanted — one willing to defend the president’s most questionable conduct, put a Trumpian spin on the results of special counsel Robert S. Mueller III’s investigation and mislead Congress along the way.

“He has failed the men and women of the Department of Justice by placing the needs of the president over the fair administration of justice,” Rep. Jerrold Nadler (D-N.Y.) said at a congressional hearing Thursday that Barr skipped after a dispute over the terms of his appearance.

Barr’s defenders say he is unbothered by the criticism.

“He is not at all surprised at the partisan reception he received,” said Richard Cullen, an attorney and close friend of Barr’s who spoke with Barr after his Senate testimony. “He is going to follow what he believes is the law, politics be damned.” Cullen represents Vice President Pence.

Political fires have raged around Barr, who was also attorney general in the George H.W. Bush administration, from even before his confirmation along largely partisan lines. But he has come under sustained attack this week after The Washington Post reported that Mueller had sent a memo to Barr complaining about how the attorney general had described the results of the special counsel investigation in a letter to Congress.

In his letter to Congress after Mueller ended his inquiry, Barr wrote that Mueller had not found that Trump or his associates coordinated with Russia to influence the election, and that the special counsel would not reach a decision on whether the president obstructed justice. Mueller countered in his own letter that the attorney general “did not fully capture the context, nature, and substance” of the special counsel’s work.

The revelation of Mueller’s March letter — which Barr derided as “a bit snitty” — sparked calls from some Democrats for Barr to resign. They also accused the attorney general of lying to Congress for not revealing Mueller’s concerns during a congressional hearing in April. Barr was asked during that hearing whether he knew to what media reports were referring when they revealed frustration among some on Mueller’s team about the limited information Barr had revealed to Congress about their work.

“He lied to Congress,” House Speaker Nancy Pelosi (D-Calif.) said during a news conference Thursday. “The attorney general of the United States of America was not telling the truth to the Congress of the United States. That’s a crime.”

Kerri Kupec, a Justice Department spokeswoman, said Pelosi’s comment was “reckless, irresponsible, and false.”

Trump, who spent a few hours at most with Barr before picking him as attorney general, told people that he was pleased with Barr’s pugnacious performance before the Senate Judiciary Committee on Wednesday, though one Trump ally who regularly speaks with the president said his joy was “shortsighted” because Barr lost credibility with some lawmakers who could be useful later.

On Wednesday night, Trump tweeted a link to a Wall Street Journal editorial lauding Barr’s testimony and bearing the headline, “A Real Attorney General” — a phrase which, to Trump, means “someone who is defending him at all costs,” one former White House official said. Trump and senior White House officials have urged surrogates to go on TV and praise Barr — and even arranged a call Wednesday afternoon to disseminate supportive talking points.

People familiar with the matter said that in the call, deputy White House press secretary Steven Groves told them to say of Barr that “nobody really laid a glove on him” and that Barr had “dismembered arguments about obstruction of justice.”

Before he was nominated, Barr wrote a detailed memo — which he sent to the Justice Department, as well as to lawyers working for the White House and Trump — blasting Mueller’s apparent theory of how Trump could have interfered with justice as “fatally misconceived.” In Barr’s view, the president should not be investigated for using his powers to affect an investigation — meaning that the firing of James B. Comey as FBI director would be out of reach for federal prosecutors.

Democrats alleged that the memo was essentially a tip at the time about how he might curtail Mueller’s not-yet-completed probe. On Wednesday, Barr held to his view, repeatedly insisting that Trump’s asking his White House counsel to have Mueller removed was not a prosecutable obstruction case because Trump had a legal right to end Mueller’s investigation.

Mueller declined to conclude whether his team believed there was a prosecutable case against Trump for obstructing justice. His report, which laid out detailed accounts of possible obstructive conduct he investigated, asserted that he could not say even privately whether the president committed a crime because of Justice Department guidance that says a sitting president cannot be indicted. Barr and Deputy Attorney General Rod J. Rosenstein reviewed the case themselves and determined that a charge could not be brought.

A White House official said that Barr had “set the narrative” in a way that was positive for the White House and that the swirling debate about the special counsel has just been “noise.”

David Rivkin, a conservative lawyer who has worked with Barr, said he believed Barr was more interested in defending the executive branch of government than the president himself. Barr was no early Trump supporter; he was a major political contributor to Jeb Bush, one of the president’s primary opponents.

“His behavior is sufficiently explained by his well-known and publicly articulated views to constitutional principles,” Rivkin said.

Barr also turned the tables on Mueller — expressing confusion about how the special counsel came to some decisions and confirming that he would examine, as many Republicans want, the origins of the FBI probe that the special counsel eventually took over.

Trump campaign advisers, who said they had raised more than $1 million after the Mueller report was released, said the attorney general’s comments have generally been helpful — especially his earlier assertion that there was “spying” during the 2016 presidential race.

At the same time, Barr sought to extricate the Justice Department from the political battles of Mueller’s investigation, declaring at one point, “We are out of it.” The comment resonated with some Barr supporters who are eager to see more stability in the Justice Department.

“It seems to me that the attorney general’s handling of the report and the issues it raised now has a punctuation point, and there are many other important matters to the American people on his plate that merit attention,” said George Terwilliger, a former deputy attorney general and a friend of Barr’s.

But more battles lie ahead. Last month, White House lawyer Emmet Flood sent Barr a letter declaring that Trump’s decision not to invoke executive privilege to block any portions of the Mueller report should not be over-interpreted. The president, Flood wrote, might still seek to curtail his advisers’ cooperation with Congress over the report or prevent the release of underlying investigative materials.

That posture will almost certainly spark a battle between Congress and the White House — with Barr at the center of the fight.

Alice Crites contributed to this report.