Skip to content
Merged
37 changes: 26 additions & 11 deletions src/ModelContextProtocol.Core/Protocol/BlobResourceContents.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Buffers;
using System.Buffers.Text;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
using System.Text.Json.Serialization;

Expand Down Expand Up @@ -28,7 +29,7 @@ namespace ModelContextProtocol.Protocol;
public sealed class BlobResourceContents : ResourceContents
{
private ReadOnlyMemory<byte>? _decodedData;
private ReadOnlyMemory<byte> _blob;
private ReadOnlyMemory<byte>? _blob;

/// <summary>
/// Creates an <see cref="BlobResourceContents"/> from raw data.
Expand All @@ -40,15 +41,20 @@ public sealed class BlobResourceContents : ResourceContents
/// <exception cref="InvalidOperationException"></exception>
public static BlobResourceContents FromBytes(ReadOnlyMemory<byte> bytes, string uri, string? mimeType = null)
{
ReadOnlyMemory<byte> blob = EncodingUtilities.EncodeToBase64Utf8(bytes);

return new()
{
_decodedData = bytes,
Blob = blob,
MimeType = mimeType,
Uri = uri
};
return new(bytes, uri, mimeType);
}

/// <summary>Initializes a new instance of the <see cref="BlobResourceContents"/> class.</summary>
public BlobResourceContents()
{
}

[SetsRequiredMembers]
private BlobResourceContents(ReadOnlyMemory<byte> decodedData, string uri, string? mimeType)
{
_decodedData = decodedData;
Uri = uri;
MimeType = mimeType;
}

/// <summary>
Expand All @@ -60,7 +66,16 @@ public static BlobResourceContents FromBytes(ReadOnlyMemory<byte> bytes, string
[JsonPropertyName("blob")]
public required ReadOnlyMemory<byte> Blob
{
get => _blob;
get
{
if (_blob is null)
{
Debug.Assert(_decodedData is not null);
_blob = EncodingUtilities.EncodeToBase64Utf8(_decodedData!.Value);
}

return _blob.Value;
}
set
{
_blob = value;
Expand Down
128 changes: 92 additions & 36 deletions src/ModelContextProtocol.Core/Protocol/ContentBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public sealed class Converter : JsonConverter<ContentBlock>
string? name = null;
string? title = null;
ReadOnlyMemory<byte>? data = null;
ReadOnlyMemory<byte>? decodedData = null;
string? mimeType = null;
string? uri = null;
string? description = null;
Expand Down Expand Up @@ -137,7 +138,14 @@ public sealed class Converter : JsonConverter<ContentBlock>
break;

case "data":
data = reader.HasValueSequence ? reader.ValueSequence.ToArray() : reader.ValueSpan.ToArray();
if (!reader.ValueIsEscaped)
{
data = reader.HasValueSequence ? reader.ValueSequence.ToArray() : reader.ValueSpan.ToArray();
}
else
{
decodedData = reader.GetBytesFromBase64();
}
break;

case "mimeType":
Expand Down Expand Up @@ -230,17 +238,23 @@ public sealed class Converter : JsonConverter<ContentBlock>
Text = text ?? throw new JsonException("Text contents must be provided for 'text' type."),
},

"image" => new ImageContentBlock
{
Data = data ?? throw new JsonException("Image data must be provided for 'image' type."),
MimeType = mimeType ?? throw new JsonException("MIME type must be provided for 'image' type."),
},

"audio" => new AudioContentBlock
{
Data = data ?? throw new JsonException("Audio data must be provided for 'audio' type."),
MimeType = mimeType ?? throw new JsonException("MIME type must be provided for 'audio' type."),
},
"image" => decodedData is not null ?
ImageContentBlock.FromBytes(decodedData.Value,
mimeType ?? throw new JsonException("MIME type must be provided for 'image' type.")) :
new ImageContentBlock
{
Data = data ?? throw new JsonException("Image data must be provided for 'image' type."),
MimeType = mimeType ?? throw new JsonException("MIME type must be provided for 'image' type."),
},

"audio" => decodedData is not null ?
AudioContentBlock.FromBytes(decodedData.Value,
mimeType ?? throw new JsonException("MIME type must be provided for 'audio' type.")) :
new AudioContentBlock
{
Data = data ?? throw new JsonException("Audio data must be provided for 'audio' type."),
MimeType = mimeType ?? throw new JsonException("MIME type must be provided for 'audio' type."),
},

"resource" => new EmbeddedResourceBlock
{
Expand Down Expand Up @@ -414,7 +428,7 @@ public sealed class TextContentBlock : ContentBlock
public sealed class ImageContentBlock : ContentBlock
{
private ReadOnlyMemory<byte>? _decodedData;
private ReadOnlyMemory<byte> _data;
private ReadOnlyMemory<byte>? _data;

/// <summary>
/// Creates an <see cref="ImageContentBlock"/> from decoded image bytes.
Expand All @@ -423,22 +437,27 @@ public sealed class ImageContentBlock : ContentBlock
/// <param name="mimeType">The MIME type of the image.</param>
/// <returns>A new <see cref="ImageContentBlock"/> instance.</returns>
/// <remarks>
/// This method stores the provided bytes as <see cref="DecodedData"/> and encodes them to base64 UTF-8 bytes for <see cref="Data"/>.
/// This method stores the provided bytes as <see cref="DecodedData"/> and lazily encodes them to base64 UTF-8 bytes for <see cref="Data"/>.
/// </remarks>
/// <exception cref="ArgumentNullException"><paramref name="mimeType"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException"><paramref name="mimeType"/> is empty or composed entirely of whitespace.</exception>
public static ImageContentBlock FromBytes(ReadOnlyMemory<byte> bytes, string mimeType)
{
Throw.IfNullOrWhiteSpace(mimeType);

ReadOnlyMemory<byte> data = EncodingUtilities.EncodeToBase64Utf8(bytes);

return new()
{
_decodedData = bytes,
Data = data,
MimeType = mimeType
};
return new(bytes, mimeType);
}

/// <summary>Initializes a new instance of the <see cref="ImageContentBlock"/> class.</summary>
public ImageContentBlock()
{
}

[SetsRequiredMembers]
private ImageContentBlock(ReadOnlyMemory<byte> decodedData, string mimeType)
{
_decodedData = decodedData;
MimeType = mimeType;
}

/// <inheritdoc/>
Expand All @@ -453,7 +472,16 @@ public static ImageContentBlock FromBytes(ReadOnlyMemory<byte> bytes, string mim
[JsonPropertyName("data")]
public required ReadOnlyMemory<byte> Data
{
get => _data;
get
{
if (_data is null)
{
Debug.Assert(_decodedData is not null);
_data = EncodingUtilities.EncodeToBase64Utf8(_decodedData!.Value);
}

return _data.Value;
}
set
{
_data = value;
Expand Down Expand Up @@ -494,15 +522,22 @@ public ReadOnlyMemory<byte> DecodedData
public required string MimeType { get; set; }

[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private string DebuggerDisplay => $"MimeType = {MimeType}, Length = {DebuggerDisplayHelper.GetBase64LengthDisplay(Data)}";
private string DebuggerDisplay
{
get
{
string lengthDisplay = _decodedData is not null ? $"{_decodedData.Value.Length} bytes" : DebuggerDisplayHelper.GetBase64LengthDisplay(Data);
return $"MimeType = {MimeType}, Length = {lengthDisplay}";
}
}
}

/// <summary>Represents audio provided to or from an LLM.</summary>
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public sealed class AudioContentBlock : ContentBlock
{
private ReadOnlyMemory<byte>? _decodedData;
private ReadOnlyMemory<byte> _data;
private ReadOnlyMemory<byte>? _data;

/// <summary>
/// Creates an <see cref="AudioContentBlock"/> from decoded audio bytes.
Expand All @@ -511,22 +546,27 @@ public sealed class AudioContentBlock : ContentBlock
/// <param name="mimeType">The MIME type of the audio.</param>
/// <returns>A new <see cref="AudioContentBlock"/> instance.</returns>
/// <remarks>
/// This method stores the provided bytes as <see cref="DecodedData"/> and encodes them to base64 UTF-8 bytes for <see cref="Data"/>.
/// This method stores the provided bytes as <see cref="DecodedData"/> and lazily encodes them to base64 UTF-8 bytes for <see cref="Data"/>.
/// </remarks>
/// <exception cref="ArgumentNullException"><paramref name="mimeType"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException"><paramref name="mimeType"/> is empty or composed entirely of whitespace.</exception>
public static AudioContentBlock FromBytes(ReadOnlyMemory<byte> bytes, string mimeType)
{
Throw.IfNullOrWhiteSpace(mimeType);

ReadOnlyMemory<byte> data = EncodingUtilities.EncodeToBase64Utf8(bytes);

return new()
{
_decodedData = bytes,
Data = data,
MimeType = mimeType
};
return new(bytes, mimeType);
}

/// <summary>Initializes a new instance of the <see cref="AudioContentBlock"/> class.</summary>
public AudioContentBlock()
{
}

[SetsRequiredMembers]
private AudioContentBlock(ReadOnlyMemory<byte> decodedData, string mimeType)
{
_decodedData = decodedData;
MimeType = mimeType;
}

/// <inheritdoc/>
Expand All @@ -541,7 +581,16 @@ public static AudioContentBlock FromBytes(ReadOnlyMemory<byte> bytes, string mim
[JsonPropertyName("data")]
public required ReadOnlyMemory<byte> Data
{
get => _data;
get
{
if (_data is null)
{
Debug.Assert(_decodedData is not null);
_data = EncodingUtilities.EncodeToBase64Utf8(_decodedData!.Value);
}

return _data.Value;
}
set
{
_data = value;
Expand Down Expand Up @@ -582,7 +631,14 @@ public ReadOnlyMemory<byte> DecodedData
public required string MimeType { get; set; }

[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private string DebuggerDisplay => $"MimeType = {MimeType}, Length = {DebuggerDisplayHelper.GetBase64LengthDisplay(Data)}";
private string DebuggerDisplay
{
get
{
string lengthDisplay = _decodedData is not null ? $"{_decodedData.Value.Length} bytes" : DebuggerDisplayHelper.GetBase64LengthDisplay(Data);
return $"MimeType = {MimeType}, Length = {lengthDisplay}";
}
}
}

/// <summary>Represents the contents of a resource, embedded into a prompt or tool call result.</summary>
Expand Down
17 changes: 16 additions & 1 deletion src/ModelContextProtocol.Core/Protocol/ResourceContents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public sealed class Converter : JsonConverter<ResourceContents>
string? uri = null;
string? mimeType = null;
ReadOnlyMemory<byte>? blob = null;
ReadOnlyMemory<byte>? decodedBlob = null;
string? text = null;
JsonObject? meta = null;

Expand All @@ -105,7 +106,14 @@ public sealed class Converter : JsonConverter<ResourceContents>
break;

case "blob":
blob = reader.HasValueSequence ? reader.ValueSequence.ToArray() : reader.ValueSpan.ToArray();
if (!reader.ValueIsEscaped)
{
blob = reader.HasValueSequence ? reader.ValueSequence.ToArray() : reader.ValueSpan.ToArray();
}
else
{
decodedBlob = reader.GetBytesFromBase64();
}
break;

case "text":
Expand All @@ -122,6 +130,13 @@ public sealed class Converter : JsonConverter<ResourceContents>
}
}

if (decodedBlob is not null)
{
var blobResource = BlobResourceContents.FromBytes(decodedBlob.Value, uri ?? string.Empty, mimeType);
blobResource.Meta = meta;
return blobResource;
}

if (blob is not null)
{
return new BlobResourceContents
Expand Down
Loading