Skip to content

Commit 0db0364

Browse files
Copilotstephentoub
andcommitted
Make LoggingMessageNotificationParams.Data required per MCP spec
The MCP schema defines `data` as required in LoggingMessageNotification params. Change from `JsonElement?` (nullable, optional) to `required JsonElement` (non-nullable, required) to match the spec. JsonElement remains the correct type (not JsonObject) because the spec defines data as typeless (any JSON-serializable value), not restricted to JSON objects. Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
1 parent 965135b commit 0db0364

3 files changed

Lines changed: 8 additions & 9 deletions

File tree

src/ModelContextProtocol.Core/Protocol/LoggingMessageNotificationParams.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ public sealed class LoggingMessageNotificationParams : NotificationParams
4545
public string? Logger { get; set; }
4646

4747
/// <summary>
48-
/// Gets or sets the data to be logged, such as a string message.
48+
/// Gets or sets the data to be logged, such as a string message or an object.
49+
/// Any JSON serializable type is allowed here.
4950
/// </summary>
5051
[JsonPropertyName("data")]
51-
public JsonElement? Data { get; set; }
52+
public required JsonElement Data { get; set; }
5253
}

tests/ModelContextProtocol.Tests/Client/McpClientTests.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -539,11 +539,9 @@ public async Task AsClientLoggerProvider_MessagesSentToClient()
539539
{
540540
var m = await channel.Reader.ReadAsync(TestContext.Current.CancellationToken);
541541
Assert.NotNull(m);
542-
Assert.NotNull(m.Data);
543-
544542
Assert.Equal("TestLogger", m.Logger);
545543

546-
string? s = JsonSerializer.Deserialize<string>(m.Data.Value, McpJsonUtilities.DefaultOptions);
544+
string? s = JsonSerializer.Deserialize<string>(m.Data, McpJsonUtilities.DefaultOptions);
547545
Assert.NotNull(s);
548546

549547
if (s.Contains("Information"))

tests/ModelContextProtocol.Tests/Protocol/LoggingMessageNotificationParamsTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ public static void LoggingMessageNotificationParams_SerializationRoundTrip_Prese
2323
Assert.NotNull(deserialized);
2424
Assert.Equal(LoggingLevel.Warning, deserialized.Level);
2525
Assert.Equal("MyApp.Services", deserialized.Logger);
26-
Assert.NotNull(deserialized.Data);
27-
Assert.Equal("Something went wrong", deserialized.Data.Value.GetString());
26+
Assert.Equal("Something went wrong", deserialized.Data.GetString());
2827
Assert.NotNull(deserialized.Meta);
2928
Assert.Equal("value", (string)deserialized.Meta["key"]!);
3029
}
@@ -34,7 +33,8 @@ public static void LoggingMessageNotificationParams_SerializationRoundTrip_WithM
3433
{
3534
var original = new LoggingMessageNotificationParams
3635
{
37-
Level = LoggingLevel.Error
36+
Level = LoggingLevel.Error,
37+
Data = JsonDocument.Parse("\"error occurred\"").RootElement.Clone(),
3838
};
3939

4040
string json = JsonSerializer.Serialize(original, McpJsonUtilities.DefaultOptions);
@@ -43,7 +43,7 @@ public static void LoggingMessageNotificationParams_SerializationRoundTrip_WithM
4343
Assert.NotNull(deserialized);
4444
Assert.Equal(LoggingLevel.Error, deserialized.Level);
4545
Assert.Null(deserialized.Logger);
46-
Assert.Null(deserialized.Data);
46+
Assert.Equal("error occurred", deserialized.Data.GetString());
4747
Assert.Null(deserialized.Meta);
4848
}
4949
}

0 commit comments

Comments
 (0)