Skip to content
Open
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 no longer logs an `ERROR` from Reactor ("Operator called default
onErrorDropped", with an `UnsupportedOperationException`) for each request it serves: the
disconnect signal of the request, a minimal stage whose `cancel` throws, was cancelled when
the exchange won the race against it. It is now read through a copy (#1764).
- A request whose preconditions fail ("If-Match", "If-None-Match"...) with a method that the
resource does not support is now answered 405 (Method Not Allowed) with the "Allow"
header, instead of 412 (Precondition Failed) or of the status of a custom precondition
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ private <T> Optional<T> admit(
context.transportRequest()
.map(
request ->
Mono.fromCompletionStage(request.getDisconnectSignal())
McpMessages.disconnected(request)
.flatMap(
cause ->
Mono.<T>error(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,26 @@
import java.nio.charset.StandardCharsets;
import java.util.LinkedHashMap;
import java.util.Map;
import org.restlet.Request;
import reactor.core.publisher.Mono;

/** Bounded transport encoding; all MCP message decoding remains in the official SDK. */
public final class McpMessages {

/**
* The disconnect signal of a request, as a {@link Mono} that emits its cause once, however late
* it is subscribed, and never on its own. The signal is a minimal stage, whose {@code cancel}
* throws: Reactor cancels the stage of a {@code Mono.fromCompletionStage} that loses a race,
* which dropped an {@link UnsupportedOperationException} on every request. The mono is built
* from a copy instead, which can be cancelled and does not complete the signal.
*
* @param request The request.
* @return Emits the cause of the disconnect.
*/
public static Mono<Throwable> disconnected(Request request) {
return Mono.fromCompletionStage(() -> request.getDisconnectSignal().toCompletableFuture());
Comment thread
jlouvel marked this conversation as resolved.
Resolved
}

private McpMessages() {}

public static byte[] read(InputStream input, int limit) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public void handle(Request request, Response response) {
stopping.get()
.flatMap(ignored -> Mono.error(new IOException("MCP server stopped")));
Mono<Void> disconnected =
Mono.fromCompletionStage(request.getDisconnectSignal())
McpMessages.disconnected(request)
.flatMap(
cause ->
Mono.error(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* Copyright 2005-2026 Qlik
*<p>
* The content of this file is subject to the terms of the Apache 2.0 open
* source license available at https://www.opensource.org/licenses/apache-2.0
*<p>
* Restlet is a registered trademark of QlikTech International AB.
*/
package org.restlet.ext.mcp;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.IOException;
import java.time.Duration;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.Test;
import org.restlet.Request;
import org.restlet.data.Method;
import org.restlet.ext.mcp.internal.McpMessages;
import reactor.core.Disposable;
import reactor.core.publisher.Mono;

/** The disconnect signal of a request as a {@link Mono}, which a race may cancel. */
class McpDisconnectedTestCase {
private static Request request() {
return new Request(Method.POST, "http://localhost/mcp");
}

/** The cause is emitted once the request disconnects, and also to a late subscriber. */
@Test
void emitsTheCauseOfTheDisconnectEvenToALateSubscriber() throws Exception {
Request request = request();
Mono<Throwable> signal = McpMessages.disconnected(request);
CompletableFuture<Throwable> early = signal.toFuture();
assertFalse(early.isDone(), "emitted before the disconnect");

IOException cause = new IOException("gone");
request.notifyDisconnect(cause);

assertSame(cause, early.get(5, TimeUnit.SECONDS));
assertSame(cause, signal.block(Duration.ofSeconds(5)));
}

/** Cancelling it, as a race does, throws nothing and leaves the request's signal as it was. */
@Test
void cancellingItNeverCompletesTheSignalOfTheRequest() {
Request request = request();
Disposable subscription = McpMessages.disconnected(request).subscribe();

subscription.dispose();

assertTrue(subscription.isDisposed());
assertFalse(
request.getDisconnectSignal().toCompletableFuture().isDone(),
"the request's own signal was completed");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import org.restlet.data.ServerSentEvent;
import org.restlet.representation.EventStreamReader;
import org.restlet.representation.StringRepresentation;
import reactor.core.publisher.Hooks;
import reactor.core.publisher.Mono;

/**
Expand All @@ -67,6 +68,39 @@ void cleanup() throws Exception {
}
}

/**
* A served request leaves nothing for Reactor to drop: the disconnect signal that loses the
* race with the exchange is cancelled without an {@link UnsupportedOperationException}.
*/
@Test
void aModernRequestDropsNoErrorInReactor() throws Exception {
List<Throwable> dropped = new CopyOnWriteArrayList<>();
Hooks.onErrorDropped(
error -> {
// Reactor's hook is global: only what this fix is about is recorded
if (error instanceof UnsupportedOperationException) {
dropped.add(error);
}
});
try {
McpServerAdapter adapter = adapter();
adapter.start();
Response response =
call(
adapter,
"tools/call",
Map.of("name", "echo", "arguments", Map.of("text", "hi")),
"echo");
assertEquals(200, response.getStatus().getCode());
response.getEntity().getText();
// stopping waits for the workers that were cancelling the race
adapter.stop();
} finally {
Hooks.resetOnErrorDropped();
}
assertEquals(List.of(), dropped);
}

/** The registrations answer 2026-07-28 clients. */
@Test
void registrationsAnswerModernClients() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.restlet.representation.ByteArrayRepresentation;
import org.restlet.representation.InputRepresentation;
import org.restlet.representation.StringRepresentation;
import reactor.core.publisher.Hooks;
import reactor.core.publisher.Mono;

class McpServerAdapterTestCase {
Expand Down Expand Up @@ -79,6 +80,32 @@ static Map<String, Object> envelope(Response response) throws Exception {
.readValue(response.getEntity().getText(), new TypeRef<Map<String, Object>>() {});
}

/**
* A served request leaves nothing for Reactor to drop: the disconnect signal that loses the
* race with the exchange is cancelled without an {@link UnsupportedOperationException}.
*/
@Test
void aLegacyRequestDropsNoErrorInReactor() throws Exception {
List<Throwable> dropped = new CopyOnWriteArrayList<>();
Hooks.onErrorDropped(
error -> {
// Reactor's hook is global: only what this fix is about is recorded
if (error instanceof UnsupportedOperationException) {
dropped.add(error);
}
});
try {
addEcho(server);
server.start();
assertEquals(200, send(LIST.formatted(1)).getStatus().getCode());
// stopping waits for the workers that were cancelling the race
server.stop();
} finally {
Hooks.resetOnErrorDropped();
}
assertEquals(List.of(), dropped);
}

@Test
void nativeDispatchToolsFiltersAndContext() throws Exception {
addEcho(server);
Expand Down
Loading