|
20 | 20 | import com.fasterxml.jackson.core.type.TypeReference;
|
21 | 21 | import com.fasterxml.jackson.databind.ObjectMapper;
|
22 | 22 | import com.google.adk.tools.BaseTool;
|
| 23 | +import com.google.adk.tools.mcp.McpToolException.McpToolDeclarationException; |
23 | 24 | import com.google.common.collect.ImmutableMap;
|
24 | 25 | import com.google.genai.types.FunctionDeclaration;
|
25 | 26 | import io.modelcontextprotocol.spec.McpSchema.CallToolResult;
|
@@ -70,23 +71,6 @@ protected AbstractMcpTool(
|
70 | 71 | this.objectMapper = objectMapper;
|
71 | 72 | }
|
72 | 73 |
|
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 |
| - |
90 | 74 | @SuppressWarnings("PreferredInterfaceType") // BaseTool.runAsync() returns Map<String, Object>
|
91 | 75 | protected static Map<String, Object> wrapCallResult(
|
92 | 76 | ObjectMapper objectMapper, String mcpToolName, CallToolResult callResult) {
|
@@ -141,4 +125,27 @@ protected static Map<String, Object> wrapCallResult(
|
141 | 125 | }
|
142 | 126 | return ImmutableMap.of("text_output", resultMaps);
|
143 | 127 | }
|
| 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 | + } |
144 | 151 | }
|
0 commit comments