Skip to content

Commit 181fdbd

Browse files
committed
feat: provide more detailed logs when mcp tool declaration failed.
1 parent 48cfb3d commit 181fdbd

File tree

2 files changed

+56
-17
lines changed

2 files changed

+56
-17
lines changed

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

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.fasterxml.jackson.core.type.TypeReference;
2121
import com.fasterxml.jackson.databind.ObjectMapper;
2222
import com.google.adk.tools.BaseTool;
23+
import com.google.adk.tools.mcp.McpToolException.McpToolDeclarationException;
2324
import com.google.common.collect.ImmutableMap;
2425
import com.google.genai.types.FunctionDeclaration;
2526
import io.modelcontextprotocol.spec.McpSchema.CallToolResult;
@@ -70,23 +71,6 @@ protected AbstractMcpTool(
7071
this.objectMapper = objectMapper;
7172
}
7273

73-
public T getMcpSession() {
74-
return this.mcpSession;
75-
}
76-
77-
@Override
78-
public Optional<FunctionDeclaration> declaration() {
79-
JsonSchema schema = this.mcpTool.inputSchema();
80-
return Optional.ofNullable(schema)
81-
.map(
82-
value ->
83-
FunctionDeclaration.builder()
84-
.name(this.name())
85-
.description(this.description())
86-
.parametersJsonSchema(value)
87-
.build());
88-
}
89-
9074
@SuppressWarnings("PreferredInterfaceType") // BaseTool.runAsync() returns Map<String, Object>
9175
protected static Map<String, Object> wrapCallResult(
9276
ObjectMapper objectMapper, String mcpToolName, CallToolResult callResult) {
@@ -141,4 +125,27 @@ protected static Map<String, Object> wrapCallResult(
141125
}
142126
return ImmutableMap.of("text_output", resultMaps);
143127
}
128+
129+
public T getMcpSession() {
130+
return this.mcpSession;
131+
}
132+
133+
@Override
134+
public Optional<FunctionDeclaration> declaration() {
135+
JsonSchema schema = this.mcpTool.inputSchema();
136+
try {
137+
return Optional.ofNullable(schema)
138+
.map(
139+
value ->
140+
FunctionDeclaration.builder()
141+
.name(this.name())
142+
.description(this.description())
143+
.parametersJsonSchema(value)
144+
.build());
145+
} catch (Exception e) {
146+
throw new McpToolDeclarationException(
147+
String.format("MCP tool:%s failed to get declaration, schema:%s.", this.name(), schema),
148+
e);
149+
}
150+
}
144151
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
17+
package com.google.adk.tools.mcp;
18+
19+
/** Base exception for all errors originating from {@code AbstractMcpTool} and its subclasses. */
20+
public class McpToolException extends RuntimeException {
21+
22+
public McpToolException(String message, Throwable cause) {
23+
super(message, cause);
24+
}
25+
26+
/** Exception thrown when there's an error during MCP tool declaration generated. */
27+
public static class McpToolDeclarationException extends McpToolException {
28+
public McpToolDeclarationException(String message, Throwable cause) {
29+
super(message, cause);
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)