Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/ModelContextProtocol.Core/Protocol/ContentBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -391,9 +391,12 @@ public sealed class ImageContentBlock : ContentBlock
/// <remarks>
/// This method stores the provided bytes as <see cref="DecodedData"/> and encodes them to base64 UTF-8 bytes for <see cref="Data"/>.
/// </remarks>
/// <exception cref="InvalidOperationException"></exception>
/// <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()
Expand Down Expand Up @@ -476,9 +479,12 @@ public sealed class AudioContentBlock : ContentBlock
/// <remarks>
/// This method stores the provided bytes as <see cref="DecodedData"/> and encodes them to base64 UTF-8 bytes for <see cref="Data"/>.
/// </remarks>
/// <exception cref="InvalidOperationException"></exception>
/// <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()
Expand Down
18 changes: 18 additions & 0 deletions tests/ModelContextProtocol.Tests/Protocol/ContentBlockTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,4 +225,22 @@ public void ToolUseContentBlock_SerializationRoundTrip()
Assert.Equal("Paris", result.Input.GetProperty("city").GetString());
Assert.Equal("metric", result.Input.GetProperty("units").GetString());
}

[Theory]
[InlineData(null)]
[InlineData("")]
[InlineData(" ")]
public void ImageContentBlock_FromBytes_ThrowsForNullOrWhiteSpaceMimeType(string? mimeType)
{
Assert.ThrowsAny<ArgumentException>(() => ImageContentBlock.FromBytes((byte[])[1, 2, 3], mimeType!));
}

[Theory]
[InlineData(null)]
[InlineData("")]
[InlineData(" ")]
public void AudioContentBlock_FromBytes_ThrowsForNullOrWhiteSpaceMimeType(string? mimeType)
{
Assert.ThrowsAny<ArgumentException>(() => AudioContentBlock.FromBytes((byte[])[1, 2, 3], mimeType!));
}
}