Guard MessagePack with a ThreadLocal where necessary

Unfortunately MessagePack (or more precisely the TemplateRegistry and BuildContext)
is not thread-safe, so each thread will need its own instance of MessagePack to
work correctly.

(cherry picked from commit d4405a9cce22742a7053b3b5cb45a260a0efb93f)