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
4 changes: 4 additions & 0 deletions changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ Changes log
proxy.

- Bugs fixed
- The MCP extension answers a `POST` without content with a `400` parse error: it answered
`503` and logged a `NullPointerException`, as an entity without content has no stream to
read (#1768); the MCP client likewise fails with an `IOException`, not a
`NullPointerException`, on a response without content.
- The MCP STDIO connector no longer treats a clean end of its input as a failure: the requests
already sent are answered before it closes (within the request timeout of the adapter,
plus 5 s), where they were aborted, so a client that half-closes stdin after its last request, or a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,20 @@ public static Mono<Throwable> disconnected(Request request) {

private McpMessages() {}

/**
* Reads a message, at most {@code limit} bytes.
*
* @param input The stream of the entity: {@code null} for an entity without content, which
* reads as an empty message.
* @param limit The most bytes a message may have.
* @return The bytes read.
* @throws IOException When the stream fails.
* @throws MessageLimitException When the message is longer than the limit.
*/
public static byte[] read(InputStream input, int limit) throws IOException {
if (input == null) {
return new byte[0];
Comment thread
jlouvel marked this conversation as resolved.
Resolved
}
byte[] bytes = input.readNBytes(limit + 1);
if (bytes.length > limit) {
throw new MessageLimitException("MCP message exceeds " + limit + " bytes");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,11 @@ private JsonRpc.JSONRPCMessage single(Representation entity, Response response,
* message size limit, in bytes.
*/
private void stream(Representation entity, Object id, Emitter sink) throws IOException {
try (InputStream input = entity.getStream();
InputStream stream = entity.getStream();
if (stream == null) {
throw new IOException("MCP event stream without content");
}
try (InputStream input = stream;
EventStreamReader reader =
new EventStreamReader(
new InputStreamReader(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,11 @@ private void readResponse(Response response, Object id, boolean initializing)
throw new IOException("Missing MCP response entity or media type");
}
MediaType mediaType = response.getEntity().getMediaType();
try (InputStream input =
Objects.requireNonNull(response.getEntity().getStream(), "response stream")) {
InputStream stream = response.getEntity().getStream();
if (stream == null) {
throw new IOException("MCP response without content");
}
try (InputStream input = stream) {
if (MediaType.APPLICATION_JSON.isCompatible(mediaType)) {
if (!deliver(
McpSchema.deserializeJsonRpcMessage(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,35 @@ static Request request(String body) {
return request;
}

/**
* A request without content is answered with a client error under its protocol version, not a
* failure of the server.
*/
@Test
void aRequestWithoutContentIsAClientError() throws Exception {
server.start();
Map<String, Integer> expected = new java.util.LinkedHashMap<>();
expected.put(McpProtocol.VERSION, -32700);
expected.put("2026-07-28", -32700);
expected.put("2027-04-01", -32022);
Map<String, Object> seen = new java.util.LinkedHashMap<>();
for (String version : expected.keySet()) {
Request request = request("");
org.restlet.representation.EmptyRepresentation empty =
new org.restlet.representation.EmptyRepresentation();
empty.setMediaType(MediaType.APPLICATION_JSON);
request.setEntity(empty);
request.getHeaders().set(McpProtocol.VERSION_HEADER, version);
request.getHeaders().set("Mcp-Method", "tools/list");

Response response = server.handle(request);

assertEquals(Status.CLIENT_ERROR_BAD_REQUEST, response.getStatus(), version);
seen.put(version, ((Map<?, ?>) envelope(response).get("error")).get("code"));
}
assertEquals(expected, seen);
}

private Response send(String body) {
return server.handle(request(body));
}
Expand Down