From ec52bd06f242abe5d308771639fddaa68b622fd5 Mon Sep 17 00:00:00 2001 From: Jerome Louvel <374450+jlouvel@users.noreply.github.com> Date: Sun, 29 Nov 2026 14:09:35 -0500 Subject: [PATCH 1/2] Read the disconnect signal of an MCP request through a copy (#1764) The signal is a minimal stage, whose cancel throws: Reactor cancelled it when the exchange won the race against it, and dropped an UnsupportedOperationException on every request. Co-Authored-By: Claude Sonnet 5.5 Signed-off-by: Jerome Louvel <374450+jlouvel@users.noreply.github.com> --- changes.md | 4 +++ .../org/restlet/ext/mcp/McpServerAdapter.java | 2 +- .../restlet/ext/mcp/internal/McpMessages.java | 17 +++++++++++ .../ext/mcp/internal/McpModernExchange.java | 2 +- .../ext/mcp/McpModernServerTestCase.java | 28 +++++++++++++++++++ .../ext/mcp/McpServerAdapterTestCase.java | 21 ++++++++++++++ 6 files changed, 72 insertions(+), 2 deletions(-) diff --git a/changes.md b/changes.md index 82bb39b298..9183f6aba0 100644 --- a/changes.md +++ b/changes.md @@ -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 diff --git a/org.restlet.ext.mcp/src/main/java/org/restlet/ext/mcp/McpServerAdapter.java b/org.restlet.ext.mcp/src/main/java/org/restlet/ext/mcp/McpServerAdapter.java index 4dbbfa6c2b..c2c61b97e0 100644 --- a/org.restlet.ext.mcp/src/main/java/org/restlet/ext/mcp/McpServerAdapter.java +++ b/org.restlet.ext.mcp/src/main/java/org/restlet/ext/mcp/McpServerAdapter.java @@ -679,7 +679,7 @@ private Optional admit( context.transportRequest() .map( request -> - Mono.fromCompletionStage(request.getDisconnectSignal()) + McpMessages.disconnected(request) .flatMap( cause -> Mono.error( diff --git a/org.restlet.ext.mcp/src/main/java/org/restlet/ext/mcp/internal/McpMessages.java b/org.restlet.ext.mcp/src/main/java/org/restlet/ext/mcp/internal/McpMessages.java index b6bae6ec71..efa832f90d 100644 --- a/org.restlet.ext.mcp/src/main/java/org/restlet/ext/mcp/internal/McpMessages.java +++ b/org.restlet.ext.mcp/src/main/java/org/restlet/ext/mcp/internal/McpMessages.java @@ -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 disconnected(Request request) { + return Mono.fromCompletionStage(() -> request.getDisconnectSignal().toCompletableFuture()); + } + private McpMessages() {} public static byte[] read(InputStream input, int limit) throws IOException { diff --git a/org.restlet.ext.mcp/src/main/java/org/restlet/ext/mcp/internal/McpModernExchange.java b/org.restlet.ext.mcp/src/main/java/org/restlet/ext/mcp/internal/McpModernExchange.java index 1c2d2ca449..5e8f44ce14 100644 --- a/org.restlet.ext.mcp/src/main/java/org/restlet/ext/mcp/internal/McpModernExchange.java +++ b/org.restlet.ext.mcp/src/main/java/org/restlet/ext/mcp/internal/McpModernExchange.java @@ -180,7 +180,7 @@ public void handle(Request request, Response response) { stopping.get() .flatMap(ignored -> Mono.error(new IOException("MCP server stopped"))); Mono disconnected = - Mono.fromCompletionStage(request.getDisconnectSignal()) + McpMessages.disconnected(request) .flatMap( cause -> Mono.error( diff --git a/org.restlet.ext.mcp/src/test/java/org/restlet/ext/mcp/McpModernServerTestCase.java b/org.restlet.ext.mcp/src/test/java/org/restlet/ext/mcp/McpModernServerTestCase.java index 0ab4970566..26ee11cab6 100644 --- a/org.restlet.ext.mcp/src/test/java/org/restlet/ext/mcp/McpModernServerTestCase.java +++ b/org.restlet.ext.mcp/src/test/java/org/restlet/ext/mcp/McpModernServerTestCase.java @@ -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; /** @@ -67,6 +68,33 @@ 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 dropped = new CopyOnWriteArrayList<>(); + Hooks.onErrorDropped(dropped::add); + 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 { diff --git a/org.restlet.ext.mcp/src/test/java/org/restlet/ext/mcp/McpServerAdapterTestCase.java b/org.restlet.ext.mcp/src/test/java/org/restlet/ext/mcp/McpServerAdapterTestCase.java index bfcb866af9..7007f69df5 100644 --- a/org.restlet.ext.mcp/src/test/java/org/restlet/ext/mcp/McpServerAdapterTestCase.java +++ b/org.restlet.ext.mcp/src/test/java/org/restlet/ext/mcp/McpServerAdapterTestCase.java @@ -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 { @@ -79,6 +80,26 @@ static Map envelope(Response response) throws Exception { .readValue(response.getEntity().getText(), new TypeRef>() {}); } + /** + * 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 dropped = new CopyOnWriteArrayList<>(); + Hooks.onErrorDropped(dropped::add); + 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); From a3c196539c8ee2704dc3b3c8961edd9da7d93a00 Mon Sep 17 00:00:00 2001 From: Jerome Louvel <374450+jlouvel@users.noreply.github.com> Date: Sun, 29 Nov 2026 14:14:41 -0500 Subject: [PATCH 2/2] Test the disconnect signal of an MCP request and narrow the dropped-error filter (#1764) Co-Authored-By: Claude Sonnet 5.5 Signed-off-by: Jerome Louvel <374450+jlouvel@users.noreply.github.com> --- .../ext/mcp/McpDisconnectedTestCase.java | 60 +++++++++++++++++++ .../ext/mcp/McpModernServerTestCase.java | 8 ++- .../ext/mcp/McpServerAdapterTestCase.java | 8 ++- 3 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 org.restlet.ext.mcp/src/test/java/org/restlet/ext/mcp/McpDisconnectedTestCase.java diff --git a/org.restlet.ext.mcp/src/test/java/org/restlet/ext/mcp/McpDisconnectedTestCase.java b/org.restlet.ext.mcp/src/test/java/org/restlet/ext/mcp/McpDisconnectedTestCase.java new file mode 100644 index 0000000000..87e3f6658f --- /dev/null +++ b/org.restlet.ext.mcp/src/test/java/org/restlet/ext/mcp/McpDisconnectedTestCase.java @@ -0,0 +1,60 @@ +/** + * Copyright 2005-2026 Qlik + *

+ * 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 + *

+ * 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 signal = McpMessages.disconnected(request); + CompletableFuture 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"); + } +} diff --git a/org.restlet.ext.mcp/src/test/java/org/restlet/ext/mcp/McpModernServerTestCase.java b/org.restlet.ext.mcp/src/test/java/org/restlet/ext/mcp/McpModernServerTestCase.java index 26ee11cab6..ad799b34f1 100644 --- a/org.restlet.ext.mcp/src/test/java/org/restlet/ext/mcp/McpModernServerTestCase.java +++ b/org.restlet.ext.mcp/src/test/java/org/restlet/ext/mcp/McpModernServerTestCase.java @@ -75,7 +75,13 @@ void cleanup() throws Exception { @Test void aModernRequestDropsNoErrorInReactor() throws Exception { List dropped = new CopyOnWriteArrayList<>(); - Hooks.onErrorDropped(dropped::add); + 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(); diff --git a/org.restlet.ext.mcp/src/test/java/org/restlet/ext/mcp/McpServerAdapterTestCase.java b/org.restlet.ext.mcp/src/test/java/org/restlet/ext/mcp/McpServerAdapterTestCase.java index 7007f69df5..65d45b292f 100644 --- a/org.restlet.ext.mcp/src/test/java/org/restlet/ext/mcp/McpServerAdapterTestCase.java +++ b/org.restlet.ext.mcp/src/test/java/org/restlet/ext/mcp/McpServerAdapterTestCase.java @@ -87,7 +87,13 @@ static Map envelope(Response response) throws Exception { @Test void aLegacyRequestDropsNoErrorInReactor() throws Exception { List dropped = new CopyOnWriteArrayList<>(); - Hooks.onErrorDropped(dropped::add); + 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();