Right-hander Michael Wacha and the Kansas City Royals agreed on a three-year, $51 million contract that includes a fourth-year club option and can max out at $72 million, sources told ESPN.

The Royals announced the deal Sunday but did not disclose terms.

Wacha, 33, was expected to opt out of the final year of the two-year, $32 million deal he signed with the Royals last winter but preferred to stay in Kansas City, where he thrived in front of one of baseball’s best defensive teams.

Kansas City likely would have tendered Wacha a qualifying offer, which would have dampened his free agent market, and the sides struck a deal during the five-day quiet period after the World Series during which teams can re-sign their free-agents-to-be. Wacha will nearly double his career earnings with the deal.

Wacha’s nomadic career — he has played for six teams in the past six seasons — has been on the upswing since he turned 30. He found perhaps the best version of himself last year, going 13-8 with a 3.35 ERA in 29 starts, his most since 2017. Wacha and Seth Lugo, his former San Diego Padres teammate, helped stabilize a rotation that also includes All-Star Cole Ragans and drove Kansas City’s evolution from a 56-win team in 2023 to an 86-win playoff team this year.

“I can’t say enough good things about him as a teammate. I really can’t,” Royals manager Matt Quatraro said before Wacha’s Game 1 start of the American League Division Series. “He is the most dialed-in pitcher I have ever seen when he’s not pitching. He’s into every pitch. He’s high-fiving guys for keeping the double play in order, for breaking up a double play, for throwing to the right base. He’s into every at-bat when his teammates are up there. He’s looking for things with the other team when he’s on the bench.

“He is the consummate professional.”

Over 166⅔ innings in 2024, Wacha struck out 145, walked 45 and allowed 17 home runs. Armed with one of the best changeups in baseball, Wacha has the 18th-ranked ERA (3.30) among the 80 starting pitchers with at least 300 innings since 2022.

The deal will pay Wacha $18 million in 2025, $18 million in 2026 and $14 million in 2027, with an additional $4 million available in easily reachable performance bonuses, sources said. The club option is for $14 million and includes the same $4 million in incentives and a $1 million buyout.

Kansas City is hoping to fortify its rotation as well as add impact bats this winter. The Royals’ offense struggled in six postseason games — two in a wild-card-round win against Baltimore and four in a division-series loss against New York — hitting .231/.287/.291.

Wacha started Games 1 and 4 against the Yankees, going 4⅔ innings and allowing two runs in the first game and allowing three runs over four innings in his second start.