diff --git a/mcp/src/main/java/io/modelcontextprotocol/server/McpAsyncServer.java b/mcp/src/main/java/io/modelcontextprotocol/server/McpAsyncServer.java index 02ad955b9..640882adc 100644 --- a/mcp/src/main/java/io/modelcontextprotocol/server/McpAsyncServer.java +++ b/mcp/src/main/java/io/modelcontextprotocol/server/McpAsyncServer.java @@ -348,9 +348,31 @@ private McpServerSession.RequestHandler toolsListRequ private McpServerSession.RequestHandler toolsCallRequestHandler() { return (exchange, params) -> { - McpSchema.CallToolRequest callToolRequest = objectMapper.convertValue(params, - new TypeReference() { - }); + // Handle the conversion based on what we actually receive + McpSchema.CallToolRequest callToolRequest; + + if (params instanceof McpSchema.CallToolRequest) { + // Already the correct type + callToolRequest = (McpSchema.CallToolRequest) params; + } + else if (params instanceof java.util.Map) { + // This is the expected case - params is a Map representing the CallToolRequest + try { + callToolRequest = objectMapper.convertValue(params, McpSchema.CallToolRequest.class); + } + catch (Exception e) { + throw new IllegalArgumentException("Invalid params for tools/call: " + params, e); + } + } + else { + // Unknown type - try generic conversion + try { + callToolRequest = objectMapper.convertValue(params, McpSchema.CallToolRequest.class); + } + catch (Exception e) { + throw new IllegalArgumentException("Cannot convert params to CallToolRequest: " + params, e); + } + } Optional toolSpecification = this.tools.stream() .filter(tr -> callToolRequest.name().equals(tr.tool().name()))