Representatives of the United States, Mexico and Canada on Tuesday agreed to amend a North American trade deal, accepting significant changes demanded by House Democrats on workers’ rights, environmental protection and prescription drug prices.

The compromise all but guarantees that President Trump will achieve one of his top priorities: a replacement for the 1994 North American Free Trade Agreement that he says stripped the industrial Midwest of millions of good-paying factory jobs.

At a signing ceremony in Mexico City’s National Palace, U.S. Trade Representative Robert E. Lighthizer, Mexican Deputy Foreign Minister Jesús Seade and Canadian Deputy Prime Minister Chrystia Freeland endorsed revisions to the 2,000-plus page accord they had produced one year ago.

“This is the best trade agreement in history,” Lighthizer said. “It’s something that’s going to make North America richer. It’s going to make America richer. It’s going to make Canada richer, and it’s going to make Mexico richer.”

The revised trade deal — called the U.S.-Mexico-Canada Agreement — would create new intellectual property protections, require more North American parts to be used in automobiles to qualify for zero tariffs, open the Canadian milk market to U.S. farmers and create new rules for e-commerce. It would also boost wages, benefits and safety rules for workers and put in place updated environmental protections.

The modified deal adds tougher measures to ensure that Mexican officials implement promised labor reforms, such as recognizing the right of workers to form independent unions. The U.S. government can crack down if Mexico doesn’t comply.

With Democratic and labor backing, the pact should help the president appeal to blue-collar workers next year — a critical constituency for his reelection hopes.

“It’s a rare thing that’s a win for Trump and a win for Democrats,” said Gregory Mastel, a senior trade adviser for Kelley Drye & Warren. “There aren’t too many of those.”

On Twitter, the president celebrated progress toward rewriting the rules governing more than $1.2 trillion in merchandise trade.

“America’s great USMCA Trade Bill is looking good. It will be the best and most important trade deal ever made by the USA. Good for everybody - Farmers, Manufacturers, Energy, Unions - tremendous support,” Trump tweeted. “Importantly, we will finally end our Country’s worst Trade Deal, NAFTA!”

The House is expected to vote next week on legislation to implement the deal, which also reflects the emergence of the digital economy in the quarter-century since NAFTA took effect. But the Senate will not take up the measure until after an expected impeachment trial early next year, Senate Majority Leader Mitch McConnell (R-Ky.) said.

Reflecting Republican concern over the concessions Trump granted to win Democratic support, McConnell offered only a tepid endorsement.

“It’s not as good as I had hoped,” he told reporters.

McConnell spoke shortly after House Democrats took credit for rewriting the deal.

“There is no question that this trade agreement is much better than NAFTA. It is infinitely better than what was initially proposed by the administration,” said House Speaker Nancy Pelosi (D-Calif.). “We’re declaring victory for the American worker.”

Helping sway Democrats, the nation’s largest labor federation backed the compromise.

“We demanded a trade deal that benefits workers and fought every single day to negotiate that deal; and now we have secured an agreement that working people can proudly support,” said Richard Trumka, the president of the AFL-CIO.

Trumka called the revised accord a “vast improvement over both the original NAFTA and the flawed proposal brought forward in 2017.”

The agreement is the first to contain “enforceable labor standards,” which will include inspections of suspect manufacturing sites in Mexico, he said.

But labor is not united. The International Association of Machinists and Aerospace Workers, one of the AFL-CIO’s major affiliates, said the agreement fails to halt the flow of American jobs to Mexico.

“U.S. workers have been waiting over 25 years for a responsible trade deal that puts their interests ahead of corporations who are fleeing our shores,” said IAM President Robert Martinez Jr. “They are still waiting.”

Bringing Democrats and some of their labor allies on board marks a singular achievement for the president and, in the long run, may scramble the entrenched politics of trade. But the Democrats’ public crowing sparked Republican concern that Trump had overpaid for Pelosi’s support.

Sen. Patrick J. Toomey (R-Pa.), an early critic of the NAFTA rewrite, criticized the removal of expanded patent protections for a class of drugs called biologics as a “complete capitulation” to Democrats.

“It’s clearly moved way to the left,” Toomey said, “which is why you had a celebratory press conference by all the Democratic leadership in the House.”

Mexico’s Senate, which approved the USMCA in June, is expected to promptly ratify the amendments. Mexico is heavily dependent on trade with its northern neighbor — about 80 percent of its exports go to the United States — and the export sector has been one of the few bright spots in an economy that has flatlined this year.

The U.S. Chamber of Commerce and other business groups that have complained about fallout from Trump’s trade war backed the compromise.

“It’s good to get this thing finished and out of the way and not have uncertainty about our North American market,” said Rufus Yerxa, head of the National Foreign Trade Council, which represents companies such as Amazon, General Electric and Mastercard.

Democrats said they had secured specific changes in the text signed by Trump and the leaders of Mexico and Canada one year ago. The Democratic revisions eliminated a provision in the deal that would have granted biologics 10 years of protection from generic competitors, and added tougher environmental standards to the treaty.

“We made this agreement,” said Rep. Richard E. Neal (D-Mass.), the chairman of the House Ways and Means Committee.

Under the rewritten deal, if Mexico fails to hit specific targets in its labor reform program, U.S. enforcement actions will be triggered. New labor attaches will be dispatched to Mexico to monitor compliance.

The new enforcement mechanism also provides for “facility-based enforcement of labor obligations within a rapid timeframe,” according to a Ways and Means Committee fact sheet. That was an apparent reference to factory inspections Democrats had sought, which would block goods produced in violation of agreed labor rules from entering the United States.

Pelosi conceded that she had failed to strip from the USMCA legal protections for Internet companies that absolve them of responsibility for user content published on their websites.

The speaker said her members raised the issue after she had already promised Lighthizer she would limit her requests to four subjects: labor, environment, biologics and enforcement.

“I lost,” she said. “It’s a real gift to big tech.”

Trump spoke with Canadian Prime Minister Justin Trudeau about the “final stages” of the USMCA negotiations on Monday, according to a Canadian readout of the call.

“This negotiation has been an existential challenge for our country, and at times, an existential drama,” said Freeland, who at times wore a T-shirt emblazoned with “Keep calm and negotiate NAFTA” during the trade talks. She said that the agreement will be “profoundly beneficial” for Canadians.

Canada has said that it will ratify the trade pact “in tandem” with the United States. Trudeau, whose government was reduced to a minority after a federal election in October, will need the support of at least one other party to pass legislation. Analysts expect him to get the votes needed for ratification, but Canada’s Parliament is set to rise Friday, making passage unlikely before the new year.

Canadian officials applauded many of the changes in the new agreement, particularly the elimination of the provision that would have extended patent protections for biologics. In April, Canada’s parliamentary budget officer estimated that an extension would add at least 169 million Canadian dollars in annual costs for Canadian patients and drug plans beginning in 2029.

Amanda Coletta in Toronto contributed to this report.