Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions core/src/main/java/com/google/adk/tools/mcp/AbstractMcpTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -77,14 +78,20 @@ public T getMcpSession() {
@Override
public Optional<FunctionDeclaration> 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<String, Object>
Expand Down
32 changes: 32 additions & 0 deletions core/src/main/java/com/google/adk/tools/mcp/McpToolException.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}