Skip to content

Commit 871add4

Browse files
committed
refactor: reduce log verbosity by adjusting log levels
- Change most INFO logs to DEBUG level for operational messages - Keep stderr messages at INFO level - Rename errorHandler to stdErrorHandler in StdioClientTransport - Add debug logging for JSON message deserialization Signed-off-by: Christian Tzolov <[email protected]>
1 parent 5d27a13 commit 871add4

File tree

9 files changed

+35
-28
lines changed

9 files changed

+35
-28
lines changed

mcp-spring/mcp-spring-webflux/src/main/java/io/modelcontextprotocol/server/transport/WebFluxSseServerTransport.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ public Mono<Void> closeGracefully() {
251251
.then(Mono.fromRunnable(() -> sessions.remove(sessionId)));
252252
}).toList()))
253253
.timeout(Duration.ofSeconds(5))
254-
.doOnSuccess(v -> logger.info("Graceful shutdown completed"))
254+
.doOnSuccess(v -> logger.debug("Graceful shutdown completed"))
255255
.doOnError(e -> logger.error("Error during graceful shutdown: {}", e.getMessage()));
256256
}
257257

mcp-spring/mcp-spring-webmvc/src/main/java/io/modelcontextprotocol/server/transport/WebMvcSseServerTransport.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ public Mono<Void> closeGracefully() {
353353
sessions.remove(sessionId);
354354
});
355355

356-
logger.info("Graceful shutdown completed");
356+
logger.debug("Graceful shutdown completed");
357357
});
358358
}
359359

mcp/src/main/java/io/modelcontextprotocol/client/McpAsyncClient.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public class McpAsyncClient {
173173
// Tools Change Notification
174174
List<Function<List<McpSchema.Tool>, Mono<Void>>> toolsChangeConsumersFinal = new ArrayList<>();
175175
toolsChangeConsumersFinal
176-
.add((notification) -> Mono.fromRunnable(() -> logger.info("Tools changed: {}", notification)));
176+
.add((notification) -> Mono.fromRunnable(() -> logger.debug("Tools changed: {}", notification)));
177177

178178
if (!Utils.isEmpty(features.toolsChangeConsumers())) {
179179
toolsChangeConsumersFinal.addAll(features.toolsChangeConsumers());
@@ -184,7 +184,7 @@ public class McpAsyncClient {
184184
// Resources Change Notification
185185
List<Function<List<McpSchema.Resource>, Mono<Void>>> resourcesChangeConsumersFinal = new ArrayList<>();
186186
resourcesChangeConsumersFinal
187-
.add((notification) -> Mono.fromRunnable(() -> logger.info("Resources changed: {}", notification)));
187+
.add((notification) -> Mono.fromRunnable(() -> logger.debug("Resources changed: {}", notification)));
188188

189189
if (!Utils.isEmpty(features.resourcesChangeConsumers())) {
190190
resourcesChangeConsumersFinal.addAll(features.resourcesChangeConsumers());
@@ -196,7 +196,7 @@ public class McpAsyncClient {
196196
// Prompts Change Notification
197197
List<Function<List<McpSchema.Prompt>, Mono<Void>>> promptsChangeConsumersFinal = new ArrayList<>();
198198
promptsChangeConsumersFinal
199-
.add((notification) -> Mono.fromRunnable(() -> logger.info("Prompts changed: {}", notification)));
199+
.add((notification) -> Mono.fromRunnable(() -> logger.debug("Prompts changed: {}", notification)));
200200
if (!Utils.isEmpty(features.promptsChangeConsumers())) {
201201
promptsChangeConsumersFinal.addAll(features.promptsChangeConsumers());
202202
}
@@ -205,7 +205,7 @@ public class McpAsyncClient {
205205

206206
// Utility Logging Notification
207207
List<Function<LoggingMessageNotification, Mono<Void>>> loggingConsumersFinal = new ArrayList<>();
208-
loggingConsumersFinal.add((notification) -> Mono.fromRunnable(() -> logger.info("Logging: {}", notification)));
208+
loggingConsumersFinal.add((notification) -> Mono.fromRunnable(() -> logger.debug("Logging: {}", notification)));
209209
if (!Utils.isEmpty(features.loggingConsumers())) {
210210
loggingConsumersFinal.addAll(features.loggingConsumers());
211211
}
@@ -364,7 +364,7 @@ public Mono<Void> addRoot(Root root) {
364364

365365
this.roots.put(root.uri(), root);
366366

367-
logger.info("Added root: {}", root);
367+
logger.debug("Added root: {}", root);
368368

369369
if (this.clientCapabilities.roots().listChanged()) {
370370
return this.rootsListChangedNotification();
@@ -390,7 +390,7 @@ public Mono<Void> removeRoot(String rootUri) {
390390
Root removed = this.roots.remove(rootUri);
391391

392392
if (removed != null) {
393-
logger.info("Removed Root: {}", rootUri);
393+
logger.debug("Removed Root: {}", rootUri);
394394
if (this.clientCapabilities.roots().listChanged()) {
395395
return this.rootsListChangedNotification();
396396
}

mcp/src/main/java/io/modelcontextprotocol/client/transport/StdioClientTransport.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public class StdioClientTransport implements ClientMcpTransport {
6868
private volatile boolean isClosing = false;
6969

7070
// visible for tests
71-
private Consumer<String> errorHandler = error -> logger.error("Error received: {}", error);
71+
private Consumer<String> stdErrorHandler = error -> logger.info("STDERR Message received: {}", error);
7272

7373
/**
7474
* Creates a new StdioClientTransport with the specified parameters and default
@@ -164,8 +164,8 @@ protected ProcessBuilder getProcessBuilder() {
164164
* </p>
165165
* @param errorHandler a consumer that processes error messages
166166
*/
167-
public void setErrorHandler(Consumer<String> errorHandler) {
168-
this.errorHandler = errorHandler;
167+
public void setStdErrorHandler(Consumer<String> errorHandler) {
168+
this.stdErrorHandler = errorHandler;
169169
}
170170

171171
/**
@@ -192,7 +192,6 @@ private void startErrorProcessing() {
192192
String line;
193193
while (!isClosing && (line = processErrorReader.readLine()) != null) {
194194
try {
195-
logger.error("Received error line: {}", line);
196195
if (!this.errorSink.tryEmitNext(line).isSuccess()) {
197196
if (!isClosing) {
198197
logger.error("Failed to emit error message");
@@ -230,7 +229,7 @@ private void handleIncomingMessages(Function<Mono<JSONRPCMessage>, Mono<JSONRPCM
230229

231230
private void handleIncomingErrors() {
232231
this.errorSink.asFlux().subscribe(e -> {
233-
this.errorHandler.accept(e);
232+
this.stdErrorHandler.accept(e);
234233
});
235234
}
236235

@@ -355,7 +354,7 @@ public Mono<Void> closeGracefully() {
355354
// Give a short time for any pending messages to be processed
356355
return Mono.delay(Duration.ofMillis(100));
357356
})).then(Mono.fromFuture(() -> {
358-
logger.info("Sending TERM to process");
357+
logger.debug("Sending TERM to process");
359358
if (this.process != null) {
360359
this.process.destroy();
361360
return process.onExit();
@@ -375,7 +374,7 @@ public Mono<Void> closeGracefully() {
375374
errorScheduler.dispose();
376375
outboundScheduler.dispose();
377376

378-
logger.info("Graceful shutdown completed");
377+
logger.debug("Graceful shutdown completed");
379378
}
380379
catch (Exception e) {
381380
logger.error("Error during graceful shutdown", e);

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ public Mono<Void> addTool(McpServerFeatures.AsyncToolRegistration toolRegistrati
318318
}
319319

320320
this.tools.add(toolRegistration);
321-
logger.info("Added tool handler: {}", toolRegistration.tool().name());
321+
logger.debug("Added tool handler: {}", toolRegistration.tool().name());
322322

323323
if (this.serverCapabilities.tools().listChanged()) {
324324
return notifyToolsListChanged();
@@ -343,7 +343,7 @@ public Mono<Void> removeTool(String toolName) {
343343
return Mono.defer(() -> {
344344
boolean removed = this.tools.removeIf(toolRegistration -> toolRegistration.tool().name().equals(toolName));
345345
if (removed) {
346-
logger.info("Removed tool handler: {}", toolName);
346+
logger.debug("Removed tool handler: {}", toolName);
347347
if (this.serverCapabilities.tools().listChanged()) {
348348
return notifyToolsListChanged();
349349
}
@@ -411,7 +411,7 @@ public Mono<Void> addResource(McpServerFeatures.AsyncResourceRegistration resour
411411
return Mono
412412
.error(new McpError("Resource with URI '" + resourceHandler.resource().uri() + "' already exists"));
413413
}
414-
logger.info("Added resource handler: {}", resourceHandler.resource().uri());
414+
logger.debug("Added resource handler: {}", resourceHandler.resource().uri());
415415
if (this.serverCapabilities.resources().listChanged()) {
416416
return notifyResourcesListChanged();
417417
}
@@ -435,7 +435,7 @@ public Mono<Void> removeResource(String resourceUri) {
435435
return Mono.defer(() -> {
436436
McpServerFeatures.AsyncResourceRegistration removed = this.resources.remove(resourceUri);
437437
if (removed != null) {
438-
logger.info("Removed resource handler: {}", resourceUri);
438+
logger.debug("Removed resource handler: {}", resourceUri);
439439
if (this.serverCapabilities.resources().listChanged()) {
440440
return notifyResourcesListChanged();
441441
}
@@ -507,7 +507,7 @@ public Mono<Void> addPrompt(McpServerFeatures.AsyncPromptRegistration promptRegi
507507
new McpError("Prompt with name '" + promptRegistration.prompt().name() + "' already exists"));
508508
}
509509

510-
logger.info("Added prompt handler: {}", promptRegistration.prompt().name());
510+
logger.debug("Added prompt handler: {}", promptRegistration.prompt().name());
511511

512512
// Servers that declared the listChanged capability SHOULD send a
513513
// notification,
@@ -536,7 +536,7 @@ public Mono<Void> removePrompt(String promptName) {
536536
McpServerFeatures.AsyncPromptRegistration removed = this.prompts.remove(promptName);
537537

538538
if (removed != null) {
539-
logger.info("Removed prompt handler: {}", promptName);
539+
logger.debug("Removed prompt handler: {}", promptName);
540540
// Servers that declared the listChanged capability SHOULD send a
541541
// notification, when the list of available prompts changes
542542
if (this.serverCapabilities.prompts().listChanged()) {

mcp/src/main/java/io/modelcontextprotocol/server/transport/StdioServerTransport.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ private void startInboundProcessing() {
141141
break;
142142
}
143143

144+
logger.debug("Received JSON message: {}", line);
145+
144146
try {
145147
JSONRPCMessage message = McpSchema.deserializeJsonRpcMessage(this.objectMapper, line);
146148
if (!this.inboundSink.tryEmitNext(message).isSuccess()) {
@@ -229,7 +231,7 @@ public Mono<Void> closeGracefully() {
229231
// Completing the inbound causes the outbound to be completed as well, so
230232
// we only close the inbound.
231233
inboundSink.tryEmitComplete();
232-
logger.info("Graceful shutdown complete");
234+
logger.debug("Graceful shutdown complete");
233235
return Mono.empty();
234236
}).subscribeOn(Schedulers.boundedElastic());
235237
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public DefaultMcpSession(Duration requestTimeout, McpTransport transport,
124124
// consumer
125125
this.connection = this.transport.connect(mono -> mono.doOnNext(message -> {
126126
if (message instanceof McpSchema.JSONRPCResponse response) {
127-
logger.info("Received Response: {}", response);
127+
logger.debug("Received Response: {}", response);
128128
var sink = pendingResponses.remove(response.id());
129129
if (sink == null) {
130130
logger.warn("Unexpected response for unkown id {}", response.id());
@@ -134,7 +134,7 @@ public DefaultMcpSession(Duration requestTimeout, McpTransport transport,
134134
}
135135
}
136136
else if (message instanceof McpSchema.JSONRPCRequest request) {
137-
logger.info("Received request: {}", request);
137+
logger.debug("Received request: {}", request);
138138
handleIncomingRequest(request).subscribe(response -> transport.sendMessage(response).subscribe(),
139139
error -> {
140140
var errorResponse = new McpSchema.JSONRPCResponse(McpSchema.JSONRPC_VERSION, request.id(),
@@ -144,7 +144,7 @@ else if (message instanceof McpSchema.JSONRPCRequest request) {
144144
});
145145
}
146146
else if (message instanceof McpSchema.JSONRPCNotification notification) {
147-
logger.info("Received notification: {}", notification);
147+
logger.debug("Received notification: {}", notification);
148148
handleIncomingNotification(notification).subscribe(null,
149149
error -> logger.error("Error handling notification: {}", error.getMessage()));
150150
}

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import com.fasterxml.jackson.annotation.JsonTypeInfo.As;
1818
import com.fasterxml.jackson.core.type.TypeReference;
1919
import com.fasterxml.jackson.databind.ObjectMapper;
20+
import org.slf4j.Logger;
21+
import org.slf4j.LoggerFactory;
2022

2123
/**
2224
* Based on the <a href="http://www.jsonrpc.org/specification">JSON-RPC 2.0
@@ -28,6 +30,8 @@
2830
*/
2931
public final class McpSchema {
3032

33+
private static final Logger logger = LoggerFactory.getLogger(McpSchema.class);
34+
3135
private McpSchema() {
3236
}
3337

@@ -144,6 +148,8 @@ public sealed interface Request
144148
public static JSONRPCMessage deserializeJsonRpcMessage(ObjectMapper objectMapper, String jsonText)
145149
throws IOException {
146150

151+
logger.debug("Received JSON message: {}", jsonText);
152+
147153
var map = objectMapper.readValue(jsonText, MAP_TYPE_REF);
148154

149155
// Determine message type based on specific JSON structure
@@ -662,8 +668,8 @@ public record ListToolsResult( // @formatter:off
662668
@JsonInclude(JsonInclude.Include.NON_ABSENT)
663669
@JsonIgnoreProperties(ignoreUnknown = true)
664670
record JsonSchema( // @formatter:off
665-
@JsonProperty("type") String type,
666-
@JsonProperty("properties") Map<String, Object> properties,
671+
@JsonProperty("type") String type,
672+
@JsonProperty("properties") Map<String, Object> properties,
667673
@JsonProperty("required") List<String> required,
668674
@JsonProperty("additionalProperties") Boolean additionalProperties) {
669675
} // @formatter:on

mcp/src/test/java/io/modelcontextprotocol/client/StdioMcpSyncClientTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ protected ClientMcpTransport createMcpTransport() {
3636
void customErrorHandlerShouldReceiveErrors() {
3737
AtomicReference<String> receivedError = new AtomicReference<>();
3838

39-
((StdioClientTransport) mcpTransport).setErrorHandler(error -> receivedError.set(error));
39+
((StdioClientTransport) mcpTransport).setStdErrorHandler(error -> receivedError.set(error));
4040

4141
String errorMessage = "Test error";
4242
((StdioClientTransport) mcpTransport).getErrorSink().tryEmitNext(errorMessage);

0 commit comments

Comments
 (0)