The two words are often used in contracts, since the contracting parties always have obligations to undertake, and they always have to agree on the different terms of the contract or agreement.
To undertake something means that the parties both agree to commit themselves to an obligation.
If the parties agree, it means that they come to an understanding about something. In contracts, the verb ‘undertake’ may be replaced by ‘agree’, but it does not work the other way round.