Skip to content

Commit bb87ddf

Browse files
authored
LoggingMessageNotification#data accepts non-String payload (modelcontextprotocol#1201)
Fixes modelcontextprotocol#1180 Signed-off-by: Daniel Garnier-Moiroux <[email protected]>
1 parent 7fc1c32 commit bb87ddf

3 files changed

Lines changed: 88 additions & 2 deletions

File tree

‎mcp-core/src/main/java/io/modelcontextprotocol/spec/McpSchema.java‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5355,7 +5355,10 @@ static ResourcesUpdatedNotification fromJson(@JsonProperty("uri") String uri,
53555355
*
53565356
* @param level The severity levels. The minimum log level is set by the client.
53575357
* @param logger The logger that generated the message.
5358-
* @param data JSON-serializable logging data.
5358+
* @param data The logging data, as a string. The MCP specification allows any JSON
5359+
* value; when a non-string value is received, it is converted with
5360+
* {@link Object#toString()}. The resulting format is intended for human readers and
5361+
* is not guaranteed to be valid JSON.
53595362
* @param meta See specification for notes on _meta usage
53605363
* <p>
53615364
* Note: {@code level} and {@code data} are required by the MCP specification.
@@ -5377,8 +5380,9 @@ public record LoggingMessageNotification( // @formatter:off
53775380

53785381
@JsonCreator
53795382
static LoggingMessageNotification fromJson(@JsonProperty("level") LoggingLevel level,
5380-
@JsonProperty("logger") String loggerName, @JsonProperty("data") String data,
5383+
@JsonProperty("logger") String loggerName, @JsonProperty("data") Object rawData,
53815384
@JsonProperty("_meta") Map<String, Object> meta) {
5385+
String data = rawData != null ? rawData.toString() : null;
53825386
if (level == null || data == null) {
53835387
List<String> missing = new ArrayList<>();
53845388
if (level == null) {

‎mcp-test/src/test/java/io/modelcontextprotocol/client/McpAsyncClientResponseHandlerTests.java‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,4 +559,31 @@ void testPingMessageRequestHandling() {
559559
asyncMcpClient.closeGracefully();
560560
}
561561

562+
@Test
563+
void testLoggingNotificationWithObjectData() throws IOException {
564+
MockMcpClientTransport transport = initializationEnabledTransport();
565+
566+
List<McpSchema.LoggingMessageNotification> receivedNotifications = new ArrayList<>();
567+
568+
McpAsyncClient asyncMcpClient = McpClient.async(transport)
569+
.loggingConsumer(notification -> Mono.fromRunnable(() -> receivedNotifications.add(notification)))
570+
.build();
571+
572+
assertThat(asyncMcpClient.initialize().block()).isNotNull();
573+
574+
// Logging notification as sent by FastMCP servers, with an object as data
575+
McpSchema.JSONRPCMessage notification = McpSchema.deserializeJsonRpcMessage(JSON_MAPPER, """
576+
{"jsonrpc":"2.0","method":"notifications/message",
577+
"params":{"level":"info","logger":"server","data":{"msg":"working","extra":null}}}""");
578+
transport.simulateIncomingMessage(notification);
579+
580+
assertThat(receivedNotifications).singleElement().satisfies(received -> {
581+
assertThat(received.level()).isEqualTo(McpSchema.LoggingLevel.INFO);
582+
assertThat(received.logger()).isEqualTo("server");
583+
assertThat(received.data()).isEqualTo("{msg=working, extra=null}");
584+
});
585+
586+
asyncMcpClient.closeGracefully();
587+
}
588+
562589
}

‎mcp-test/src/test/java/io/modelcontextprotocol/spec/McpSchemaTests.java‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.assertj.core.api.InstanceOfAssertFactories;
1818
import org.junit.jupiter.api.Test;
1919
import org.junit.jupiter.params.ParameterizedTest;
20+
import org.junit.jupiter.params.provider.CsvSource;
2021
import org.junit.jupiter.params.provider.NullSource;
2122
import org.junit.jupiter.params.provider.ValueSource;
2223

@@ -2729,6 +2730,60 @@ void testLoggingMessageNotificationDeserializationWithMissingRequiredFields() th
27292730
assertThat(notification.data()).isEmpty();
27302731
}
27312732

2733+
@Test
2734+
void testLoggingMessageNotificationDeserializationWithNullData() throws Exception {
2735+
McpSchema.LoggingMessageNotification notification = JSON_MAPPER.readValue("""
2736+
{"level":"info","data":null}""", McpSchema.LoggingMessageNotification.class);
2737+
2738+
assertThat(notification.data()).isEmpty();
2739+
}
2740+
2741+
@Test
2742+
void testLoggingMessageNotificationDeserializationWithStringData() throws Exception {
2743+
McpSchema.LoggingMessageNotification notification = JSON_MAPPER.readValue("""
2744+
{"level":"warning","logger":"my-logger","data":"plain text"}""",
2745+
McpSchema.LoggingMessageNotification.class);
2746+
2747+
assertThat(notification.level()).isEqualTo(McpSchema.LoggingLevel.WARNING);
2748+
assertThat(notification.logger()).isEqualTo("my-logger");
2749+
assertThat(notification.data()).isEqualTo("plain text");
2750+
}
2751+
2752+
@Test
2753+
void testLoggingMessageNotificationDeserializationWithObjectData() throws Exception {
2754+
// FastMCP sends {"msg": ..., "extra": ...} for every ctx.log() call
2755+
McpSchema.LoggingMessageNotification notification = JSON_MAPPER.readValue("""
2756+
{
2757+
"level": "info",
2758+
"logger": "server",
2759+
"data": {
2760+
"msg": "working",
2761+
"extra": {
2762+
"user": "bob",
2763+
"ids": [1, 2 ]
2764+
}
2765+
}
2766+
}""", McpSchema.LoggingMessageNotification.class);
2767+
2768+
assertThat(notification.level()).isEqualTo(McpSchema.LoggingLevel.INFO);
2769+
assertThat(notification.logger()).isEqualTo("server");
2770+
assertThat(notification.data()).isEqualTo("{msg=working, extra={user=bob, ids=[1, 2]}}");
2771+
}
2772+
2773+
@ParameterizedTest
2774+
@CsvSource(delimiter = '|', textBlock = """
2775+
[1,"two",true] | [1, two, true]
2776+
42 | 42
2777+
1.5 | 1.5
2778+
true | true
2779+
""")
2780+
void testLoggingMessageNotificationDeserializationWithNonStringData(String data, String expected) throws Exception {
2781+
McpSchema.LoggingMessageNotification notification = JSON_MAPPER.readValue("""
2782+
{"level":"debug","data":%s}""".formatted(data), McpSchema.LoggingMessageNotification.class);
2783+
2784+
assertThat(notification.data()).isEqualTo(expected);
2785+
}
2786+
27322787
// --- Icon tests (SEP-973) ---
27332788

27342789
@Test

0 commit comments

Comments
 (0)