|
| 1 | +using System.Text.Json; |
| 2 | +using System.Text.Json.Serialization; |
| 3 | +using ModelContextProtocol.Protocol; |
| 4 | + |
| 5 | +namespace ModelContextProtocol.Tests; |
| 6 | + |
| 7 | +/// <summary> |
| 8 | +/// Validates that the internal property pattern used for experimental properties |
| 9 | +/// produces the expected serialization behavior for SDK consumers using source generators. |
| 10 | +/// </summary> |
| 11 | +/// <remarks> |
| 12 | +/// <para> |
| 13 | +/// Experimental properties (e.g. <see cref="Tool.Execution"/>, <see cref="ServerCapabilities.Tasks"/>) |
| 14 | +/// use an internal <c>*Core</c> property for serialization. A consumer's source-generated |
| 15 | +/// <see cref="JsonSerializerContext"/> cannot see internal members, so experimental data is |
| 16 | +/// silently dropped unless the consumer chains the SDK's resolver into their options. |
| 17 | +/// </para> |
| 18 | +/// <para> |
| 19 | +/// These tests depend on <see cref="Tool.Execution"/> and <see cref="ServerCapabilities.Tasks"/> |
| 20 | +/// being experimental. When those APIs stabilize, update these tests to reference whatever |
| 21 | +/// experimental properties exist at that time, or remove them entirely if no experimental |
| 22 | +/// APIs remain. |
| 23 | +/// </para> |
| 24 | +/// </remarks> |
| 25 | +public class ExperimentalPropertySerializationTests |
| 26 | +{ |
| 27 | + [Fact] |
| 28 | + public void ExperimentalProperties_Dropped_WithConsumerContextOnly() |
| 29 | + { |
| 30 | + var options = new JsonSerializerOptions |
| 31 | + { |
| 32 | + TypeInfoResolverChain = { ConsumerJsonContext.Default } |
| 33 | + }; |
| 34 | + |
| 35 | + var tool = new Tool |
| 36 | + { |
| 37 | + Name = "test-tool", |
| 38 | + Execution = new ToolExecution { TaskSupport = ToolTaskSupport.Optional } |
| 39 | + }; |
| 40 | + |
| 41 | + string json = JsonSerializer.Serialize(tool, options); |
| 42 | + Assert.DoesNotContain("\"execution\"", json); |
| 43 | + Assert.Contains("\"name\"", json); |
| 44 | + } |
| 45 | + |
| 46 | + [Fact] |
| 47 | + public void ExperimentalProperties_IgnoredOnDeserialize_WithConsumerContextOnly() |
| 48 | + { |
| 49 | + string json = JsonSerializer.Serialize( |
| 50 | + new Tool |
| 51 | + { |
| 52 | + Name = "test-tool", |
| 53 | + Execution = new ToolExecution { TaskSupport = ToolTaskSupport.Optional } |
| 54 | + }, |
| 55 | + McpJsonUtilities.DefaultOptions); |
| 56 | + Assert.Contains("\"execution\"", json); |
| 57 | + |
| 58 | + var options = new JsonSerializerOptions |
| 59 | + { |
| 60 | + TypeInfoResolverChain = { ConsumerJsonContext.Default } |
| 61 | + }; |
| 62 | + var deserialized = JsonSerializer.Deserialize<Tool>(json, options)!; |
| 63 | + Assert.Equal("test-tool", deserialized.Name); |
| 64 | + Assert.Null(deserialized.Execution); |
| 65 | + } |
| 66 | + |
| 67 | + [Fact] |
| 68 | + public void ExperimentalProperties_RoundTrip_WhenSdkResolverIsChained() |
| 69 | + { |
| 70 | + var options = new JsonSerializerOptions |
| 71 | + { |
| 72 | + TypeInfoResolverChain = |
| 73 | + { |
| 74 | + McpJsonUtilities.DefaultOptions.TypeInfoResolver!, |
| 75 | + ConsumerJsonContext.Default, |
| 76 | + } |
| 77 | + }; |
| 78 | + |
| 79 | + var tool = new Tool |
| 80 | + { |
| 81 | + Name = "test-tool", |
| 82 | + Execution = new ToolExecution { TaskSupport = ToolTaskSupport.Optional } |
| 83 | + }; |
| 84 | + |
| 85 | + string json = JsonSerializer.Serialize(tool, options); |
| 86 | + Assert.Contains("\"execution\"", json); |
| 87 | + Assert.Contains("\"name\"", json); |
| 88 | + |
| 89 | + var deserialized = JsonSerializer.Deserialize<Tool>(json, options)!; |
| 90 | + Assert.Equal("test-tool", deserialized.Name); |
| 91 | + Assert.NotNull(deserialized.Execution); |
| 92 | + Assert.Equal(ToolTaskSupport.Optional, deserialized.Execution.TaskSupport); |
| 93 | + } |
| 94 | + |
| 95 | + [Fact] |
| 96 | + public void ExperimentalProperties_RoundTrip_WithDefaultOptions() |
| 97 | + { |
| 98 | + var capabilities = new ServerCapabilities |
| 99 | + { |
| 100 | + Tasks = new McpTasksCapability() |
| 101 | + }; |
| 102 | + |
| 103 | + string json = JsonSerializer.Serialize(capabilities, McpJsonUtilities.DefaultOptions); |
| 104 | + Assert.Contains("\"tasks\"", json); |
| 105 | + |
| 106 | + var deserialized = JsonSerializer.Deserialize<ServerCapabilities>(json, McpJsonUtilities.DefaultOptions)!; |
| 107 | + Assert.NotNull(deserialized.Tasks); |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +[JsonSerializable(typeof(Tool))] |
| 112 | +[JsonSerializable(typeof(ServerCapabilities))] |
| 113 | +[JsonSerializable(typeof(ClientCapabilities))] |
| 114 | +[JsonSerializable(typeof(CallToolResult))] |
| 115 | +[JsonSerializable(typeof(CallToolRequestParams))] |
| 116 | +[JsonSerializable(typeof(CreateMessageRequestParams))] |
| 117 | +[JsonSerializable(typeof(ElicitRequestParams))] |
| 118 | +internal partial class ConsumerJsonContext : JsonSerializerContext; |
0 commit comments