Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
32 changes: 32 additions & 0 deletions src/ModelContextProtocol/AIContentExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using Microsoft.Extensions.AI;
using ModelContextProtocol.Protocol.Types;
using ModelContextProtocol.Utils;
using ModelContextProtocol.Utils.Json;
using System.Runtime.InteropServices;
using System.Text.Json;

namespace ModelContextProtocol;

Expand Down Expand Up @@ -101,4 +103,34 @@ internal static string GetBase64Data(this DataContent dataContent)
Convert.ToBase64String(dataContent.Data.ToArray());
#endif
}

/// <summary>
/// Converts different types of <see cref="AIContent"/> into a standardized <see cref="Content"/> object with specific properties based on the
/// content type.
/// </summary>
/// <param name="content"></param>
/// <returns>A <see cref="Content"/> object that encapsulates the relevant properties derived from the input content.</returns>
public static Content ToContent(this AIContent content) =>
content switch
{
TextContent textContent => new()
{
Text = textContent.Text,
Type = "text",
},
DataContent dataContent => new()
{
Data = dataContent.GetBase64Data(),
MimeType = dataContent.MediaType,
Type =
dataContent.HasTopLevelMediaType("image") ? "image" :
dataContent.HasTopLevelMediaType("audio") ? "audio" :
"resource",
},
_ => new()
{
Text = JsonSerializer.Serialize(content, McpJsonUtilities.DefaultOptions.GetTypeInfo(typeof(object))),
Type = "text",
}
};
}
27 changes: 8 additions & 19 deletions src/ModelContextProtocol/Server/AIFunctionMcpServerTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,35 +207,23 @@ public override async Task<CallToolResponse> InvokeAsync(
},
TextContent textContent => new()
{
Content = [new() { Text = textContent.Text, Type = "text" }]
Content = [textContent.ToContent()]
},
DataContent dataContent => new()
{
Content = [new()
{
Data = dataContent.GetBase64Data(),
MimeType = dataContent.MediaType,
Type = dataContent.HasTopLevelMediaType("image") ? "image" : "resource",
}]
Content = [dataContent.ToContent()]
},
string[] texts => new()
{
Content = [.. texts.Select(x => new Content() { Type = "text", Text = x ?? string.Empty })]
},

IEnumerable<AIContent> contentItems => new()
{
Content = [.. contentItems.Select(static item => item switch
{
TextContent textContent => new Content() { Type = "text", Text = textContent.Text },
DataContent dataContent => new Content()
{
Data = dataContent.GetBase64Data(),
MimeType = dataContent.MediaType,
Type = dataContent.HasTopLevelMediaType("image") ? "image" : "resource",
},
_ => new Content() { Type = "text", Text = item.ToString() ?? string.Empty }
})]
Content = [.. contentItems.Select(static item => item.ToContent())]
},
IEnumerable<Content> contents => new()
{
Content = [.. contents]
},

// TODO https://github.com/modelcontextprotocol/csharp-sdk/issues/69:
Expand All @@ -250,4 +238,5 @@ public override async Task<CallToolResponse> InvokeAsync(
},
};
}

}