|
| 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 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | +import com.google.adk.JsonBaseModel; |
| 21 | +import com.google.adk.tools.BaseTool; |
| 22 | +import com.google.adk.tools.ToolContext; |
| 23 | +import com.google.common.collect.ImmutableMap; |
| 24 | +import com.google.genai.types.FunctionDeclaration; |
| 25 | +import com.google.genai.types.Schema; |
| 26 | +import io.modelcontextprotocol.client.McpAsyncClient; |
| 27 | +import io.modelcontextprotocol.spec.McpSchema.CallToolRequest; |
| 28 | +import io.modelcontextprotocol.spec.McpSchema.JsonSchema; |
| 29 | +import io.modelcontextprotocol.spec.McpSchema.Tool; |
| 30 | +import io.reactivex.rxjava3.core.Maybe; |
| 31 | +import io.reactivex.rxjava3.core.Single; |
| 32 | + |
| 33 | +import java.util.Map; |
| 34 | +import java.util.Optional; |
| 35 | + |
| 36 | +import static java.util.concurrent.TimeUnit.MILLISECONDS; |
| 37 | + |
| 38 | +// TODO(b/413489523): Add support for auth. This is a TODO for Python as well. |
| 39 | + |
| 40 | +/** |
| 41 | + * Initializes a MCP tool. |
| 42 | + * |
| 43 | + * <p>This wraps a MCP Tool interface and an active MCP Session. It invokes the MCP Tool through |
| 44 | + * executing the tool from remote MCP Session. |
| 45 | + */ |
| 46 | +public final class McpAsyncTool extends BaseTool { |
| 47 | + |
| 48 | + Tool mcpTool; |
| 49 | + Single<McpAsyncClient> mcpSession; |
| 50 | + McpSessionManager mcpSessionManager; |
| 51 | + ObjectMapper objectMapper; |
| 52 | + |
| 53 | + /** |
| 54 | + * Creates a new McpTool with the default ObjectMapper. |
| 55 | + * |
| 56 | + * @param mcpTool The MCP tool to wrap. |
| 57 | + * @param mcpSession The MCP session to use to call the tool. |
| 58 | + * @param mcpSessionManager The MCP session manager to use to create new sessions. |
| 59 | + * @throws IllegalArgumentException If mcpTool or mcpSession are null. |
| 60 | + */ |
| 61 | + public McpAsyncTool(Tool mcpTool, Single<McpAsyncClient> mcpSession, McpSessionManager mcpSessionManager) { |
| 62 | + this(mcpTool, mcpSession, mcpSessionManager, JsonBaseModel.getMapper()); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Creates a new McpTool with the default ObjectMapper. |
| 67 | + * |
| 68 | + * @param mcpTool The MCP tool to wrap. |
| 69 | + * @param mcpSession The MCP session to use to call the tool. |
| 70 | + * @param mcpSessionManager The MCP session manager to use to create new sessions. |
| 71 | + * @param objectMapper The ObjectMapper to use to convert JSON schemas. |
| 72 | + * @throws IllegalArgumentException If mcpTool or mcpSession are null. |
| 73 | + */ |
| 74 | + public McpAsyncTool( |
| 75 | + Tool mcpTool, |
| 76 | + Single<McpAsyncClient> mcpSession, |
| 77 | + McpSessionManager mcpSessionManager, |
| 78 | + ObjectMapper objectMapper) { |
| 79 | + super( |
| 80 | + mcpTool == null ? "" : mcpTool.name(), |
| 81 | + mcpTool == null ? "" : (mcpTool.description().isEmpty() ? "" : mcpTool.description())); |
| 82 | + |
| 83 | + if (mcpTool == null) { |
| 84 | + throw new IllegalArgumentException("mcpTool cannot be null"); |
| 85 | + } |
| 86 | + if (mcpSession == null) { |
| 87 | + throw new IllegalArgumentException("mcpSession cannot be null"); |
| 88 | + } |
| 89 | + if (objectMapper == null) { |
| 90 | + throw new IllegalArgumentException("objectMapper cannot be null"); |
| 91 | + } |
| 92 | + this.mcpTool = mcpTool; |
| 93 | + this.mcpSession = mcpSession; |
| 94 | + this.mcpSessionManager = mcpSessionManager; |
| 95 | + this.objectMapper = objectMapper; |
| 96 | + } |
| 97 | + |
| 98 | + public Single<McpAsyncClient> getMcpSession() { |
| 99 | + return this.mcpSession; |
| 100 | + } |
| 101 | + |
| 102 | + public Schema toGeminiSchema(JsonSchema openApiSchema) { |
| 103 | + return Schema.fromJson(objectMapper.valueToTree(openApiSchema).toString()); |
| 104 | + } |
| 105 | + |
| 106 | + private void reintializeSession() { |
| 107 | + this.mcpSession = this.mcpSessionManager.createAsyncSession(); |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public Optional<FunctionDeclaration> declaration() { |
| 112 | + return Optional.of( |
| 113 | + FunctionDeclaration.builder() |
| 114 | + .name(this.name()) |
| 115 | + .description(this.description()) |
| 116 | + .parameters(toGeminiSchema(this.mcpTool.inputSchema())) |
| 117 | + .build()); |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public Single<Map<String, Object>> runAsync(Map<String, Object> args, ToolContext toolContext) { |
| 122 | + return Single.defer(() -> |
| 123 | + this.mcpSession.flatMapMaybe(client -> |
| 124 | + Maybe.fromCompletionStage( |
| 125 | + client.callTool(new CallToolRequest(this.name(), ImmutableMap.copyOf(args))) |
| 126 | + .toFuture() |
| 127 | + ) |
| 128 | + ).map(callResult -> McpTool.wrapCallResult( |
| 129 | + this.objectMapper, this.name(), callResult) |
| 130 | + ).switchIfEmpty( |
| 131 | + Single.fromCallable( |
| 132 | + () -> McpTool.wrapCallResult(this.objectMapper, this.name(), null) |
| 133 | + ) |
| 134 | + ) |
| 135 | + ) |
| 136 | + .retryWhen( |
| 137 | + errors -> |
| 138 | + errors |
| 139 | + .delay(100, MILLISECONDS) |
| 140 | + .take(3) |
| 141 | + .doOnNext( |
| 142 | + error -> { |
| 143 | + System.err.println("Retrying callTool due to: " + error); |
| 144 | + reintializeSession(); |
| 145 | + })); |
| 146 | + } |
| 147 | +} |
0 commit comments