Skip to content

Commit c4d1500

Browse files
Copilotstephentoub
andcommitted
Simplify error data deserialization to handle only JsonElement
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
1 parent f34c302 commit c4d1500

1 file changed

Lines changed: 5 additions & 15 deletions

File tree

src/ModelContextProtocol.Core/McpSessionHandler.cs

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -455,23 +455,13 @@ public async Task<JsonRpcResponse> SendRequestAsync(JsonRpcRequest request, Canc
455455
LogSendingRequestFailed(EndpointName, request.Method, error.Error.Message, error.Error.Code);
456456
var exception = new McpProtocolException($"Request failed (remote): {error.Error.Message}", (McpErrorCode)error.Error.Code);
457457

458-
// Populate exception.Data with the error data if present
459-
if (error.Error.Data is not null)
458+
// Populate exception.Data with the error data if present.
459+
// When deserializing JSON, Data will be a JsonElement.
460+
if (error.Error.Data is JsonElement jsonElement && jsonElement.ValueKind == JsonValueKind.Object)
460461
{
461-
// The data could be a Dictionary<string, JsonElement> or a JsonElement containing an object
462-
if (error.Error.Data is Dictionary<string, JsonElement> dataDict)
462+
foreach (var property in jsonElement.EnumerateObject())
463463
{
464-
foreach (var kvp in dataDict)
465-
{
466-
exception.Data[kvp.Key] = kvp.Value;
467-
}
468-
}
469-
else if (error.Error.Data is JsonElement jsonElement && jsonElement.ValueKind == JsonValueKind.Object)
470-
{
471-
foreach (var property in jsonElement.EnumerateObject())
472-
{
473-
exception.Data[property.Name] = property.Value;
474-
}
464+
exception.Data[property.Name] = property.Value;
475465
}
476466
}
477467

0 commit comments

Comments
 (0)