This addresses a problem Thierry found recently, but I doubt it solves it. Not sure what is being sent to the browser now. Have no easy way to reproduce this.
Updated to the new 'add_tool' signature.