White House counsel Donald McGahn at a Senate Judiciary Committee hearing on Sept. 27 where President Trump’s Supreme Court nominee Brett M. Kavanaugh testified. (Melina Mara/The Washington Post)

White House counsel Donald McGahn grumbled to his friends in late August that his expected exit this year from the West Wing was announced by President Trump on Twitter — without even a warning.

“Don was surprised and a little annoyed, but eventually he shrugged it off,” said a person close to McGahn who was not authorized to speak publicly. “It seemed like the last little bump in the road.”

But there was a car wreck ahead.

McGahn’s final bit of business before departing was to get Brett M. Kavanaugh seated on the Supreme Court. It was a prize — moving the court further to the right, potentially for decades — that made his final months in the White House worth their trouble.

Now, Kavanaugh’s nomination is marred over sexual assault allegations made by Christine Blasey Ford and others — and McGahn is desperately working to save it from collapse as the Senate prepares for a vote this weekend.

McGahn — 50, low-key and known for his dark humor — has been in bunker mode, working nearly nonstop to protect the nomination by serving as both lawyer and crisis communications adviser to Trump and Kavanaugh, often being the rare person who will speak bluntly to both men.

“Don believed in the potential of Trump’s candidacy early and signed on early, so he has a lot of leeway. The president doesn’t forget that — and Don’s a survivor,” former Trump adviser Sam Nunberg said.

Tempers have flared in the fallout over the allegations against Kavanaugh, both inside the administration and on Capitol Hill, where McGahn’s tight grip over the FBI’s investigation of Ford’s claims has sparked sharp questions from both parties about the inquiry’s scope and the extent to which the White House is limiting the probe.

Kavanaugh’s chances improved on Thursday after the FBI report on the allegations against the judge was shared with lawmakers, although a handful of Republicans said they remained undecided and would continue to review the documents.

Trump, who has had several shouting matches with McGahn over the ongoing Russia investigation during their time together in the White House, has fumed to associates as Kavanaugh has struggled, saying that McGahn pushed the nominee on him and that he barely even knows the federal judge, according to a White House official and three Republicans involved in the discussions.

Those frustrations about Kavanaugh’s bid — which Trump at times privately calls McGahn’s pet project and a too-kind overture to the GOP establishment that has been his foe and foil — have begun to spill out.

“I don’t even know him. I met him for the first time a few weeks ago. So it’s not like, ‘Oh, gee, I want to protect my friend,’ ” Trump said of Kavanaugh this week at a rally in Mississippi.

Kavanaugh, meanwhile, remains reliant on McGahn to guide him through the thicket of mounting allegations and trusts him as a fellow traveler of Washington’s elite conservative legal circles. Before joining the White House, McGahn was a partner at Patton Boggs and later at Jones Day.

But Kavanaugh, too, has had tense moments with McGahn. Kavanaugh’s allies have wondered behind the scenes whether McGahn’s confidence was shared throughout the rest of the White House, the three Republicans said, adding that talks over the judge’s media strategy have been strained.

So were prep sessions for Kavanaugh’s Senate hearing last week. Kavanaugh grew unhappy with the personal questions being thrown his way as McGahn and others looked on, a White House official said.

One Trump ally, who was not authorized to speak publicly, said McGahn is the person who has had to “bring Kavanaugh back to reality” and remind him of the brutal political and media challenges facing him, at once urging him to fight on but also “to not expect anything but problems.”

Friends say McGahn is driven by his desire to oversee the confirmation of a second Supreme Court justice before he leaves, as well as his own partisan anger toward the Democrats for their tactics. The person close to McGahn said the White House counsel was instrumental in urging Kavanaugh to be combative and raw during his opening statement before the Senate Judiciary Committee last week.

The bond between the two men, the friends add, is more than transactional or political. McGahn is Kavanaugh’s generational peer and shares his Irish roots, Catholic faith and conservative ideology. Both of their résumés include stints in George W. Bush’s administration. McGahn chaired the Federal Election Commission and Kavanaugh was Bush’s staff secretary.

McGahn, who has declined interview requests throughout the confirmation process and largely stays in his wing of the White House, has been the dominant force in directing the FBI’s further investigation of the allegations against Kavanaugh, serving as the liaison to the Justice Department and the “explainer in chief to senators,” as one Senate Republican adviser described McGahn’s role, making and taking calls.

Sen. Jeff Flake (R-Ariz.), a critical undecided vote, told CNN on Wednesday that McGahn has been his point of contact ever since calling for a delay on Kavanaugh’s confirmation vote until the FBI completed its updated background check.

Sen. Christopher A. Coons (D-Del.), who helped broker a deal with Republicans to secure the limited FBI investigation, spoke by phone with McGahn on Sunday, pressing him on the breadth of the probe.

At Kavanaugh’s hearing last week, McGahn’s name was front and center as millions watched on television, as Sen. Richard J. Durbin (D-Ill.) asked Kavanaugh to “turn to your left in the front row to Don McGahn” and “tell him it’s time to get this done — an FBI investigation is the only way to answer some of these questions.”

Senate Judiciary Committee Chairman Charles E. Grassley (R-Iowa) jumped in and said, “This committee is running this hearing, not the White House, not Don McGahn.”

The Kavanaugh episode is one of many crises in which McGahn has been thrust into the middle. Like other current and former Trump aides, McGahn has met on multiple occasions with special counsel Robert S. Mueller III’s team for dozens of hours. As McGahn has cooperated and waved Trump away from dramatic showdowns with the Justice Department, his relationship with Trump has eroded and they sometimes have gone weeks without speaking, according to two White House officials.

And earlier in the year, White House Chief of Staff John F. Kelly and McGahn faced scrutiny over how they handled allegations of spousal abuse against Rob Porter, who resigned as staff secretary on Feb. 7.

More broadly, Kavanaugh’s trials have prompted a reckoning for the conservative establishment in Washington that has deep networks on Capitol Hill and in the judiciary, and has become empowered with McGahn at Trump’s ear.

More than 60 of Trump’s nominees for federal courts have been confirmed. Many of them were drawn from the community of lawyers who know McGahn or Senate Majority Leader Mitch McConnell (R-Ky.) — or are on the radar of the Federalist Society, a conservative legal group run by Leonard Leo, an informal Trump adviser.

“I don’t think we’ve ever seen the equivalence of the Federalist Society in any administration prior, either Republican or Democratic,” said Walter E. Dellinger, a former acting solicitor general for President Bill Clinton. “You have the White House, Federalist Society and Senate leadership working together in an unprecedented way. . . . The president effectively outsourced his picks for the court.”

Justice Neil M. Gorsuch’s relatively smooth confirmation to the Supreme Court last year was celebrated by conservatives — nationally and in Washington, in particular — as an indicator of their influence. Gorsuch is a graduate of Georgetown Preparatory School in Maryland and is the son of Anne Gorsuch Burford, who served in the Reagan administration as the first female administrator of the Environmental Protection Agency.

But Kavanaugh’s nomination has been seen in more personal terms by the conservative legal set. He is a longtime resident of suburban Maryland and also a Georgetown Prep alumnus, as well as part of the tightknit group of former senior aides to George W. Bush, who has made calls to senators to boost the nominee.

The tumult around Kavanaugh has also frayed nerves to the point that conspiracy theories are being promoted. Prominent conservative lawyer Edward Whelan, president of the Ethics and Public Policy Center and Kavanaugh’s friend, took a leave of absence last month after casting doubt on Ford’s allegations with widely criticized tweets that insinuated that another person could be responsible for the sexual assault she alleged Kavanaugh committed while the two were in high school.

“This isn’t just a McGahn thing. It’s about the D.C. network of conservative lawyers, all of the former clerks and friends who were more or less lobbying for Kavanaugh,” said Ramesh Ponnuru, a fellow at the American Enterprise Institute, a conservative research group. “That’s what has set Kavanaugh apart, unlike the others who were in the mix for the court and couldn’t compete with that, like Amy Coney Barrett,” a lesser-known federal judge in Indiana who was on Trump’s shortlist.

Ponnuru said that conservative legal network, even if rattled, is likely to remain a force in Trump’s Washington.

“It could strengthen the emotional pull for the president with them, if Kavanaugh gets on the court and is a solid conservative jurist as people have been saying he would be,” Ponnuru said. “He’d see the attacks as unfair, not as a reason to lose confidence in [McGahn] or the Federalist Society.”