SAN FRANCISCO - When Jake Peavy joined the San Francisco Giants midseason, he brought an energy to the mound that added a new dimension to their rotation.

Now, the Giants are keeping a key member of their World Series championship team, bringing back Peavy with hopes of another October run.

The right-hander agreed to a $24 million, two-year contract to stay with San Francisco, two people familiar with the negotiations said Friday. They spoke on condition of anonymity to The Associated Press because the deal had not yet been announced.

Also on Friday night, the Giants completed a trade with the Miami Marlins to bring third baseman Casey McGehee to San Francisco. He will replace Pablo Sandoval, who signed last month with the Boston Red Sox.

The 32-year-old McGehee won NL comeback player of the year honours after hitting .287 with 29 doubles, four home runs and 76 RBIs for Miami following a season with the Rakuten Golden Eagles of Japan's Pacific League.

The Giants sent minor league right-handers Kendry Flores and Luis Castillo to Miami.

The Marlins acquired Martin Prado on Friday from the Yankees to become their likely third baseman, so they quickly struck a deal with the Giants. McGehee is from Santa Cruz and attended Fresno State.

Peavy and right-hander Sergio Romo were undergoing physicals Friday in Arizona, and San Francisco wasn't expected to make a formal announcement until early next week. Romo is set to receive a $15 million, two-year contract.

A three-time All-Star, Peavy provided a key lift and enthusiasm — not to mention fierce intensity — on the mound down the stretch as the wild-card Giants overcame their summer struggles.

Giants ace Madison Bumgarner and Tim Hudson benefited from Peavy's veteran presence. The three Southerners had a unique bond in their corner of the clubhouse.

"It is important, really a been-there-done-that kind of guy," pitching coach Dave Righetti said Friday of Peavy. "Another Southern guy, just an outgoing personality. It always lifts somebody's spirits, during long seasons you need a little jolt of electricity here and there, especially baseball. Peavy brought that. The best part was he won when he got here. He was really getting after it. He was tired of losing to be quite honest and he felt he should be winning. He was an aggressive personality, which I think Bum likes."

Peavy will receive a $4 million signing bonus, $7 million next season and $13 million in 2016. The deal includes a full no-trade provision and award bonuses.

A 33-year-old right-hander, Peavy helped Boston win the 2013 World Series and was traded to San Francisco on July 26.

After beginning 1-9 with Boston, Peavy went 6-4 with a 2.17 ERA in 12 starts for San Francisco. He went 1-2 in four post-season outings as the Giants won their third title in five years.

Last week, San Francisco lost out in bidding for new Cubs left-hander Jon Lester after Sandoval left for the Red Sox, so bringing back Peavy is a key move by general manager Brian Sabean.

Righetti enjoyed seeing how Peavy, Bumgarner and Hudson immediately clicked and fed off each other's successes.

"You see that very rarely. In a sense this wasn't made at all, it kind of grew," Righetti said. "These guys got the same makeup, a kinship. It doesn't work unless you win together. It's kind of neat how that played out and how they interact with each other. They're definitely unique personalities in their own right."

Peavy would receive $100,000 each for NL MVP or World Series MVP, $75,000 for NL Championship Series MVP, $25,000 for All-Star selection, $50,000 for a Gold Glove or Silver Slugger award and $250,000 for winning the Cy Young Award. There are also bonuses for finishing in the top five in Cy Young voting.

A 31-year-old right-hander, Romo has spent all seven of his big league seasons with San Francisco, helping the Giants win three World Series championships in the past five years. He went 6-4 with a 3.72 ERA and 23 saves in 64 outings last season as San Francisco earned a wild card and went on to win it all against Kansas City.

Romo and Santiago Casilla have shared the closer job in recent years, with Casilla again taking over ninth-inning duties in 2014 after Romo blew three saves in June — a move by manager Bruce Bochy that paid off down the stretch.

The Sun Sentinel in Florida first reported the Marlins and Giants were working on a trade involving McGehee.

___

AP Sports Writer Ronald Blum in New York contributed to this report.