Ah yes. My custom Node script (which I hope it's OK to paste below) strips the beginnings and ends of the input. I suppose that it could be done already on the Elisp side, which would take care of interpreting the $-signs as envvars too.
const katex = require('katex')
let input = process.argv[2].trim()
let disp = true
if (input.slice(0, 2) === '\\[' || input.slice(0, 2) === '$$') {
input = input.slice(2, -2)
}
else if (input.slice(0, 2) === '\\(') {
input = input.slice(2, -2)
disp = false
}
else if (input.slice(0, 1) === '$') {
input = input.slice(1, -1)
disp = false
}
else {
console.error("Did you quote the input correctly?")
process.exit(1)
}
console.log(katex.renderToString(
input, {
displayMode: disp,
output: 'mathml',
trust: true,
strict: false,
throwOnError: false,
}
))