Senate Majority Leader Mitch McConnell could barely hide his glee Wednesday moments after Justice Anthony M. Kennedy announced his retirement, giving Republicans a chance to replace the high court’s swing vote with a solid conservative for decades.

It wasn’t just a capstone for the six-term Kentucky Republican, who has spent the past year and a half muscling 42 of President Trump’s judicial nominees through the Senate, including one Supreme Court justice and 21 appeals court judges.

It was a political gift for the GOP leader determined to hold on to his fragile Senate majority in November’s midterm elections. A Supreme Court vacancy is a much-welcomed jolt as Republicans seek to galvanize core voters in states crucial to their chances — those in which Trump won by double digits and a Democratic senator faces reelection.

“One thing that energizes conservatives more than anything else is the Supreme Court,” said Sen. John Cornyn (R-Tex.), McConnell’s top deputy.

So McConnell stood on the Senate floor, with a Cheshire cat grin, knowing the vacancy would give him a chance to cement his legacy of packing the courts that has enraged Democrats and thrilled Republicans.

“We will vote to confirm Justice Kennedy’s successor this fall,” McConnell said, adding a warning to Democrats: “It is imperative that the president’s nominee be considered fairly and not be subjected to personal attacks.”

While there is little the opposition party can do to stop McConnell from pushing the already conservative court farther right for generations, McConnell’s moment is not without peril. With a scant 51-49 Republican majority and a hot-and-cold relationship with a mercurial president, the path to confirming a new justice is far from guaranteed to be smooth.

McConnell changed the Senate rules last year to ensure confirmation of a Supreme Court nominee with a simple majority, disposing of the filibuster. That cleared the way for Neil M. Gorsuch to fill a high court vacancy that had remained open for more than a year — thanks to McConnell.

After the death of Justice Antonin Scalia in February 2016, McConnell refused to fill the seat, blocking President Barack Obama’s nomination of Merrick Garland and denying him a hearing.

McConnell insisted that the seat remain vacant until voters had a say in the presidential election, infuriating Democrats.

His gambit paid off when Trump won and Republicans held the Senate.

“I felt, personally, very invested in this issue,” McConnell recalled in an interview with The Washington Post last year. He called the evening Trump introduced Gorsuch as his nominee “one of the happiest nights of my Senate career.”

On Wednesday, in an interview with Politico hours before Kennedy’s announcement, McConnell, 76, said keeping the seat vacant was the “most consequential decision I’ve ever made.”

Electoral considerations could also apply pressure on some centrist Democrats to vote for whomever Trump decides to nominate. Last year, three Democrats broke ranks to vote for Gorsuch — Sens. Heidi Heitkamp (N.D.), Joe Donnelly (Ind.) and Joe Manchin III (W.Va.). All three face reelection in states Trump won big.

“I look forward to meeting with and evaluating the qualifications of whoever President Trump nominates to become a justice on the Supreme Court,” Manchin said in a statement.

“Part of my job as a United States senator is to carefully consider the president’s judicial nominees, including for the Supreme Court, and I will thoroughly review the record and qualifications of any nominee presented to the Senate,” Donnelly said.

McConnell could well need to find crossover votes. His 51-49 majority is effectively 50-49, as Sen. John McCain (R-Ariz.) undergoes treatment for a severe form of brain cancer. Further complicating McConnell’s task: Some Republican senators have gone rogue in recent months, leveraging their clout at a time when even a single vote could prove decisive.

Senate Democratic leaders said Wednesday that McConnell ought to punt the confirmation beyond November, and they signaled that they will wage an aggressive campaign opposing any nominee who threatens to undermine abortion rights and health-care protections for Americans.

“This is the most important Supreme Court vacancy for this country in at least a generation,” said Senate Minority Leader Charles E. Schumer (D-N.Y.) “Nothing less than the fate of our health-care system, reproductive rights for women and countless other protections for middle-class Americans are at stake.”

Schumer also suggested following the McConnell model and waiting until after voters have had their say in the midterm elections.

McConnell dismissed that notion. “There’s no presidential election this year,” he told reporters.

McConnell’s inability last year to fulfill a longtime Republican promise of undoing Obama’s signature health-care law strained his relationship with Trump and turned him into a momentary villain in his own party.

As he has sought to rehabilitate his image, he has emphasized the judicial confirmations the Senate has cleared under his watch. While gridlock has seized this Congress, and Republicans have struggled to reach consensus on immigration and other major issues, McConnell has offered allies a simple and consistent reminder: The Senate is in the personnel business.

It’s also meant to be a political baton for Republican Senate candidates to pick up and run with, by reminding their base about the importance of a GOP Senate.

“I’m happy to see a Supreme Court right of center, as it was before Justice Scalia passed away,” McConnell said on Tuesday.

Not long after the Supreme Court issued a 5-to-4 decision upholding Trump’s travel ban that day, McConnell’s campaign tweeted a photograph with no caption. It was McConnell and Gorsuch, both smiling and about to shake hands.

Paul Kane and Seung Min Kim contributed to this report.