Mary bought a book for James doesn't mean that she gave it to James. It means that she
intended to give it to James later. This is the
usual meaning.
But
for also means
in place of or
instead of.
So an alternate meaning is that James could not buy the book
himself. Perhaps he was busy and had no time to buy it so he
asked Mary to buy it
for him -- to buy it 'in his place', i.e.,
acting as his representative, so to speak. James gave her the
money to buy the book. He intended to give the book to
Ethel.
In this case Mary bought the book for James so James could give it to Ethel later. So it's true that
Mary bought the book for James, and also true that
James bought the book for Ethel. Mary bought the book as James's representative, and James bought the book to give to Ethel.
CJ