|
3 | 3 | using ModelContextProtocol.Protocol.Types; |
4 | 4 | using System.Diagnostics.CodeAnalysis; |
5 | 5 | using System.Text.Json; |
6 | | -using System.Text.Json.Nodes; |
7 | 6 | using System.Text.Json.Serialization; |
8 | 7 | using System.Text.Json.Serialization.Metadata; |
9 | 8 |
|
@@ -35,36 +34,12 @@ public static partial class McpJsonUtilities |
35 | 34 | /// Creates default options to use for MCP-related serialization. |
36 | 35 | /// </summary> |
37 | 36 | /// <returns>The configured options.</returns> |
38 | | - [UnconditionalSuppressMessage("AotAnalysis", "IL3050", Justification = "DefaultJsonTypeInfoResolver is only used when reflection-based serialization is enabled")] |
39 | | - [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026", Justification = "DefaultJsonTypeInfoResolver is only used when reflection-based serialization is enabled")] |
40 | 37 | private static JsonSerializerOptions CreateDefaultOptions() |
41 | 38 | { |
42 | | - // If reflection-based serialization is enabled by default, use it, as it's the most permissive in terms of what it can serialize, |
43 | | - // and we want to be flexible in terms of what can be put into the various collections in the object model. |
44 | | - // Otherwise, use the source-generated options to enable trimming and Native AOT. |
45 | | - JsonSerializerOptions options; |
| 39 | + // Copy the configuration from the source generated context. |
| 40 | + JsonSerializerOptions options = new(JsonContext.Default.Options); |
46 | 41 |
|
47 | | - if (JsonSerializer.IsReflectionEnabledByDefault) |
48 | | - { |
49 | | - // Keep in sync with the JsonSourceGenerationOptions attribute on JsonContext below. |
50 | | - options = new(JsonSerializerDefaults.Web) |
51 | | - { |
52 | | - TypeInfoResolver = new DefaultJsonTypeInfoResolver(), |
53 | | - Converters = { new JsonStringEnumConverter() }, |
54 | | - DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, |
55 | | - NumberHandling = JsonNumberHandling.AllowReadingFromString, |
56 | | - }; |
57 | | - } |
58 | | - else |
59 | | - { |
60 | | - // Keep in sync with any additional settings above beyond what's in JsonContext below. |
61 | | - options = new(JsonContext.Default.Options) |
62 | | - { |
63 | | - }; |
64 | | - } |
65 | | - |
66 | | - // Include all types from AIJsonUtilities, so that anything default usable as part of an AIFunction |
67 | | - // is also usable as part of an McpServerTool. |
| 42 | + // Chain with all supported types from MEAI |
68 | 43 | options.TypeInfoResolverChain.Add(AIJsonUtilities.DefaultOptions.TypeInfoResolver!); |
69 | 44 |
|
70 | 45 | options.MakeReadOnly(); |
@@ -106,11 +81,6 @@ internal static bool IsValidMcpToolSchema(JsonElement element) |
106 | 81 | UseStringEnumConverter = true, |
107 | 82 | DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, |
108 | 83 | NumberHandling = JsonNumberHandling.AllowReadingFromString)] |
109 | | - |
110 | | - // JSON |
111 | | - [JsonSerializable(typeof(JsonDocument))] |
112 | | - [JsonSerializable(typeof(JsonElement))] |
113 | | - [JsonSerializable(typeof(JsonNode))] |
114 | 84 |
|
115 | 85 | // JSON-RPC |
116 | 86 | [JsonSerializable(typeof(IJsonRpcMessage))] |
|
0 commit comments