diff --git a/core/src/main/java/com/google/adk/tools/mcp/AbstractMcpTool.java b/core/src/main/java/com/google/adk/tools/mcp/AbstractMcpTool.java index 3fe76517..ddd65be5 100644 --- a/core/src/main/java/com/google/adk/tools/mcp/AbstractMcpTool.java +++ b/core/src/main/java/com/google/adk/tools/mcp/AbstractMcpTool.java @@ -20,6 +20,7 @@ import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.adk.tools.BaseTool; +import com.google.adk.tools.mcp.McpToolException.McpToolDeclarationException; import com.google.common.collect.ImmutableMap; import com.google.genai.types.FunctionDeclaration; import io.modelcontextprotocol.spec.McpSchema.CallToolResult; @@ -77,14 +78,20 @@ public T getMcpSession() { @Override public Optional declaration() { JsonSchema schema = this.mcpTool.inputSchema(); - return Optional.ofNullable(schema) - .map( - value -> - FunctionDeclaration.builder() - .name(this.name()) - .description(this.description()) - .parametersJsonSchema(value) - .build()); + try { + return Optional.ofNullable(schema) + .map( + value -> + FunctionDeclaration.builder() + .name(this.name()) + .description(this.description()) + .parametersJsonSchema(value) + .build()); + } catch (Exception e) { + throw new McpToolDeclarationException( + String.format("MCP tool:%s failed to get declaration, schema:%s.", this.name(), schema), + e); + } } @SuppressWarnings("PreferredInterfaceType") // BaseTool.runAsync() returns Map diff --git a/core/src/main/java/com/google/adk/tools/mcp/McpToolException.java b/core/src/main/java/com/google/adk/tools/mcp/McpToolException.java new file mode 100644 index 00000000..c1e296ad --- /dev/null +++ b/core/src/main/java/com/google/adk/tools/mcp/McpToolException.java @@ -0,0 +1,32 @@ +/* + * Copyright 2025 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.adk.tools.mcp; + +/** Base exception for all errors originating from {@code AbstractMcpTool} and its subclasses. */ +public class McpToolException extends RuntimeException { + + public McpToolException(String message, Throwable cause) { + super(message, cause); + } + + /** Exception thrown when there's an error during MCP tool declaration generated. */ + public static class McpToolDeclarationException extends McpToolException { + public McpToolDeclarationException(String message, Throwable cause) { + super(message, cause); + } + } +}