Skip to content

Commit 672ca29

Browse files
authored
Make clientId required for @McpLogging annotation (spring-ai-community#33)
- Breaking change! - Change @McpLogging.clientId from optional to required parameter - Add validation in AsyncLoggingSpecification and SyncLoggingSpecification constructors - Update all test examples to include explicit clientId values - Update README documentation to reflect clientId requirement Signed-off-by: Christian Tzolov <[email protected]>
1 parent 352207a commit 672ca29

File tree

10 files changed

+52
-31
lines changed

10 files changed

+52
-31
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ The Spring integration module provides seamless integration with Spring AI and S
111111
### Annotations
112112

113113
#### Client
114-
- **`@McpLogging`** - Annotates methods that handle logging message notifications from MCP servers
114+
- **`@McpLogging`** - Annotates methods that handle logging message notifications from MCP servers (requires `clientId` parameter)
115115
- **`@McpSampling`** - Annotates methods that handle sampling requests from MCP servers
116116
- **`@McpElicitation`** - Annotates methods that handle elicitation requests to gather additional information from users
117117
- **`@McpProgress`** - Annotates methods that handle progress notifications for long-running operations
@@ -931,21 +931,23 @@ public class LoggingHandler {
931931

932932
/**
933933
* Handle logging message notifications with a single parameter.
934+
* Note: clientId is now required for all @McpLogging annotations.
934935
* @param notification The logging message notification
935936
*/
936-
@McpLogging
937+
@McpLogging(clientId = "default-client")
937938
public void handleLoggingMessage(LoggingMessageNotification notification) {
938939
System.out.println("Received logging message: " + notification.level() + " - " + notification.logger() + " - "
939940
+ notification.data());
940941
}
941942

942943
/**
943944
* Handle logging message notifications with individual parameters.
945+
* Note: clientId is now required for all @McpLogging annotations.
944946
* @param level The logging level
945947
* @param logger The logger name
946948
* @param data The log message data
947949
*/
948-
@McpLogging
950+
@McpLogging(clientId = "default-client")
949951
public void handleLoggingMessageWithParams(LoggingLevel level, String logger, String data) {
950952
System.out.println("Received logging message with params: " + level + " - " + logger + " - " + data);
951953
}

mcp-annotations/src/main/java/org/springaicommunity/mcp/annotation/McpLogging.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@
5252
public @interface McpLogging {
5353

5454
/**
55-
* Used as connection or client identifier to select the MCP client, the logging
56-
* consumer is associated with. If not specified, is applied to all clients.
55+
* Used as connection or client identifier to select the MCP clients, the logging
56+
* consumer is associated with. At least one client ID must be specified.
5757
*/
58-
String clientId() default "";
58+
String clientId();
5959

6060
}

mcp-annotations/src/main/java/org/springaicommunity/mcp/method/logging/AsyncLoggingSpecification.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,21 @@
44

55
package org.springaicommunity.mcp.method.logging;
66

7+
import java.util.Objects;
78
import java.util.function.Function;
89

910
import io.modelcontextprotocol.spec.McpSchema.LoggingMessageNotification;
1011
import reactor.core.publisher.Mono;
1112

1213
public record AsyncLoggingSpecification(String clientId,
1314
Function<LoggingMessageNotification, Mono<Void>> loggingHandler) {
15+
16+
public AsyncLoggingSpecification {
17+
Objects.requireNonNull(clientId, "clientId must not be null");
18+
if (clientId.trim().isEmpty()) {
19+
throw new IllegalArgumentException("clientId must not be empty");
20+
}
21+
Objects.requireNonNull(loggingHandler, "loggingHandler must not be null");
22+
}
23+
1424
}

mcp-annotations/src/main/java/org/springaicommunity/mcp/method/logging/SyncLoggingSpecification.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,18 @@
44

55
package org.springaicommunity.mcp.method.logging;
66

7+
import java.util.Objects;
78
import java.util.function.Consumer;
89

910
import io.modelcontextprotocol.spec.McpSchema.LoggingMessageNotification;
1011

1112
public record SyncLoggingSpecification(String clientId, Consumer<LoggingMessageNotification> loggingHandler) {
12-
}
13+
14+
public SyncLoggingSpecification {
15+
Objects.requireNonNull(clientId, "clientId must not be null");
16+
if (clientId.trim().isEmpty()) {
17+
throw new IllegalArgumentException("clientId must not be empty");
18+
}
19+
Objects.requireNonNull(loggingHandler, "loggingHandler must not be null");
20+
}
21+
}

mcp-annotations/src/test/java/org/springaicommunity/mcp/method/logging/AsyncMcpLoggingMethodCallbackExample.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class AsyncMcpLoggingMethodCallbackExample {
2929
* @param notification The logging message notification
3030
* @return A Mono that completes when the processing is done
3131
*/
32-
@McpLogging
32+
@McpLogging(clientId = "test-client")
3333
public Mono<Void> handleLoggingMessage(LoggingMessageNotification notification) {
3434
return Mono.fromRunnable(() -> {
3535
System.out.println("Received logging message: " + notification.level() + " - " + notification.logger()
@@ -45,7 +45,7 @@ public Mono<Void> handleLoggingMessage(LoggingMessageNotification notification)
4545
* @param data The log message data
4646
* @return A Mono that completes when the processing is done
4747
*/
48-
@McpLogging
48+
@McpLogging(clientId = "test-client")
4949
public Mono<Void> handleLoggingMessageWithParams(LoggingLevel level, String logger, String data) {
5050
return Mono.fromRunnable(() -> {
5151
System.out.println("Received logging message with params: " + level + " - " + logger + " - " + data);
@@ -56,7 +56,7 @@ public Mono<Void> handleLoggingMessageWithParams(LoggingLevel level, String logg
5656
* Example method that accepts a LoggingMessageNotification with void return type.
5757
* @param notification The logging message notification
5858
*/
59-
@McpLogging
59+
@McpLogging(clientId = "test-client")
6060
public void handleLoggingMessageVoid(LoggingMessageNotification notification) {
6161
System.out.println("Received logging message (void): " + notification.level() + " - " + notification.logger()
6262
+ " - " + notification.data());

mcp-annotations/src/test/java/org/springaicommunity/mcp/method/logging/AsyncMcpLoggingMethodCallbackTests.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ static class ValidMethods {
4141

4242
private String lastData;
4343

44-
@McpLogging
44+
@McpLogging(clientId = "test-client")
4545
public Mono<Void> handleLoggingMessage(LoggingMessageNotification notification) {
4646
return Mono.fromRunnable(() -> {
4747
this.lastNotification = notification;
4848
});
4949
}
5050

51-
@McpLogging
51+
@McpLogging(clientId = "test-client")
5252
public Mono<Void> handleLoggingMessageWithParams(LoggingLevel level, String logger, String data) {
5353
return Mono.fromRunnable(() -> {
5454
this.lastLevel = level;
@@ -57,7 +57,7 @@ public Mono<Void> handleLoggingMessageWithParams(LoggingLevel level, String logg
5757
});
5858
}
5959

60-
@McpLogging
60+
@McpLogging(clientId = "test-client")
6161
public void handleLoggingMessageVoid(LoggingMessageNotification notification) {
6262
this.lastNotification = notification;
6363
}
@@ -69,27 +69,27 @@ public void handleLoggingMessageVoid(LoggingMessageNotification notification) {
6969
*/
7070
static class InvalidMethods {
7171

72-
@McpLogging
72+
@McpLogging(clientId = "test-client")
7373
public String invalidReturnType(LoggingMessageNotification notification) {
7474
return "Invalid";
7575
}
7676

77-
@McpLogging
77+
@McpLogging(clientId = "test-client")
7878
public Mono<String> invalidMonoReturnType(LoggingMessageNotification notification) {
7979
return Mono.just("Invalid");
8080
}
8181

82-
@McpLogging
82+
@McpLogging(clientId = "test-client")
8383
public Mono<Void> invalidParameterCount(LoggingMessageNotification notification, String extra) {
8484
return Mono.empty();
8585
}
8686

87-
@McpLogging
87+
@McpLogging(clientId = "test-client")
8888
public Mono<Void> invalidParameterType(String invalidType) {
8989
return Mono.empty();
9090
}
9191

92-
@McpLogging
92+
@McpLogging(clientId = "test-client")
9393
public Mono<Void> invalidParameterTypes(String level, int logger, boolean data) {
9494
return Mono.empty();
9595
}

mcp-annotations/src/test/java/org/springaicommunity/mcp/method/logging/SyncMcpLoggingMethodCallbackExample.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class SyncMcpLoggingMethodCallbackExample {
2727
* Example method that accepts a LoggingMessageNotification.
2828
* @param notification The logging message notification
2929
*/
30-
@McpLogging
30+
@McpLogging(clientId = "test-client")
3131
public void handleLoggingMessage(LoggingMessageNotification notification) {
3232
System.out.println("Received logging message: " + notification.level() + " - " + notification.logger() + " - "
3333
+ notification.data());
@@ -39,7 +39,7 @@ public void handleLoggingMessage(LoggingMessageNotification notification) {
3939
* @param logger The logger name
4040
* @param data The log message data
4141
*/
42-
@McpLogging
42+
@McpLogging(clientId = "test-client")
4343
public void handleLoggingMessageWithParams(LoggingLevel level, String logger, String data) {
4444
System.out.println("Received logging message with params: " + level + " - " + logger + " - " + data);
4545
}

mcp-annotations/src/test/java/org/springaicommunity/mcp/method/logging/SyncMcpLoggingMethodCallbackTests.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ static class ValidMethods {
3939

4040
private String lastData;
4141

42-
@McpLogging
42+
@McpLogging(clientId = "test-client")
4343
public void handleLoggingMessage(LoggingMessageNotification notification) {
4444
this.lastNotification = notification;
4545
}
4646

47-
@McpLogging
47+
@McpLogging(clientId = "test-client")
4848
public void handleLoggingMessageWithParams(LoggingLevel level, String logger, String data) {
4949
this.lastLevel = level;
5050
this.lastLogger = logger;
@@ -58,22 +58,22 @@ public void handleLoggingMessageWithParams(LoggingLevel level, String logger, St
5858
*/
5959
static class InvalidMethods {
6060

61-
@McpLogging
61+
@McpLogging(clientId = "test-client")
6262
public String invalidReturnType(LoggingMessageNotification notification) {
6363
return "Invalid";
6464
}
6565

66-
@McpLogging
66+
@McpLogging(clientId = "test-client")
6767
public void invalidParameterCount(LoggingMessageNotification notification, String extra) {
6868
// Invalid parameter count
6969
}
7070

71-
@McpLogging
71+
@McpLogging(clientId = "test-client")
7272
public void invalidParameterType(String invalidType) {
7373
// Invalid parameter type
7474
}
7575

76-
@McpLogging
76+
@McpLogging(clientId = "test-client")
7777
public void invalidParameterTypes(String level, int logger, boolean data) {
7878
// Invalid parameter types
7979
}

mcp-annotations/src/test/java/org/springaicommunity/mcp/provider/logging/AsyncMcpLoggingProviderTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ static class TestAsyncLoggingProvider {
3838

3939
private String lastData;
4040

41-
@McpLogging
41+
@McpLogging(clientId = "test-client")
4242
public Mono<Void> handleLoggingMessage(LoggingMessageNotification notification) {
4343
return Mono.fromRunnable(() -> {
4444
this.lastNotification = notification;
4545
});
4646
}
4747

48-
@McpLogging
48+
@McpLogging(clientId = "test-client")
4949
public Mono<Void> handleLoggingMessageWithParams(LoggingLevel level, String logger, String data) {
5050
return Mono.fromRunnable(() -> {
5151
this.lastLevel = level;
@@ -54,7 +54,7 @@ public Mono<Void> handleLoggingMessageWithParams(LoggingLevel level, String logg
5454
});
5555
}
5656

57-
@McpLogging
57+
@McpLogging(clientId = "test-client")
5858
public void handleLoggingMessageVoid(LoggingMessageNotification notification) {
5959
this.lastNotification = notification;
6060
}

mcp-annotations/src/test/java/org/springaicommunity/mcp/provider/logging/SyncMcpLoggingProviderTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ static class LoggingHandler {
3636

3737
private String lastData;
3838

39-
@McpLogging
39+
@McpLogging(clientId = "test-client")
4040
public void handleLoggingMessage(LoggingMessageNotification notification) {
4141
this.lastNotification = notification;
4242
}
4343

44-
@McpLogging
44+
@McpLogging(clientId = "test-client")
4545
public void handleLoggingMessageWithParams(LoggingLevel level, String logger, String data) {
4646
this.lastLevel = level;
4747
this.lastLogger = logger;

0 commit comments

Comments
 (0)