Skip to content
Merged
Show file tree
Hide file tree
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 @@ -87,13 +87,23 @@ public <T> T readValue(byte[] content, TypeRef<T> type) throws IOException {

@Override
public <T> T convertValue(Object fromValue, Class<T> type) {
return jsonMapper.convertValue(fromValue, type);
try {
return jsonMapper.convertValue(fromValue, type);
}
catch (JacksonException ex) {
throw new IllegalArgumentException(ex);
}
}

@Override
public <T> T convertValue(Object fromValue, TypeRef<T> type) {
JavaType javaType = jsonMapper.getTypeFactory().constructType(type.getType());
return jsonMapper.convertValue(fromValue, javaType);
try {
return jsonMapper.convertValue(fromValue, javaType);
}
catch (JacksonException ex) {
throw new IllegalArgumentException(ex);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -951,6 +951,26 @@ void rejectsNonJsonContentType(String contentType) throws Exception {
assertThat(toolCalled).isFalse();
}

@Test
void rejectsMalformedMessageAsInvalidRequest() throws Exception {
McpServer.sync(mcpStatelessServerTransport).build();

// Valid JSON, but "jsonrpc" is an object instead of a string, so it cannot be
// converted into a JSONRPCRequest
var request = HttpRequest.newBuilder()
.uri(URI.create("http://localhost:" + PORT + CUSTOM_MESSAGE_ENDPOINT))
.header("Content-Type", APPLICATION_JSON)
.header("Accept", APPLICATION_JSON + ", " + TEXT_EVENT_STREAM)
.POST(HttpRequest.BodyPublishers.ofString("""
{"jsonrpc":{"a":1},"id":1,"method":"tools/list"}"""))
.build();

var response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());

assertThat(response.statusCode()).isEqualTo(HttpServletResponse.SC_BAD_REQUEST);
assertThatJson(response.body()).inPath("message").isEqualTo("Invalid message format");
}

private double evaluateExpression(String expression) {
// Simple expression evaluator for testing
return switch (expression) {
Expand Down
Loading