Skip to content

Commit 3073246

Browse files
Copilotstephentoub
authored andcommitted
Add cache invalidation when Blob/Data properties are set
Co-authored-by: stephentoub <[email protected]>
1 parent 202bead commit 3073246

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

src/ModelContextProtocol.Core/Protocol/BlobResourceContents.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ namespace ModelContextProtocol.Protocol;
2525
public sealed class BlobResourceContents : ResourceContents
2626
{
2727
private byte[]? _decodedData;
28+
private ReadOnlyMemory<byte> _blob;
2829

2930
/// <summary>
3031
/// Gets or sets the base64-encoded UTF-8 bytes representing the binary data of the item.
@@ -33,7 +34,15 @@ public sealed class BlobResourceContents : ResourceContents
3334
/// This is a zero-copy representation of the wire payload of this item. Setting this value will invalidate any cached value of <see cref="Data"/>.
3435
/// </remarks>
3536
[JsonPropertyName("blob")]
36-
public required ReadOnlyMemory<byte> Blob { get; set; }
37+
public required ReadOnlyMemory<byte> Blob
38+
{
39+
get => _blob;
40+
set
41+
{
42+
_blob = value;
43+
_decodedData = null; // Invalidate cache
44+
}
45+
}
3746

3847
/// <summary>
3948
/// Gets the decoded data represented by <see cref="Blob"/>.

src/ModelContextProtocol.Core/Protocol/ContentBlock.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,7 @@ public sealed class TextContentBlock : ContentBlock
388388
public sealed class ImageContentBlock : ContentBlock
389389
{
390390
private byte[]? _decodedData;
391+
private ReadOnlyMemory<byte> _data;
391392

392393
/// <inheritdoc/>
393394
public override string Type => "image";
@@ -399,7 +400,15 @@ public sealed class ImageContentBlock : ContentBlock
399400
/// This is a zero-copy representation of the wire payload of this item. Setting this value will invalidate any cached value of <see cref="DecodedData"/>.
400401
/// </remarks>
401402
[JsonPropertyName("data")]
402-
public required ReadOnlyMemory<byte> Data { get; set; }
403+
public required ReadOnlyMemory<byte> Data
404+
{
405+
get => _data;
406+
set
407+
{
408+
_data = value;
409+
_decodedData = null; // Invalidate cache
410+
}
411+
}
403412

404413
/// <summary>
405414
/// Gets the decoded image data represented by <see cref="Data"/>.
@@ -443,6 +452,7 @@ public ReadOnlyMemory<byte> DecodedData
443452
public sealed class AudioContentBlock : ContentBlock
444453
{
445454
private byte[]? _decodedData;
455+
private ReadOnlyMemory<byte> _data;
446456

447457
/// <inheritdoc/>
448458
public override string Type => "audio";
@@ -454,7 +464,15 @@ public sealed class AudioContentBlock : ContentBlock
454464
/// This is a zero-copy representation of the wire payload of this item. Setting this value will invalidate any cached value of <see cref="DecodedData"/>.
455465
/// </remarks>
456466
[JsonPropertyName("data")]
457-
public required ReadOnlyMemory<byte> Data { get; set; }
467+
public required ReadOnlyMemory<byte> Data
468+
{
469+
get => _data;
470+
set
471+
{
472+
_data = value;
473+
_decodedData = null; // Invalidate cache
474+
}
475+
}
458476

459477
/// <summary>
460478
/// Gets the decoded audio data represented by <see cref="Data"/>.

0 commit comments

Comments
 (0)