mcpParamHeaderValues function

Map<String, String> mcpParamHeaderValues(
  1. List<McpParamHeader> headers,
  2. Map arguments
)

The Mcp-Param-{name} values for a call with arguments (absent and null values omitted), encoded for the header; throws ArgumentError for a value of the wrong type or an integer outside the safe range.

Implementation

Map<String, String> mcpParamHeaderValues(List<McpParamHeader> headers, Map arguments) {
  final out = <String, String>{};
  for (final h in headers) {
    Object? value = arguments;
    for (final key in h.path) {
      value = value is Map ? value[key] : null;
    }
    if (value == null) continue;
    final String text;
    switch (h.type) {
      case 'string' when value is String:
        text = value;
      case 'boolean' when value is bool:
        text = '$value';
      case 'integer' when value is num && value == value.truncate():
        if (value.abs() > _maxSafeInteger) {
          throw ArgumentError.value(value, h.name, 'outside the safe integer range');
        }
        text = '${value.truncate()}';
      default:
        throw ArgumentError.value(value, h.name, 'is not a ${h.type}');
    }
    out['Mcp-Param-${h.name}'] = encodeMcpHeaderValue(text);
  }
  return out;
}