@@ -10,11 +10,6 @@ namespace ModelContextProtocol.Tests;
1010/// <summary>
1111/// Tests for McpProtocolException.Data propagation to JSON-RPC error responses.
1212/// </summary>
13- /// <remarks>
14- /// Note: On .NET Framework, Exception.Data requires values to be serializable with [Serializable].
15- /// Since JsonElement is not marked as serializable, the data population on the client side is skipped
16- /// on that platform. These tests verify the error message and code are correct regardless of platform.
17- /// </remarks>
1813public class McpProtocolExceptionDataTests : ClientServerTestBase
1914{
2015 public McpProtocolExceptionDataTests ( ITestOutputHelper testOutputHelper )
@@ -68,6 +63,20 @@ protected override void ConfigureServices(ServiceCollection services, IMcpServer
6863 } ) ;
6964 }
7065
66+ /// <summary>
67+ /// Gets the JsonElement from an Exception.Data value, handling both direct JsonElement (on .NET Core)
68+ /// and SerializableJsonElement wrapper (on .NET Framework).
69+ /// </summary>
70+ private static JsonElement GetJsonElement ( object ? value )
71+ {
72+ return value switch
73+ {
74+ JsonElement je => je ,
75+ SerializableJsonElement sje => sje . Value ,
76+ _ => throw new InvalidOperationException ( $ "Expected JsonElement or SerializableJsonElement, got { value ? . GetType ( ) . Name ?? "null" } ")
77+ } ;
78+ }
79+
7180 [ Fact ]
7281 public async Task Exception_With_Serializable_Data_Propagates_To_Client ( )
7382 {
@@ -78,12 +87,6 @@ public async Task Exception_With_Serializable_Data_Propagates_To_Client()
7887
7988 Assert . Equal ( "Request failed (remote): Resource not found" , exception . Message ) ;
8089 Assert . Equal ( ( McpErrorCode ) ( - 32002 ) , exception . ErrorCode ) ;
81-
82- // Skip data verification on .NET Framework since JsonElement cannot be stored in Exception.Data
83- if ( PlatformDetection . IsNetFramework )
84- {
85- return ;
86- }
8790
8891 // Verify the data was propagated to the exception
8992 // The Data collection should contain the expected keys
@@ -100,11 +103,11 @@ public async Task Exception_With_Serializable_Data_Propagates_To_Client()
100103 Assert . True ( hasUri , "Exception.Data should contain 'uri' key" ) ;
101104 Assert . True ( hasCode , "Exception.Data should contain 'code' key" ) ;
102105
103- // Verify the values (they should be JsonElements)
104- var uriValue = Assert . IsType < JsonElement > ( exception . Data [ "uri" ] ) ;
106+ // Verify the values (they are JsonElements on .NET Core, SerializableJsonElement on .NET Framework )
107+ var uriValue = GetJsonElement ( exception . Data [ "uri" ] ) ;
105108 Assert . Equal ( "file:///path/to/resource" , uriValue . GetString ( ) ) ;
106109
107- var codeValue = Assert . IsType < JsonElement > ( exception . Data [ "code" ] ) ;
110+ var codeValue = GetJsonElement ( exception . Data [ "code" ] ) ;
108111 Assert . Equal ( 404 , codeValue . GetInt32 ( ) ) ;
109112 }
110113
@@ -121,12 +124,6 @@ public async Task Exception_With_NonSerializable_Data_Still_Propagates_Error_To_
121124
122125 Assert . Equal ( "Request failed (remote): Resource not found" , exception . Message ) ;
123126 Assert . Equal ( ( McpErrorCode ) ( - 32002 ) , exception . ErrorCode ) ;
124-
125- // Skip data verification on .NET Framework since JsonElement cannot be stored in Exception.Data
126- if ( PlatformDetection . IsNetFramework )
127- {
128- return ;
129- }
130127
131128 // Verify that only the serializable data was propagated (non-serializable was filtered out)
132129 var hasUri = false ;
@@ -142,7 +139,7 @@ public async Task Exception_With_NonSerializable_Data_Still_Propagates_Error_To_
142139 Assert . True ( hasUri , "Exception.Data should contain 'uri' key" ) ;
143140 Assert . False ( hasNonSerializable , "Exception.Data should not contain 'nonSerializable' key" ) ;
144141
145- var uriValue = Assert . IsType < JsonElement > ( exception . Data [ "uri" ] ) ;
142+ var uriValue = GetJsonElement ( exception . Data [ "uri" ] ) ;
146143 Assert . Equal ( "file:///path/to/resource" , uriValue . GetString ( ) ) ;
147144 }
148145
0 commit comments