-
Notifications
You must be signed in to change notification settings - Fork 614
Fix: Handle JsonSerializerOptions without TypeInfoResolver #1203
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -76,7 +76,7 @@ private static AIFunctionFactoryOptions CreateAIFunctionFactoryOptions( | |
| Name = options?.Name ?? method.GetCustomAttribute<McpServerToolAttribute>()?.Name ?? DeriveName(method), | ||
| Description = options?.Description, | ||
| MarshalResult = static (result, _, cancellationToken) => new ValueTask<object?>(result), | ||
| SerializerOptions = options?.SerializerOptions ?? McpJsonUtilities.DefaultOptions, | ||
| SerializerOptions = GetSerializerOptions(options?.SerializerOptions), | ||
| JsonSchemaCreateOptions = options?.SchemaCreateOptions, | ||
| ConfigureParameterBinding = pi => | ||
| { | ||
|
|
@@ -580,4 +580,22 @@ private static CallToolResult ConvertAIContentEnumerableToCallToolResult(IEnumer | |
| IsError = allErrorContent && hasAny | ||
| }; | ||
| } | ||
|
|
||
| private static JsonSerializerOptions GetSerializerOptions(JsonSerializerOptions? customOptions) | ||
| { | ||
| if (customOptions is null) | ||
| { | ||
| return McpJsonUtilities.DefaultOptions; | ||
| } | ||
|
|
||
| if (customOptions.TypeInfoResolver is not null) | ||
| { | ||
| return customOptions; | ||
| } | ||
|
|
||
| customOptions.TypeInfoResolverChain.Add(McpJsonUtilities.JsonContext.Default); | ||
| customOptions.TypeInfoResolverChain.Add(AIJsonUtilities.DefaultOptions.TypeInfoResolver!); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @eiriktsarpalis, is it desirable to mutate the externally-provided JsonSerializerOptions like this? And is this thread-safe? What would happen if this JSO were passed to two McpServerTool.Create calls concurrently?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Νο. And even if it was, it would fail assuming the passed instance were read-only.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @eiriktsarpalis, thanks. What is the right answer for #1150? We should support someone passing in
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's using the copy constructor like the PR was doing originally. That does have the side-effect of creating a new JSO and repopulating caches every time the method is invoked.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How does
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reading #1150 more closely, I believe the user expects that we replicate the semantics of the non-AOT friendly if (options.TypeInfoResolver is null)
{
Debug.Assert(!options.IsReadOnly, "If no resolver is present then the options must still be editable");
options.TypeInfoResolver = McpJsonUtilities.DefaultOptions.TypeInfoResolver;
options.MakeReadOnly();
}
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Just saw this but I think my last response should asnwer your question. |
||
|
|
||
| return customOptions; | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.