Skip to content

Commit b1ccca1

Browse files
Copilotstephentoub
andcommitted
Refine DebuggerDisplay implementations based on feedback
- Move try/catch inside #else block for Base64 validation - Create DebuggerDisplayHelper internal class for shared base64 decoding logic - Simplify DebuggerDisplay attribute escaping (use \" instead of \\\") - Use DebuggerDisplayHelper in both ContentBlock types and BlobResourceContents Co-authored-by: stephentoub <[email protected]>
1 parent e234284 commit b1ccca1

File tree

4 files changed

+39
-51
lines changed

4 files changed

+39
-51
lines changed

src/ModelContextProtocol.Core/Protocol/BlobResourceContents.cs

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -35,28 +35,7 @@ private string DebuggerDisplay
3535
{
3636
get
3737
{
38-
string lengthDisplay;
39-
try
40-
{
41-
#if NET
42-
if (System.Buffers.Text.Base64.IsValid(System.Text.Encoding.UTF8.GetBytes(Blob), out int decodedLength))
43-
{
44-
lengthDisplay = $"{decodedLength} bytes";
45-
}
46-
else
47-
{
48-
lengthDisplay = "invalid base64";
49-
}
50-
#else
51-
byte[] decoded = Convert.FromBase64String(Blob);
52-
lengthDisplay = $"{decoded.Length} bytes";
53-
#endif
54-
}
55-
catch
56-
{
57-
lengthDisplay = "invalid base64";
58-
}
59-
38+
string lengthDisplay = DebuggerDisplayHelper.GetBase64LengthDisplay(Blob);
6039
string mimeInfo = MimeType is not null ? $", MimeType = {MimeType}" : "";
6140
return $"Uri = \"{Uri}\"{mimeInfo}, Length = {lengthDisplay}";
6241
}

src/ModelContextProtocol.Core/Protocol/ContentBlock.cs

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -32,30 +32,6 @@ private protected ContentBlock()
3232
{
3333
}
3434

35-
/// <summary>
36-
/// Helper method to get the decoded length of base64 data for debugger display.
37-
/// </summary>
38-
private protected static string GetBase64LengthDisplay(string base64Data)
39-
{
40-
try
41-
{
42-
#if NET
43-
if (System.Buffers.Text.Base64.IsValid(System.Text.Encoding.UTF8.GetBytes(base64Data), out int decodedLength))
44-
{
45-
return $"{decodedLength} bytes";
46-
}
47-
return "invalid base64";
48-
#else
49-
byte[] decoded = Convert.FromBase64String(base64Data);
50-
return $"{decoded.Length} bytes";
51-
#endif
52-
}
53-
catch
54-
{
55-
return "invalid base64";
56-
}
57-
}
58-
5935
/// <summary>
6036
/// When overridden in a derived class, gets the type of content.
6137
/// </summary>
@@ -380,7 +356,7 @@ public override void Write(Utf8JsonWriter writer, ContentBlock value, JsonSerial
380356
}
381357

382358
/// <summary>Represents text provided to or from an LLM.</summary>
383-
[DebuggerDisplay("Text = \\\"{Text}\\\"")]
359+
[DebuggerDisplay("Text = \"{Text}\"")]
384360
public sealed class TextContentBlock : ContentBlock
385361
{
386362
/// <inheritdoc/>
@@ -419,7 +395,7 @@ public sealed class ImageContentBlock : ContentBlock
419395
public required string MimeType { get; set; }
420396

421397
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
422-
private string DebuggerDisplay => $"MimeType = {MimeType}, Length = {GetBase64LengthDisplay(Data)}";
398+
private string DebuggerDisplay => $"MimeType = {MimeType}, Length = {DebuggerDisplayHelper.GetBase64LengthDisplay(Data)}";
423399
}
424400

425401
/// <summary>Represents audio provided to or from an LLM.</summary>
@@ -445,7 +421,7 @@ public sealed class AudioContentBlock : ContentBlock
445421
public required string MimeType { get; set; }
446422

447423
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
448-
private string DebuggerDisplay => $"MimeType = {MimeType}, Length = {GetBase64LengthDisplay(Data)}";
424+
private string DebuggerDisplay => $"MimeType = {MimeType}, Length = {DebuggerDisplayHelper.GetBase64LengthDisplay(Data)}";
449425
}
450426

451427
/// <summary>Represents the contents of a resource, embedded into a prompt or tool call result.</summary>
@@ -479,7 +455,7 @@ public sealed class EmbeddedResourceBlock : ContentBlock
479455
/// <remarks>
480456
/// Resource links returned by tools are not guaranteed to appear in the results of `resources/list` requests.
481457
/// </remarks>
482-
[DebuggerDisplay("Name = {Name}, Uri = \\\"{Uri}\\\"")]
458+
[DebuggerDisplay("Name = {Name}, Uri = \"{Uri}\"")]
483459
public sealed class ResourceLinkBlock : ContentBlock
484460
{
485461
/// <inheritdoc/>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
3+
namespace ModelContextProtocol.Protocol;
4+
5+
/// <summary>
6+
/// Internal helper methods for DebuggerDisplay implementations.
7+
/// </summary>
8+
internal static class DebuggerDisplayHelper
9+
{
10+
/// <summary>
11+
/// Gets the decoded length of base64 data for debugger display.
12+
/// </summary>
13+
internal static string GetBase64LengthDisplay(string base64Data)
14+
{
15+
#if NET
16+
if (System.Buffers.Text.Base64.IsValid(System.Text.Encoding.UTF8.GetBytes(base64Data), out int decodedLength))
17+
{
18+
return $"{decodedLength} bytes";
19+
}
20+
return "invalid base64";
21+
#else
22+
try
23+
{
24+
byte[] decoded = Convert.FromBase64String(base64Data);
25+
return $"{decoded.Length} bytes";
26+
}
27+
catch
28+
{
29+
return "invalid base64";
30+
}
31+
#endif
32+
}
33+
}

src/ModelContextProtocol.Core/Protocol/TextResourceContents.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ namespace ModelContextProtocol.Protocol;
2020
/// See the <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/">schema</see> for more details.
2121
/// </para>
2222
/// </remarks>
23-
[DebuggerDisplay("Uri = \\\"{Uri}\\\", Text = \\\"{Text}\\\"")]
23+
[DebuggerDisplay("Uri = \"{Uri}\", Text = \"{Text}\"")]
2424
public sealed class TextResourceContents : ResourceContents
2525
{
2626
/// <summary>

0 commit comments

Comments
 (0)