encodeMcpHeaderValue function

String encodeMcpHeaderValue(
  1. String value
)

A header value as Streamable HTTP carries it: as-is when it is plain visible ASCII without surrounding whitespace, otherwise (or when it looks like the sentinel) =?base64?…?= of its UTF-8 bytes.

Implementation

String encodeMcpHeaderValue(String value) {
  final plain = value.codeUnits.every((c) => c >= 0x20 && c <= 0x7e) &&
      value.trim() == value &&
      !(value.startsWith('=?base64?') && value.endsWith('?='));
  return plain ? value : '=?base64?${base64.encode(utf8.encode(value))}?=';
}