|
16 | 16 |
|
17 | 17 | package com.google.adk.tools.mcp; |
18 | 18 |
|
| 19 | +import static java.util.concurrent.TimeUnit.MILLISECONDS; |
| 20 | + |
19 | 21 | import com.fasterxml.jackson.databind.ObjectMapper; |
20 | 22 | import com.google.adk.JsonBaseModel; |
21 | 23 | import com.google.adk.tools.BaseTool; |
|
29 | 31 | import io.modelcontextprotocol.spec.McpSchema.Tool; |
30 | 32 | import io.reactivex.rxjava3.core.Maybe; |
31 | 33 | import io.reactivex.rxjava3.core.Single; |
32 | | - |
33 | 34 | import java.util.Map; |
34 | 35 | import java.util.Optional; |
35 | 36 |
|
36 | | -import static java.util.concurrent.TimeUnit.MILLISECONDS; |
37 | | - |
38 | 37 | // TODO(b/413489523): Add support for auth. This is a TODO for Python as well. |
39 | 38 |
|
40 | 39 | /** |
|
45 | 44 | */ |
46 | 45 | public final class McpAsyncTool extends BaseTool { |
47 | 46 |
|
48 | | - Tool mcpTool; |
49 | | - Single<McpAsyncClient> mcpSession; |
50 | | - McpSessionManager mcpSessionManager; |
51 | | - ObjectMapper objectMapper; |
| 47 | + Tool mcpTool; |
| 48 | + Single<McpAsyncClient> mcpSession; |
| 49 | + McpSessionManager mcpSessionManager; |
| 50 | + ObjectMapper objectMapper; |
52 | 51 |
|
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 | | - } |
| 52 | + /** |
| 53 | + * Creates a new McpTool with the default ObjectMapper. |
| 54 | + * |
| 55 | + * @param mcpTool The MCP tool to wrap. |
| 56 | + * @param mcpSession The MCP session to use to call the tool. |
| 57 | + * @param mcpSessionManager The MCP session manager to use to create new sessions. |
| 58 | + * @throws IllegalArgumentException If mcpTool or mcpSession are null. |
| 59 | + */ |
| 60 | + public McpAsyncTool( |
| 61 | + Tool mcpTool, Single<McpAsyncClient> mcpSession, McpSessionManager mcpSessionManager) { |
| 62 | + this(mcpTool, mcpSession, mcpSessionManager, JsonBaseModel.getMapper()); |
| 63 | + } |
64 | 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())); |
| 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 | 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; |
| 83 | + if (mcpTool == null) { |
| 84 | + throw new IllegalArgumentException("mcpTool cannot be null"); |
96 | 85 | } |
97 | | - |
98 | | - public Single<McpAsyncClient> getMcpSession() { |
99 | | - return this.mcpSession; |
| 86 | + if (mcpSession == null) { |
| 87 | + throw new IllegalArgumentException("mcpSession cannot be null"); |
100 | 88 | } |
101 | | - |
102 | | - public Schema toGeminiSchema(JsonSchema openApiSchema) { |
103 | | - return Schema.fromJson(objectMapper.valueToTree(openApiSchema).toString()); |
| 89 | + if (objectMapper == null) { |
| 90 | + throw new IllegalArgumentException("objectMapper cannot be null"); |
104 | 91 | } |
| 92 | + this.mcpTool = mcpTool; |
| 93 | + this.mcpSession = mcpSession; |
| 94 | + this.mcpSessionManager = mcpSessionManager; |
| 95 | + this.objectMapper = objectMapper; |
| 96 | + } |
105 | 97 |
|
106 | | - private void reintializeSession() { |
107 | | - this.mcpSession = this.mcpSessionManager.createAsyncSession(); |
108 | | - } |
| 98 | + public Single<McpAsyncClient> getMcpSession() { |
| 99 | + return this.mcpSession; |
| 100 | + } |
109 | 101 |
|
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 | | - } |
| 102 | + public Schema toGeminiSchema(JsonSchema openApiSchema) { |
| 103 | + return Schema.fromJson(objectMapper.valueToTree(openApiSchema).toString()); |
| 104 | + } |
119 | 105 |
|
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 | | - } |
| 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 | + () -> |
| 124 | + this.mcpSession |
| 125 | + .flatMapMaybe( |
| 126 | + client -> |
| 127 | + Maybe.fromCompletionStage( |
| 128 | + client |
| 129 | + .callTool( |
| 130 | + new CallToolRequest(this.name(), ImmutableMap.copyOf(args))) |
| 131 | + .toFuture())) |
| 132 | + .map( |
| 133 | + callResult -> |
| 134 | + McpTool.wrapCallResult(this.objectMapper, this.name(), callResult)) |
| 135 | + .switchIfEmpty( |
| 136 | + Single.fromCallable( |
| 137 | + () -> McpTool.wrapCallResult(this.objectMapper, this.name(), null)))) |
| 138 | + .retryWhen( |
| 139 | + errors -> |
| 140 | + errors |
| 141 | + .delay(100, MILLISECONDS) |
| 142 | + .take(3) |
| 143 | + .doOnNext( |
| 144 | + error -> { |
| 145 | + System.err.println("Retrying callTool due to: " + error); |
| 146 | + reintializeSession(); |
| 147 | + })); |
| 148 | + } |
147 | 149 | } |
0 commit comments