Expose a JsonSerializerOptions setting in the prompts APIs (#279)

* Expose a JsonSerializerOptions setting in the prompts APIs

* Change namespace for workaround converter.
This commit is contained in:
Eirik Tsarpalis 2025-04-11 18:02:21 +01:00 • committed by GitHub
commit ce4e6586d2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 54 additions and 19 deletions

View file

@ -2,6 +2,7 @@ using Microsoft.Extensions.AI;
using Microsoft.Extensions.DependencyInjection;
using ModelContextProtocol.Protocol.Types;
using ModelContextProtocol.Utils;
using ModelContextProtocol.Utils.Json;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Text.Json;
@ -66,6 +67,7 @@ internal sealed class AIFunctionMcpServerPrompt : McpServerPrompt
Name = options?.Name ?? method.GetCustomAttribute<McpServerPromptAttribute>()?.Name,
Description = options?.Description,
MarshalResult = static (result, _, cancellationToken) => new ValueTask<object?>(result),
SerializerOptions = options?.SerializerOptions ?? McpJsonUtilities.DefaultOptions,
ConfigureParameterBinding = pi =>
{
if (pi.ParameterType == typeof(RequestContext<GetPromptRequestParams>))
@ -136,6 +138,10 @@ internal sealed class AIFunctionMcpServerPrompt : McpServerPrompt
Throw.IfNull(function);
List<PromptArgument> args = [];
HashSet<string>? requiredProps = function.JsonSchema.TryGetProperty("required", out JsonElement required)
? new(required.EnumerateArray().Select(p => p.GetString()!), StringComparer.Ordinal)
: null;
if (function.JsonSchema.TryGetProperty("properties", out JsonElement properties))
{
foreach (var param in properties.EnumerateObject())
@ -144,7 +150,7 @@ internal sealed class AIFunctionMcpServerPrompt : McpServerPrompt
{
Name = param.Name,
Description = param.Value.TryGetProperty("description", out JsonElement description) ? description.GetString() : null,
Required = param.Value.TryGetProperty("required", out JsonElement required) && required.GetBoolean(),
Required = requiredProps?.Contains(param.Name) ?? false,
});
}
}

View file

@ -1,4 +1,6 @@
using ModelContextProtocol.Utils.Json;
using System.ComponentModel;
using System.Text.Json;
namespace ModelContextProtocol.Server;
@ -45,14 +47,23 @@ public sealed class McpServerPromptCreateOptions
/// </remarks>
public string? Description { get; set; }
/// <summary>
/// Gets or sets the JSON serializer options to use when marshalling data to/from JSON.
/// </summary>
/// <remarks>
/// Defaults to <see cref="McpJsonUtilities.DefaultOptions"/> if left unspecified.
/// </remarks>
public JsonSerializerOptions? SerializerOptions { get; set; }
/// <summary>
/// Creates a shallow clone of the current <see cref="McpServerPromptCreateOptions"/> instance.
/// </summary>
internal McpServerPromptCreateOptions Clone() =>
new McpServerPromptCreateOptions()
new McpServerPromptCreateOptions
{
Services = Services,
Name = Name,
Description = Description,
SerializerOptions = SerializerOptions,
};
}