Skip to content

Commit 605e1ba

Browse files
committed
Code review feedback
1 parent 5bbf1c2 commit 605e1ba

File tree

2 files changed

+40
-19
lines changed

2 files changed

+40
-19
lines changed

src/ModelContextProtocol/AIContentExtensions.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using Microsoft.Extensions.AI;
22
using ModelContextProtocol.Protocol.Types;
33
using ModelContextProtocol.Utils;
4+
using ModelContextProtocol.Utils.Json;
45
using System.Runtime.InteropServices;
6+
using System.Text.Json;
57

68
namespace ModelContextProtocol;
79

@@ -101,4 +103,34 @@ internal static string GetBase64Data(this DataContent dataContent)
101103
Convert.ToBase64String(dataContent.Data.ToArray());
102104
#endif
103105
}
106+
107+
/// <summary>
108+
/// Converts different types of <see cref="AIContent"/> into a standardized <see cref="Content"/> object with specific properties based on the
109+
/// content type.
110+
/// </summary>
111+
/// <param name="content"></param>
112+
/// <returns>A <see cref="Content"/> object that encapsulates the relevant properties derived from the input content.</returns>
113+
public static Content ToContent(this AIContent content) =>
114+
content switch
115+
{
116+
TextContent textContent => new()
117+
{
118+
Text = textContent.Text,
119+
Type = "text",
120+
},
121+
DataContent dataContent => new()
122+
{
123+
Data = dataContent.GetBase64Data(),
124+
MimeType = dataContent.MediaType,
125+
Type =
126+
dataContent.HasTopLevelMediaType("image") ? "image" :
127+
dataContent.HasTopLevelMediaType("audio") ? "audio" :
128+
"resource",
129+
},
130+
_ => new()
131+
{
132+
Text = JsonSerializer.Serialize(content, McpJsonUtilities.DefaultOptions.GetTypeInfo(typeof(object))),
133+
Type = "text",
134+
}
135+
};
104136
}

src/ModelContextProtocol/Server/AIFunctionMcpServerTool.cs

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -207,35 +207,23 @@ public override async Task<CallToolResponse> InvokeAsync(
207207
},
208208
TextContent textContent => new()
209209
{
210-
Content = [new() { Text = textContent.Text, Type = "text" }]
210+
Content = [textContent.ToContent()]
211211
},
212212
DataContent dataContent => new()
213213
{
214-
Content = [new()
215-
{
216-
Data = dataContent.GetBase64Data(),
217-
MimeType = dataContent.MediaType,
218-
Type = dataContent.HasTopLevelMediaType("image") ? "image" : "resource",
219-
}]
214+
Content = [dataContent.ToContent()]
220215
},
221216
string[] texts => new()
222217
{
223218
Content = [.. texts.Select(x => new Content() { Type = "text", Text = x ?? string.Empty })]
224219
},
225-
226220
IEnumerable<AIContent> contentItems => new()
227221
{
228-
Content = [.. contentItems.Select(static item => item switch
229-
{
230-
TextContent textContent => new Content() { Type = "text", Text = textContent.Text },
231-
DataContent dataContent => new Content()
232-
{
233-
Data = dataContent.GetBase64Data(),
234-
MimeType = dataContent.MediaType,
235-
Type = dataContent.HasTopLevelMediaType("image") ? "image" : "resource",
236-
},
237-
_ => new Content() { Type = "text", Text = item.ToString() ?? string.Empty }
238-
})]
222+
Content = [.. contentItems.Select(static item => item.ToContent())]
223+
},
224+
IEnumerable<Content> contents => new()
225+
{
226+
Content = [.. contents]
239227
},
240228

241229
// TODO https://github.com/modelcontextprotocol/csharp-sdk/issues/69:
@@ -250,4 +238,5 @@ public override async Task<CallToolResponse> InvokeAsync(
250238
},
251239
};
252240
}
241+
253242
}

0 commit comments

Comments
 (0)