Split up MessageMetadataHelper and use it in more places

This is extracted from #821 because it's useful on its own.