diff --git a/src/ModelContextProtocol/Configuration/McpServerBuilderExtensions.cs b/src/ModelContextProtocol/Configuration/McpServerBuilderExtensions.cs
index d685cc4..521c4a1 100644
--- a/src/ModelContextProtocol/Configuration/McpServerBuilderExtensions.cs
+++ b/src/ModelContextProtocol/Configuration/McpServerBuilderExtensions.cs
@@ -143,6 +143,7 @@ public static partial class McpServerBuilderExtensions
/// Adds instances to the service collection backing .
/// The prompt type.
/// The builder instance.
+ /// The serializer options governing prompt parameter marshalling.
/// The builder provided in .
/// is .
///
@@ -154,7 +155,8 @@ public static partial class McpServerBuilderExtensions
DynamicallyAccessedMemberTypes.PublicMethods |
DynamicallyAccessedMemberTypes.NonPublicMethods |
DynamicallyAccessedMemberTypes.PublicConstructors)] TPromptType>(
- this IMcpServerBuilder builder)
+ this IMcpServerBuilder builder,
+ JsonSerializerOptions? serializerOptions = null)
{
Throw.IfNull(builder);
@@ -163,8 +165,8 @@ public static partial class McpServerBuilderExtensions
if (promptMethod.GetCustomAttribute() is not null)
{
builder.Services.AddSingleton((Func)(promptMethod.IsStatic ?
- services => McpServerPrompt.Create(promptMethod, options: new() { Services = services }) :
- services => McpServerPrompt.Create(promptMethod, typeof(TPromptType), new() { Services = services })));
+ services => McpServerPrompt.Create(promptMethod, options: new() { Services = services, SerializerOptions = serializerOptions }) :
+ services => McpServerPrompt.Create(promptMethod, typeof(TPromptType), new() { Services = services, SerializerOptions = serializerOptions })));
}
}
@@ -174,6 +176,7 @@ public static partial class McpServerBuilderExtensions
/// Adds instances to the service collection backing .
/// The builder instance.
/// Types with marked methods to add as prompts to the server.
+ /// The serializer options governing prompt parameter marshalling.
/// The builder provided in .
/// is .
/// is .
@@ -183,7 +186,7 @@ public static partial class McpServerBuilderExtensions
/// instance for each. For instance methods, an instance will be constructed for each invocation of the prompt.
///
[RequiresUnreferencedCode(WithPromptsRequiresUnreferencedCodeMessage)]
- public static IMcpServerBuilder WithPrompts(this IMcpServerBuilder builder, params IEnumerable promptTypes)
+ public static IMcpServerBuilder WithPrompts(this IMcpServerBuilder builder, IEnumerable promptTypes, JsonSerializerOptions? serializerOptions = null)
{
Throw.IfNull(builder);
Throw.IfNull(promptTypes);
@@ -197,8 +200,8 @@ public static partial class McpServerBuilderExtensions
if (promptMethod.GetCustomAttribute() is not null)
{
builder.Services.AddSingleton((Func)(promptMethod.IsStatic ?
- services => McpServerPrompt.Create(promptMethod, options: new() { Services = services }) :
- services => McpServerPrompt.Create(promptMethod, promptType, new() { Services = services })));
+ services => McpServerPrompt.Create(promptMethod, options: new() { Services = services, SerializerOptions = serializerOptions }) :
+ services => McpServerPrompt.Create(promptMethod, promptType, new() { Services = services, SerializerOptions = serializerOptions })));
}
}
}
@@ -211,6 +214,7 @@ public static partial class McpServerBuilderExtensions
/// Adds types marked with the attribute from the given assembly as prompts to the server.
///
/// The builder instance.
+ /// The serializer options governing prompt parameter marshalling.
/// The assembly to load the types from. If , the calling assembly will be used.
/// The builder provided in .
/// is .
@@ -235,7 +239,7 @@ public static partial class McpServerBuilderExtensions
///
///
[RequiresUnreferencedCode(WithPromptsRequiresUnreferencedCodeMessage)]
- public static IMcpServerBuilder WithPromptsFromAssembly(this IMcpServerBuilder builder, Assembly? promptAssembly = null)
+ public static IMcpServerBuilder WithPromptsFromAssembly(this IMcpServerBuilder builder, Assembly? promptAssembly = null, JsonSerializerOptions? serializerOptions = null)
{
Throw.IfNull(builder);
@@ -244,7 +248,8 @@ public static partial class McpServerBuilderExtensions
return builder.WithPrompts(
from t in promptAssembly.GetTypes()
where t.GetCustomAttribute() is not null
- select t);
+ select t,
+ serializerOptions);
}
#endregion
diff --git a/src/ModelContextProtocol/Protocol/Types/ContextInclusion.cs b/src/ModelContextProtocol/Protocol/Types/ContextInclusion.cs
index 161b284..e10791e 100644
--- a/src/ModelContextProtocol/Protocol/Types/ContextInclusion.cs
+++ b/src/ModelContextProtocol/Protocol/Types/ContextInclusion.cs
@@ -1,4 +1,5 @@
-using System.Text.Json.Serialization;
+using ModelContextProtocol.Utils.Json;
+using System.Text.Json.Serialization;
namespace ModelContextProtocol.Protocol.Types;
diff --git a/src/ModelContextProtocol/Protocol/Types/LoggingLevel.cs b/src/ModelContextProtocol/Protocol/Types/LoggingLevel.cs
index c57c4c4..7bc4355 100644
--- a/src/ModelContextProtocol/Protocol/Types/LoggingLevel.cs
+++ b/src/ModelContextProtocol/Protocol/Types/LoggingLevel.cs
@@ -1,4 +1,5 @@
-using System.Text.Json.Serialization;
+using ModelContextProtocol.Utils.Json;
+using System.Text.Json.Serialization;
namespace ModelContextProtocol.Protocol.Types;
diff --git a/src/ModelContextProtocol/Protocol/Types/Role.cs b/src/ModelContextProtocol/Protocol/Types/Role.cs
index de2eaae..5b6ee9c 100644
--- a/src/ModelContextProtocol/Protocol/Types/Role.cs
+++ b/src/ModelContextProtocol/Protocol/Types/Role.cs
@@ -1,3 +1,4 @@
+using ModelContextProtocol.Utils.Json;
using System.Text.Json.Serialization;
namespace ModelContextProtocol.Protocol.Types;
diff --git a/src/ModelContextProtocol/Server/AIFunctionMcpServerPrompt.cs b/src/ModelContextProtocol/Server/AIFunctionMcpServerPrompt.cs
index 78b20ce..7684d62 100644
--- a/src/ModelContextProtocol/Server/AIFunctionMcpServerPrompt.cs
+++ b/src/ModelContextProtocol/Server/AIFunctionMcpServerPrompt.cs
@@ -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()?.Name,
Description = options?.Description,
MarshalResult = static (result, _, cancellationToken) => new ValueTask