Skip to content

Commit a3cb705

Browse files
Copilotstephentoub
andcommitted
Add optional JsonSerializerOptions parameter to ToChatMessage
- Updated ToChatMessage to accept optional JsonSerializerOptions parameter - Changed from using JsonContext.Default.CallToolResult to options.GetTypeInfo<CallToolResult>() - Ensures user-defined serialization options are used throughout the call chain - Defaults to McpJsonUtilities.DefaultOptions when no options provided - All tests passing on .NET 8, 9, and 10 Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
1 parent cb95813 commit a3cb705

1 file changed

Lines changed: 5 additions & 2 deletions

File tree

src/ModelContextProtocol.Core/AIContentExtensions.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ public static ChatMessage ToChatMessage(this PromptMessage promptMessage)
184184
/// </summary>
185185
/// <param name="result">The tool result to convert.</param>
186186
/// <param name="callId">The identifier for the function call request that triggered the tool invocation.</param>
187+
/// <param name="options">The <see cref="JsonSerializerOptions"/> to use for serialization. If <see langword="null"/>, <see cref="McpJsonUtilities.DefaultOptions"/> is used.</param>
187188
/// <returns>A <see cref="ChatMessage"/> object created from the tool result.</returns>
188189
/// <remarks>
189190
/// This method transforms a protocol-specific <see cref="CallToolResult"/> from the Model Context Protocol
@@ -192,12 +193,14 @@ public static ChatMessage ToChatMessage(this PromptMessage promptMessage)
192193
/// serialized <see cref="JsonElement"/>.
193194
/// </remarks>
194195
/// <exception cref="ArgumentNullException"><paramref name="result"/> or <paramref name="callId"/> is <see langword="null"/>.</exception>
195-
public static ChatMessage ToChatMessage(this CallToolResult result, string callId)
196+
public static ChatMessage ToChatMessage(this CallToolResult result, string callId, JsonSerializerOptions? options = null)
196197
{
197198
Throw.IfNull(result);
198199
Throw.IfNull(callId);
199200

200-
return new(ChatRole.Tool, [new FunctionResultContent(callId, JsonSerializer.SerializeToElement(result, McpJsonUtilities.JsonContext.Default.CallToolResult))
201+
options ??= McpJsonUtilities.DefaultOptions;
202+
203+
return new(ChatRole.Tool, [new FunctionResultContent(callId, JsonSerializer.SerializeToElement(result, options.GetTypeInfo<CallToolResult>()))
201204
{
202205
RawRepresentation = result,
203206
}]);

0 commit comments

Comments
 (0)