Skip to content

Commit e2c4d40

Browse files
vorburgercopybara-github
authored andcommitted
fix: Add missing logging for MCP Servers
This is (very) useful for debugging MCP related problems. I've stumbled over this while I've built https://docs.enola.dev/concepts/mcp. The code is originally from (my) https://github.com/enola-dev/enola/blob/ffc004666ea7f71357562ef12464d2b9fdbf9dbd/java/dev/enola/ai/mcp/McpServer.java#L29, but I'm hereby donating it! ;-) PiperOrigin-RevId: 796463552
1 parent 864d8e4 commit e2c4d40

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.google.adk.tools.mcp;
17+
18+
import io.modelcontextprotocol.spec.McpSchema;
19+
import io.modelcontextprotocol.spec.McpSchema.LoggingMessageNotification;
20+
import java.util.function.Consumer;
21+
import org.slf4j.Logger;
22+
import org.slf4j.LoggerFactory;
23+
import org.slf4j.event.Level;
24+
25+
class McpServerLogConsumer implements Consumer<LoggingMessageNotification> {
26+
27+
@Override
28+
public void accept(LoggingMessageNotification notif) {
29+
Logger log = LoggerFactory.getLogger(notif.logger());
30+
log.atLevel(convert(notif.level())).log("{}", notif.data());
31+
}
32+
33+
private Level convert(McpSchema.LoggingLevel level) {
34+
return switch (level) {
35+
case DEBUG -> Level.DEBUG;
36+
case INFO, NOTICE -> Level.INFO;
37+
case WARNING -> Level.WARN;
38+
case ERROR, CRITICAL, ALERT, EMERGENCY -> Level.ERROR;
39+
};
40+
}
41+
}

core/src/main/java/com/google/adk/tools/mcp/McpSessionManager.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public static McpSyncClient initializeSession(
7171
.initializationTimeout(
7272
Optional.ofNullable(initializationTimeout).orElse(Duration.ofSeconds(10)))
7373
.requestTimeout(Optional.ofNullable(requestTimeout).orElse(Duration.ofSeconds(10)))
74+
.loggingConsumer(new McpServerLogConsumer())
7475
.capabilities(ClientCapabilities.builder().build())
7576
.build();
7677
InitializeResult initResult = client.initialize();

0 commit comments

Comments
 (0)