|
5 | 5 | package io.modelcontextprotocol.client; |
6 | 6 |
|
7 | 7 | import java.time.Duration; |
8 | | -import java.util.HashMap; |
| 8 | +import java.util.Optional; |
9 | 9 | import java.util.Set; |
| 10 | +import java.util.concurrent.ConcurrentHashMap; |
10 | 11 |
|
11 | 12 | import org.slf4j.Logger; |
12 | 13 | import org.slf4j.LoggerFactory; |
@@ -244,18 +245,19 @@ public Object ping() { |
244 | 245 | */ |
245 | 246 | public McpSchema.CallToolResult callTool(McpSchema.CallToolRequest callToolRequest) { |
246 | 247 | McpSchema.CallToolResult result = this.delegate.callTool(callToolRequest).block(); |
247 | | - HashMap<String, McpSchema.JsonSchema> toolsOutputSchemaCache = this.delegate.getToolsOutputSchemaCache(); |
| 248 | + ConcurrentHashMap<String, Optional<McpSchema.JsonSchema>> toolsOutputSchemaCache = this.delegate |
| 249 | + .getToolsOutputSchemaCache(); |
248 | 250 | // Should not be triggered but added for completeness |
249 | 251 | if (!toolsOutputSchemaCache.containsKey(callToolRequest.name())) { |
250 | 252 | throw new McpError("Tool with name '" + callToolRequest.name() + "' not found"); |
251 | 253 | } |
252 | | - if (result != null && toolsOutputSchemaCache.get(callToolRequest.name()) != null) { |
| 254 | + Optional<McpSchema.JsonSchema> optOutputSchema = toolsOutputSchemaCache.get(callToolRequest.name()); |
| 255 | + if (result != null && optOutputSchema != null && optOutputSchema.isPresent()) { |
253 | 256 | if (result.structuredContent() == null) { |
254 | 257 | throw new McpError("CallToolResult validation failed: structuredContent is null and " |
255 | 258 | + "does not match tool outputSchema."); |
256 | 259 | } |
257 | | - |
258 | | - McpSchema.JsonSchema outputSchema = toolsOutputSchemaCache.get(callToolRequest.name()); |
| 260 | + McpSchema.JsonSchema outputSchema = optOutputSchema.get(); |
259 | 261 |
|
260 | 262 | try { |
261 | 263 | // Convert outputSchema to string |
@@ -284,9 +286,9 @@ public McpSchema.CallToolResult callTool(McpSchema.CallToolRequest callToolReque |
284 | 286 | } |
285 | 287 | } |
286 | 288 | catch (JsonProcessingException e) { |
287 | | - // Log error if output schema can't be parsed to prevent erroring out for |
288 | | - // successful call tool request |
289 | | - logger.error("Encountered exception when parsing outputSchema: {}", e); |
| 289 | + // Log warning if output schema can't be parsed to prevent erroring out |
| 290 | + // for successful call tool request |
| 291 | + logger.warn("Failed to validate CallToolResult: Error parsing tool outputSchema: {}", e); |
290 | 292 | } |
291 | 293 | } |
292 | 294 | return result; |
|
0 commit comments