The Giants and Matt Cain have agreed to a five-year contract extension through the 2017 season.

The deal, which supersedes Cain's current contract, includes $112.5 million of new guaranteed money and a vesting option for 2018 that could bring the total value of the deal to $141 million.

It is the largest contract ever handed out to a right-handed pitcher and the second-largest contract for a pitcher who was not a free agent at the time. Cain, who would have been a free agent after this season, also got a full no-trade clause.

"Ensuring that Matt remained a Giant beyond this season was a top priority for the organization," general manger Brian Sabean said in a statement. "Matt is an integral part of the team whose performance on the mound will be one of the keys to our success for years to come."