Skip to content
Merged
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
82 changes: 42 additions & 40 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,59 @@ 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()
null => new()
{
Content = []
},
string text => new()
{
Content = [new() { Text = text, Type = "text" }]
},
TextContent textContent => new()
{
Content = [new() { Text = textContent.Text, Type = "text" }]
},
DataContent dataContent => new()
{
Content = [new()
{
Data = dataContent.GetBase64Data(),
MimeType = dataContent.MediaType,
Type = dataContent.HasTopLevelMediaType("image") ? "image" : "resource",
}]
};
},
string[] texts => new()
{
Content = [.. texts.Select(x => new Content() { Type = "text", Text = x ?? string.Empty })]
},

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

// 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"
}]
};
}
},
};
}
}