Skip to content

Commit 1c145a5

Browse files
Hardcode the source generator in one remaining location and simplify McpJsonUtilities. (#204)
1 parent a04cf6a commit 1c145a5

File tree

3 files changed

+32
-34
lines changed

3 files changed

+32
-34
lines changed

src/ModelContextProtocol/Protocol/Transport/SseResponseStreamTransport.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ private void WriteJsonRpcMessageToBuffer(SseItem<IJsonRpcMessage?> item, IBuffer
5353
return;
5454
}
5555

56-
JsonSerializer.Serialize(GetUtf8JsonWriter(writer), item.Data, McpJsonUtilities.DefaultOptions.GetTypeInfo<IJsonRpcMessage?>());
56+
JsonSerializer.Serialize(GetUtf8JsonWriter(writer), item.Data, McpJsonUtilities.JsonContext.Default.IJsonRpcMessage);
5757
}
5858

5959
/// <inheritdoc/>

src/ModelContextProtocol/Utils/Json/McpJsonUtilities.cs

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using ModelContextProtocol.Protocol.Types;
44
using System.Diagnostics.CodeAnalysis;
55
using System.Text.Json;
6-
using System.Text.Json.Nodes;
76
using System.Text.Json.Serialization;
87
using System.Text.Json.Serialization.Metadata;
98

@@ -35,36 +34,12 @@ public static partial class McpJsonUtilities
3534
/// Creates default options to use for MCP-related serialization.
3635
/// </summary>
3736
/// <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")]
4037
private static JsonSerializerOptions CreateDefaultOptions()
4138
{
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);
4641

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
6843
options.TypeInfoResolverChain.Add(AIJsonUtilities.DefaultOptions.TypeInfoResolver!);
6944

7045
options.MakeReadOnly();
@@ -106,11 +81,6 @@ internal static bool IsValidMcpToolSchema(JsonElement element)
10681
UseStringEnumConverter = true,
10782
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
10883
NumberHandling = JsonNumberHandling.AllowReadingFromString)]
109-
110-
// JSON
111-
[JsonSerializable(typeof(JsonDocument))]
112-
[JsonSerializable(typeof(JsonElement))]
113-
[JsonSerializable(typeof(JsonNode))]
11484

11585
// JSON-RPC
11686
[JsonSerializable(typeof(IJsonRpcMessage))]
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using ModelContextProtocol.Utils.Json;
2+
using System.Text.Json;
3+
4+
namespace ModelContextProtocol.Tests;
5+
6+
public static class McpJsonUtilitiesTests
7+
{
8+
[Fact]
9+
public static void DefaultOptions_IsSingleton()
10+
{
11+
var options = McpJsonUtilities.DefaultOptions;
12+
13+
Assert.NotNull(options);
14+
Assert.True(options.IsReadOnly);
15+
Assert.Same(options, McpJsonUtilities.DefaultOptions);
16+
}
17+
18+
[Fact]
19+
public static void DefaultOptions_UseReflectionWhenEnabled()
20+
{
21+
var options = McpJsonUtilities.DefaultOptions;
22+
bool isReflectionEnabled = JsonSerializer.IsReflectionEnabledByDefault;
23+
Type anonType = new { Id = 42 }.GetType();
24+
25+
Assert.True(isReflectionEnabled); // To be disabled once https://github.com/dotnet/extensions/pull/6241 is incorporated
26+
Assert.Equal(isReflectionEnabled, options.TryGetTypeInfo(anonType, out _));
27+
}
28+
}

0 commit comments

Comments
 (0)