I have a similar problem.
You can store any function in Mongodb. I used this to create a function that returns the current date and time and tried to call it from the metabase.
https://docs.mongodb.com/manual/tutorial/store-javascript-function-on-server/
However, in order to execute any additional functions, you need to run db.loadServerScripts() to load the function.
I couldn’t do this with metabase, so I gave up.
Translated with www.DeepL.com/Translator (free version)