Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
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",
}
};
}
83 changes: 37 additions & 46 deletions src/ModelContextProtocol/Server/AIFunctionMcpServerTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ internal sealed class AIFunctionMcpServerTool : McpServerTool
public static new AIFunctionMcpServerTool Create(
Delegate method,
string? name,
string? description,
string? description,
IServiceProvider? services)
{
Throw.IfNull(method);
Expand All @@ -34,7 +34,7 @@ internal sealed class AIFunctionMcpServerTool : McpServerTool
/// Creates an <see cref="McpServerTool"/> instance for a method, specified via a <see cref="Delegate"/> instance.
/// </summary>
public static new AIFunctionMcpServerTool Create(
MethodInfo method,
MethodInfo method,
object? target,
string? name,
string? description,
Expand Down Expand Up @@ -195,57 +195,48 @@ public override async Task<CallToolResponse> InvokeAsync(
};
}

switch (result)
return result switch
{
case null:
return new()
{
Content = []
};

case string text:
return new()
{
Content = [new() { Text = text, Type = "text" }]
};

case TextContent textContent:
return new()
{
Content = [new() { Text = textContent.Text, Type = "text" }]
};

case DataContent dataContent:
return new()
{
Content = [new()
{
Data = dataContent.GetBase64Data(),
MimeType = dataContent.MediaType,
Type = dataContent.HasTopLevelMediaType("image") ? "image" : "resource",
}]
};

case string[] texts:
return new()
{
Content = texts
.Select(x => new Content() { Type = "text", Text = x ?? string.Empty })
.ToList()
};
null => new()
{
Content = []
},
string text => new()
{
Content = [new() { Text = text, Type = "text" }]
},
TextContent textContent => new()
{
Content = [textContent.ToContent()]
},
DataContent dataContent => new()
{
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.ToContent())]
},
IEnumerable<Content> contents => new()
{
Content = [.. contents]
},

// TODO https://github.com/modelcontextprotocol/csharp-sdk/issues/69:
// Add specialization for annotations.

default:
return new()
{
Content = [new()
_ => new()
{
Content = [new()
{
Text = JsonSerializer.Serialize(result, McpJsonUtilities.DefaultOptions.GetTypeInfo(typeof(object))),
Type = "text"
}]
};
}
},
};
}

}