|
| 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 | +} |
0 commit comments