White House counsel Donald McGahn, who has led the Trump administration’s efforts to reshape the judiciary while confronting turmoil in the West Wing surrounding the ongoing special counsel probe, will leave his post in the coming weeks, President Trump announced Wednesday.

The exit of McGahn — a low-key, 50-year-old lawyer who has been at Trump’s side since the early days of the 2016 presidential campaign — comes at a fragile moment inside the White House amid escalating tensions between the president and the Department of Justice in recent weeks.

McGahn, who has deep ties in Washington’s legal community, has long functioned as Trump’s liaison to Justice officials and Congress. He has frequently played the informal role of peacemaker, as special counsel Robert S. Mueller III and his team have pursued interviews, including with McGahn, and documents from White House officials as part of their investigation into Russian interference in the 2016 presidential campaign.

“White House Counsel Don McGahn will be leaving his position in the fall, shortly after the confirmation (hopefully) of Judge Brett Kavanaugh to the United States Supreme Court,” Trump wrote on Twitter. “I have worked with Don for a long time and truly appreciate his service!”

Trump’s announcement of McGahn’s departure came as a surprise, including to McGahn.

He was not aware that Trump planned to send the tweet, according to a person close to McGahn who was not authorized to speak publicly.

“He was surprised,” this person said. While it had been an open secret inside the White House that McGahn planned to leave after Kavanaugh’s confirmation process was concluded, he had not discussed his plans directly with Trump, according to this person.

McGahn, who has told many friends that he has wearily endured countless political and legal battles, saw Trump’s tweet as abrupt but typical of how the president acts — and it did not make him angry, according to two people familiar with his reaction. His reaction was, “Of course it happened this way,” one person said.

Trump was in part prompted to tweet on Wednesday morning after reading a report in Axios, a news publication, about McGahn’s likely departure, according to two Trump advisers who were not authorized to speak publicly. Trump read the story and quickly decided to act, giving a formal nod to what had been an expected move inside the White House, the advisers said.

The announcement also came as Trump’s family members and senior advisers — daughter Ivanka Trump and son-in-law Jared Kushner — have seen their relationship with McGahn fray over time, Trump associates said. One Trump aide described McGahn as someone who “had to tell them ‘no’ on some issues” and never clicked with them personally. Kushner and Ivanka Trump, along with the president, have regularly chafed at McGahn’s counsel and caution, the associates said.

Many Republicans on Capitol Hill who see McGahn as a stable force and accessible official were stunned and dismayed by Trump’s announcement.

Senate Judiciary Committee Chairman Charles E. Grassley (R-Iowa) tweeted on Wednesday that he hopes “it’s not true” that McGahn is leaving and urged Trump to not “let that happen.” Senate Majority Leader Mitch McConnell (R-Ky.) called it “sad news for our country.”

McGahn’s role in seeking to transform the judiciary — he helped select Trump’s two Supreme Court nominees and dozens of picks for lower federal courts — made him a favorite of conservatives who view the endeavor as the most successful project of Trump’s presidency.

“Don is the most impressive White House Counsel during my time in Washington, and I’ve known them all,” McConnell, who has worked closely with McGahn on confirming conservative judges, said in a statement. “Don’s significance to the judiciary, the White House and the nation cannot be overstated, and I look forward to his continued efforts on behalf of our country.”

White House attorney Emmet T. Flood, a veteran Washington lawyer whom McGahn recruited to join the West Wing in May, is seen by most Trump aides as a likely replacement because of his rapport with the president and his knowledge of impeachment proceedings. Flood represented Bill Clinton and has begun to prepare for a potential Democratic move against Trump, should that party win control of the House this fall.

McGahn’s impending departure is widely seen within Trump’s circle as the culmination of frustrations that have come with each new crisis that he has been forced to handle since Trump took office, from a scandal over then-national security adviser Michael Flynn’s disclosure of contacts with Russian officials to the many times the president has lashed out about Mueller’s probe.

Still, McGahn has often expressed a sense of accomplishment to allies despite the controversies and turbulence. As evidence of conservative success, he has pointed in particular to his efforts last year on the nomination of Supreme Court Justice Neil M. Gorsuch and on other judicial nominees later approved by the Republican-controlled Senate, as well as his work to curb federal regulations.

But McGahn’s tenure has nonetheless been dominated by pressure-filled flash points that have obligated him to respond to the president’s demands and deal with Trump’s anger over an investigation the president has called a “witch hunt.”

Since April, former New York mayor Rudolph W. Giuliani has been serving as the president’s lead outside attorney with a focus on the Russia investigation and has been combative and public in his efforts as McGahn has stayed out of sight.

McGahn, especially during the beginning of Trump’s term, cautioned the president about contacting Justice Department officials and even told associates he was concerned that Trump was doing so without his knowledge. The two men would have “spectacular” fights, according to a person who witnessed some of them.

When Trump sought the firing of Mueller in June 2017, McGahn threatened to resign over the proposed move, according to two people familiar with the exchange. McGahn’s warning to the president was first reported by the New York Times and denied by Trump, who called it “fake news.”

Charlie Dent, then a GOP congressman from Pennsylvania, said in an interview at the time that McGahn “prevented an Archibald Cox moment,” referring to the special prosecutor whom President Richard M. Nixon ordered fired during the Watergate investigation.

In an earlier showdown, in March 2017, McGahn urged Attorney General Jeff Sessions to not recuse himself from the Russia probe. And the next month, Trump asked McGahn to persuade then-FBI Director James B. Comey to announce that Trump was not personally under investigation, but Comey rejected McGahn’s entreaties.

In recent months, as he has worked on the Kavanaugh confirmation, McGahn’s challenges have only mounted.

The president has repeatedly belittled Sessions, and in the spring, the Justice Department chief told the White House that he might have to leave his job if Trump fired Deputy Attorney General Rod J. Rosenstein, who oversees the Russia investigation, according to people familiar with the exchange.

Sessions made his position known in a phone call with McGahn, as Trump’s fury at Rosenstein peaked after the deputy attorney general approved the FBI’s April 9 raid on the president’s personal attorney, Michael Cohen.

Like other current and former Trump aides, McGahn has met on multiple occasions with Mueller’s team, for dozens of hours. Throughout that process, McGahn has had tense clashes with Trump’s outside attorneys over the Russia inquiry — as well as with then-White House lawyer Ty Cobb, who coordinated the White House’s interaction with Mueller’s team — questioning their cooperative strategy in the case.

On Wednesday, Trump told reporters he isn’t worried about anything McGahn might have told Mueller’s investigators.

“We do everything straight; we do everything by the book,” Trump said.

As the White House Counsel’s Office prepares for a revamp, Trump’s outside legal team for the Russia probe is being led by Giuliani, lawyer Jay Sekulow and two former federal prosecutors. John Dowd, a veteran lawyer, resigned in March as Trump’s lead attorney for the Russia investigation.

Led by Giuliani, Trump’s latest legal team is taking a more antagonistic approach to the Russia investigation.

McGahn’s struggles in the West Wing have not solely been related to Trump’s conduct, but also to his own.

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.

Kelly, in a five-page memo directed to McGahn and others, later ordered changes to how security clearances are granted as part of the White House’s response to continued questions about Porter, who had access to highly classified material months after the claims by his two ex-wives were reported to the FBI.

Within an administration that has had a strained relationship with the GOP, McGahn has been known on Capitol Hill and elsewhere as a totem of conservatism — veering each week from managing political and legal eruptions around Trump to private confabs with conservative allies and attorneys to talk through policy and nominees.

McGahn advised Trump to pick Gorsuch, at the time a federal appeals court judge in Colorado, to fill the Supreme Court opening that the new president inherited and oversaw Gorsuch’s confirmation. Trump came close to breaking the record for a new president having a nominee on the court so close to his inauguration.

McGahn also played a big role in helping fill numerous openings in lower federal courts. Trump has seen 60 of his nominees already confirmed, outdistancing the totals for other presidents at similar points in their tenures. Twenty-six of Trump’s confirmations were for nominees to the influential regional courts of appeals, far more than President Barack Obama was able to get confirmed early in his term.

Trump’s nominees have been overwhelmingly white and male, and critics claim they are the least diverse collection of nominees since President Ronald Reagan. But they fit the mold favored by the conservative legal establishment: relatively young and with a strong track record of judging or other legal experience that provides comfort for predicting their futures.

McGahn has worked with the conservative Federalist Society and the Heritage Foundation to select the nominees. He told the Federalist Society last fall that the Trump administration would not waste the opportunity to stock the federal judiciary with judges who take an “originalist” approach to constitutional interpretation that closely follows the text of statutes and looks at the context of laws at the time when they were written.

“This administration’s mandate on judicial selections is crystal clear: Choose judges in the mold of Justice [Antonin] Scalia, Justice [Clarence] Thomas and now Justice Gorsuch,” McGahn said.

Long before Trump was seen as a viable presidential contender, McGahn, a former commissioner of the Federal Election Commission, became the Trump campaign’s attorney. McGahn has told friends that he admires Trump as a Republican outsider and understands his appeal. McGahn’s uncle, Atlantic City lawyer Patrick McGahn, had represented Trump for years.

Before joining the White House, McGahn was a partner at Patton Boggs and later at Jones Day and was counsel to the National Republican Congressional Committee. President George W. Bush nominated him to serve on the FEC.

McGahn’s personal views — he is known to lean hard to the right — aligned him during the administration’s first few months with then-Chief of Staff Reince Priebus and then-chief strategist Stephen K. Bannon, and he worked with them on projects to overhaul federal regulations and nominate conservatives to the federal bench.

When Priebus and Bannon left the White House, McGahn became close with Kelly, who has relied on McGahn for advice in trying to bring order to a chaotic environment.

Josh Dawsey and Matt Zapotosky contributed to this report.