Skip to content

Fix CallToolRequest deserialization in McpAsyncServer #355

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -348,9 +348,31 @@ private McpServerSession.RequestHandler<McpSchema.ListToolsResult> toolsListRequ

private McpServerSession.RequestHandler<CallToolResult> toolsCallRequestHandler() {
return (exchange, params) -> {
McpSchema.CallToolRequest callToolRequest = objectMapper.convertValue(params,
new TypeReference<McpSchema.CallToolRequest>() {
});
// 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<McpServerFeatures.AsyncToolSpecification> toolSpecification = this.tools.stream()
.filter(tr -> callToolRequest.name().equals(tr.tool().name()))
Expand Down