|
24 | 24 | import com.google.adk.agents.ReadonlyContext; |
25 | 25 | import com.google.adk.tools.BaseTool; |
26 | 26 | import com.google.adk.tools.BaseToolset; |
| 27 | +import com.google.common.primitives.Booleans; |
27 | 28 | import io.modelcontextprotocol.client.McpSyncClient; |
28 | 29 | import io.modelcontextprotocol.client.transport.ServerParameters; |
29 | 30 | import io.modelcontextprotocol.spec.McpSchema.ListToolsResult; |
@@ -285,12 +286,23 @@ public void close() { |
285 | 286 |
|
286 | 287 | /** Configuration class for MCPToolset. */ |
287 | 288 | public static class McpToolsetConfig extends JsonBaseModel { |
| 289 | + |
| 290 | + private StdioConnectionParameters stdioConnectionParams; |
| 291 | + |
288 | 292 | private StdioServerParameters stdioServerParams; |
289 | 293 |
|
290 | 294 | private SseServerParameters sseServerParams; |
291 | 295 |
|
292 | 296 | private List<String> toolFilter; |
293 | 297 |
|
| 298 | + public StdioConnectionParameters stdioConnectionParams() { |
| 299 | + return stdioConnectionParams; |
| 300 | + } |
| 301 | + |
| 302 | + public void setStdioConnectionParams(StdioConnectionParameters stdioConnectionParams) { |
| 303 | + this.stdioConnectionParams = stdioConnectionParams; |
| 304 | + } |
| 305 | + |
294 | 306 | public StdioServerParameters stdioServerParams() { |
295 | 307 | return stdioServerParams; |
296 | 308 | } |
@@ -337,23 +349,26 @@ public static McpToolset fromConfig(BaseTool.ToolConfig config, String configAbs |
337 | 349 | mapper.convertValue(config.args(), McpToolsetConfig.class); |
338 | 350 |
|
339 | 351 | // Validate that exactly one parameter type is set |
340 | | - if ((mcpToolsetConfig.stdioServerParams() != null) |
341 | | - == (mcpToolsetConfig.sseServerParams() != null)) { |
| 352 | + if (Booleans.countTrue( |
| 353 | + mcpToolsetConfig.stdioServerParams() != null, |
| 354 | + mcpToolsetConfig.sseServerParams() != null, |
| 355 | + mcpToolsetConfig.stdioConnectionParams() != null) |
| 356 | + != 1) { |
342 | 357 | throw new ConfigurationException( |
343 | | - "Exactly one of stdioServerParams or sseServerParams must be set for McpToolset"); |
| 358 | + "Exactly one of stdioConnectionParams, stdioServerParams or sseServerParams must be set" |
| 359 | + + " for McpToolset"); |
344 | 360 | } |
345 | 361 |
|
346 | 362 | // Convert tool filter to Optional<Object> |
347 | | - Optional<Object> toolFilter = |
348 | | - Optional.ofNullable(mcpToolsetConfig.toolFilter()).map(filter -> filter); |
349 | | - |
350 | | - // Create McpToolset with appropriate connection parameters |
351 | | - if (mcpToolsetConfig.stdioServerParams() != null) { |
352 | | - return new McpToolset( |
353 | | - mcpToolsetConfig.stdioServerParams().toServerParameters(), mapper, toolFilter); |
354 | | - } else { |
355 | | - return new McpToolset(mcpToolsetConfig.sseServerParams(), mapper, toolFilter); |
356 | | - } |
| 363 | + Optional<Object> toolFilter = Optional.ofNullable(mcpToolsetConfig.toolFilter()); |
| 364 | + |
| 365 | + Object connectionParameters = |
| 366 | + Optional.<Object>ofNullable(mcpToolsetConfig.stdioConnectionParams()) |
| 367 | + .or(() -> Optional.ofNullable(mcpToolsetConfig.sseServerParams())) |
| 368 | + .orElse(mcpToolsetConfig.stdioConnectionParams()); |
| 369 | + |
| 370 | + // Create McpToolset with McpSessionManager having appropriate connection parameters |
| 371 | + return new McpToolset(new McpSessionManager(connectionParameters), mapper, toolFilter); |
357 | 372 | } catch (IllegalArgumentException e) { |
358 | 373 | throw new ConfigurationException("Failed to parse McpToolsetConfig from ToolArgsConfig", e); |
359 | 374 | } |
|
0 commit comments