Skip to content

Commit 86c5208

Browse files
committed
refactor: Move logging level management from McpServerSession to McpAsyncServerExchange
Signed-off-by: Christian Tzolov <christian.tzolov@broadcom.com>
1 parent b2b5b19 commit 86c5208

File tree

2 files changed

+14
-36
lines changed

2 files changed

+14
-36
lines changed

mcp/src/main/java/io/modelcontextprotocol/server/McpAsyncServerExchange.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ public class McpAsyncServerExchange {
2828

2929
private final McpSchema.Implementation clientInfo;
3030

31+
private volatile LoggingLevel minLoggingLevel = LoggingLevel.INFO;
32+
3133
private static final TypeReference<McpSchema.CreateMessageResult> CREATE_MESSAGE_RESULT_TYPE_REF = new TypeReference<>() {
3234
};
3335

@@ -122,11 +124,10 @@ public Mono<Void> loggingNotification(LoggingMessageNotification loggingMessageN
122124
}
123125

124126
return Mono.defer(() -> {
125-
if (!this.session.isLoingLevelEnabled(loggingMessageNotification.level())) {
126-
return Mono.empty();
127+
if (this.isNotificationForLevelAllowed(loggingMessageNotification.level())) {
128+
return this.session.sendNotification(McpSchema.METHOD_NOTIFICATION_MESSAGE, loggingMessageNotification);
127129
}
128-
129-
return this.session.sendNotification(McpSchema.METHOD_NOTIFICATION_MESSAGE, loggingMessageNotification);
130+
return Mono.empty();
130131
});
131132
}
132133

@@ -137,7 +138,15 @@ public Mono<Void> loggingNotification(LoggingMessageNotification loggingMessageN
137138
*/
138139
void setMinLoggingLevel(LoggingLevel minLoggingLevel) {
139140
Assert.notNull(minLoggingLevel, "minLoggingLevel must not be null");
140-
this.session.setMinLoggingLevel(minLoggingLevel);
141+
this.minLoggingLevel = minLoggingLevel;
142+
}
143+
144+
/**
145+
* Checks if the logging level bigger or equal to the minimum set logging level.
146+
* @return true if the logging level is enabled, false otherwise
147+
*/
148+
private boolean isNotificationForLevelAllowed(LoggingLevel loggingLevel) {
149+
return loggingLevel.level() >= this.minLoggingLevel.level();
141150
}
142151

143152
}

mcp/src/main/java/io/modelcontextprotocol/spec/McpServerSession.java

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99

1010
import com.fasterxml.jackson.core.type.TypeReference;
1111
import io.modelcontextprotocol.server.McpAsyncServerExchange;
12-
import io.modelcontextprotocol.spec.McpSchema.LoggingLevel;
13-
import io.modelcontextprotocol.util.Assert;
1412
import org.slf4j.Logger;
1513
import org.slf4j.LoggerFactory;
1614
import reactor.core.publisher.Mono;
@@ -55,8 +53,6 @@ public class McpServerSession implements McpSession {
5553

5654
private final AtomicInteger state = new AtomicInteger(STATE_UNINITIALIZED);
5755

58-
private volatile LoggingLevel minLoggingLevel = LoggingLevel.INFO;
59-
6056
/**
6157
* Creates a new server session with the given parameters and the transport to use.
6258
* @param id session id
@@ -88,23 +84,6 @@ public String getId() {
8884
return this.id;
8985
}
9086

91-
/**
92-
* Checks if the logging level bigger or equal to the minimum set logging level.
93-
* @return true if the logging level is enabled, false otherwise
94-
*/
95-
public boolean isLoingLevelEnabled(LoggingLevel loggingLevel) {
96-
return loggingLevel.level() >= this.minLoggingLevel.level();
97-
}
98-
99-
/**
100-
* Set the minimum logging level for this session.
101-
* @param minLoggingLevel the minimum logging level
102-
*/
103-
public void setMinLoggingLevel(LoggingLevel minLoggingLevel) {
104-
Assert.notNull(minLoggingLevel, "minLoggingLevel can't be null");
105-
this.minLoggingLevel = minLoggingLevel;
106-
}
107-
10887
/**
10988
* Called upon successful initialization sequence between the client and the server
11089
* with the client capabilities and information.
@@ -153,16 +132,6 @@ public <T> Mono<T> sendRequest(String method, Object requestParams, TypeReferenc
153132
}
154133

155134
@Override
156-
public Mono<Void> sendNotification(String method, Object params) {
157-
McpSchema.JSONRPCNotification jsonrpcNotification = new McpSchema.JSONRPCNotification(McpSchema.JSONRPC_VERSION,
158-
method, params);
159-
return this.transport.sendMessage(jsonrpcNotification);
160-
}
161-
162-
// NOTE: This is a workaround for the fact that the {@link #sendNotification(String,
163-
// Map)} method doesn't accept types like LoggingMessageNotification
164-
// TODO investigate if this method can replace the {@link #sendNotification(String,
165-
// Map)} - Breaking change.
166135
public Mono<Void> sendNotification(String method, Object params) {
167136
McpSchema.JSONRPCNotification jsonrpcNotification = new McpSchema.JSONRPCNotification(McpSchema.JSONRPC_VERSION,
168137
method, this.transport.unmarshalFrom(params, new TypeReference<Map<String, Object>>() {

0 commit comments

Comments
 (0)