Class McpHeaderEncoder
- Namespace
- ModelContextProtocol.Protocol
- Assembly
- ModelContextProtocol.Core.dll
Encodes and decodes parameter values for use in MCP HTTP headers according to the HTTP Standardization SEP.
public static class McpHeaderEncoder
- Inheritance
-
McpHeaderEncoder
- Inherited Members
Remarks
This encoder handles conversion of parameter values to HTTP header-safe strings, including Base64 encoding for values that cannot be safely transmitted as plain text.
Per SEP-2243 only primitive parameter types are supported: string, integer, and
boolean. The JSON Schema number type is not permitted, and integer values must be
within the JavaScript safe integer range (−2^53+1 to 2^53−1).
Encoding rules:
- Plain ASCII values (0x20-0x7E): sent as-is
- Values with leading/trailing whitespace: Base64 encoded with
=?base64?{value}?=wrapper - Non-ASCII characters: Base64 encoded
- Control characters: Base64 encoded
- Plain ASCII values that themselves match the
=?base64?...?=sentinel pattern: Base64 encoded to avoid ambiguity
Methods
ConvertToHeaderValue(JsonElement)
Converts a JsonElement value to an encoded header value string.
public static string? ConvertToHeaderValue(JsonElement element)
Parameters
elementJsonElementThe JSON element to convert.
Returns
ConvertToHeaderValue(JsonNode)
Converts a JsonNode value to an encoded header value string.
public static string? ConvertToHeaderValue(JsonNode node)
Parameters
nodeJsonNodeThe JSON node to convert.
Returns
- string
The encoded header value, or null if the node is not a JsonValue or is not a supported primitive type.
DecodeValue(string?)
Decodes a header value that may be Base64-encoded according to SEP rules.
public static string? DecodeValue(string? headerValue)
Parameters
headerValuestringThe header value to decode.
Returns
- string
The decoded string value, or null if decoding fails. If the value is not Base64-encoded, returns the original value.
EncodeValue(bool)
Encodes a boolean parameter value for use in an HTTP header.
public static string EncodeValue(bool value)
Parameters
valueboolThe boolean value to encode.
Returns
- string
The encoded header value (
"true"or"false").
EncodeValue(long)
Encodes an integer parameter value for use in an HTTP header.
public static string EncodeValue(long value)
Parameters
valuelongThe integer value to encode.
Returns
- string
The decimal string representation of the value.
EncodeValue(object?)
Encodes a parameter value for use in an HTTP header.
public static string? EncodeValue(object? value)
Parameters
valueobjectThe value to encode. Supported types are
string, integer, andboolean.
Returns
- string
The encoded header value, or null if the value is null or is not a supported type (string, integer, or boolean).
EncodeValue(string?)
Encodes a string parameter value for use in an HTTP header.
public static string? EncodeValue(string? value)
Parameters
valuestringThe string value to encode.