Skip to content

Remove internal GetBase64Data helper #422

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 16, 2025
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
14 changes: 1 addition & 13 deletions src/ModelContextProtocol/AIContentExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,18 +178,6 @@ public static IList<AIContent> ToAIContents(this IEnumerable<ResourceContents> c
return [.. contents.Select(ToAIContent)];
}

/// <summary>Extracts the data from a <see cref="DataContent"/> as a Base64 string.</summary>
internal static string GetBase64Data(this DataContent dataContent)
{
#if NET
return Convert.ToBase64String(dataContent.Data.Span);
#else
return MemoryMarshal.TryGetArray(dataContent.Data, out ArraySegment<byte> segment) ?
Convert.ToBase64String(segment.Array!, segment.Offset, segment.Count) :
Convert.ToBase64String(dataContent.Data.ToArray());
#endif
}

internal static Content ToContent(this AIContent content) =>
content switch
{
Expand All @@ -201,7 +189,7 @@ internal static Content ToContent(this AIContent content) =>

DataContent dataContent => new()
{
Data = dataContent.GetBase64Data(),
Data = dataContent.Base64Data.ToString(),
MimeType = dataContent.MediaType,
Type =
dataContent.HasTopLevelMediaType("image") ? "image" :
Expand Down
2 changes: 1 addition & 1 deletion src/ModelContextProtocol/Client/McpClientExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -976,7 +976,7 @@ internal static CreateMessageResult ToCreateMessageResult(this ChatResponse chat
{
Type = dc.HasTopLevelMediaType("image") ? "image" : "audio",
MimeType = dc.MediaType,
Data = dc.GetBase64Data(),
Data = dc.Base64Data.ToString(),
};
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ private AIFunctionMcpServerResource(AIFunction function, ResourceTemplate resour

DataContent dc => new()
{
Contents = [new BlobResourceContents() { Uri = request.Params!.Uri, MimeType = dc.MediaType, Blob = dc.GetBase64Data() }],
Contents = [new BlobResourceContents() { Uri = request.Params!.Uri, MimeType = dc.MediaType, Blob = dc.Base64Data.ToString() }],
},

string text => new()
Expand Down Expand Up @@ -429,7 +429,7 @@ private AIFunctionMcpServerResource(AIFunction function, ResourceTemplate resour
{
Uri = request.Params!.Uri,
MimeType = dc.MediaType,
Blob = dc.GetBase64Data()
Blob = dc.Base64Data.ToString()
},

_ => throw new InvalidOperationException($"Unsupported AIContent type '{ac.GetType()}' returned from resource function."),
Expand Down
2 changes: 1 addition & 1 deletion src/ModelContextProtocol/Server/McpServerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public static async Task<ChatResponse> RequestSamplingAsync(
{
Type = dataContent.HasTopLevelMediaType("image") ? "image" : "audio",
MimeType = dataContent.MediaType,
Data = dataContent.GetBase64Data(),
Data = dataContent.Base64Data.ToString(),
},
});
break;
Expand Down