diff --git a/changes.md b/changes.md index 665f5e2272..f3905510d2 100644 --- a/changes.md +++ b/changes.md @@ -2,6 +2,11 @@ Changes log =========== - 3.0 Milestone 4 (month-day-2026) + - Bugs fixed + - The MCP client sends its requests as an entity that can be read again, where it sent a + transient one: a request customizer can hash the body, as the signer of AWS Signature + Version 4 (`ChallengeScheme.HTTP_AWS_SIGV4`) does, which refused a service other than S3 + and left an S3 body unsigned (`UNSIGNED-PAYLOAD`) (#1777). - 3.0 Milestone 3 (12-01-2026) - Breaking changes diff --git a/org.restlet.ext.mcp/src/main/java/org/restlet/ext/mcp/internal/McpModernHttpTransport.java b/org.restlet.ext.mcp/src/main/java/org/restlet/ext/mcp/internal/McpModernHttpTransport.java index 006c286460..3fcf3de66a 100644 --- a/org.restlet.ext.mcp/src/main/java/org/restlet/ext/mcp/internal/McpModernHttpTransport.java +++ b/org.restlet.ext.mcp/src/main/java/org/restlet/ext/mcp/internal/McpModernHttpTransport.java @@ -42,9 +42,9 @@ import org.restlet.data.ServerSentEvent; import org.restlet.data.Status; import org.restlet.ext.mcp.McpProtocol; -import org.restlet.representation.ByteArrayRepresentation; import org.restlet.representation.EventStreamReader; import org.restlet.representation.Representation; +import org.restlet.representation.StringRepresentation; import reactor.core.Disposable; import reactor.core.publisher.Flux; import reactor.core.publisher.Operators; @@ -297,6 +297,16 @@ private void exchange( } } + /** + * The message as an entity that can be read again, which a request customizer may need: the + * signer of AWS Signature Version 4 hashes the body before it is sent, and refuses, or leaves + * unsigned, a transient entity such as a {@code ByteArrayRepresentation}. + */ + public static Representation repeatable(byte[] message) { + return new StringRepresentation( + new String(message, StandardCharsets.UTF_8), MediaType.APPLICATION_JSON); + } + private Request request( JsonRpc.JSONRPCRequest rpc, String version, @@ -308,9 +318,7 @@ private Request request( new Request( Method.POST, target, - new ByteArrayRepresentation( - McpMessages.encode(mapper, rpc, maxMessageBytes), - MediaType.APPLICATION_JSON)); + repeatable(McpMessages.encode(mapper, rpc, maxMessageBytes))); customizer.accept(request); callCustomizer.accept(request, context); request.getHeaders().set(McpProtocol.VERSION_HEADER, version); diff --git a/org.restlet.ext.mcp/src/main/java/org/restlet/ext/mcp/internal/legacy/RestletMcpClientTransport.java b/org.restlet.ext.mcp/src/main/java/org/restlet/ext/mcp/internal/legacy/RestletMcpClientTransport.java index 6f4bc5c062..4f29d7bb66 100644 --- a/org.restlet.ext.mcp/src/main/java/org/restlet/ext/mcp/internal/legacy/RestletMcpClientTransport.java +++ b/org.restlet.ext.mcp/src/main/java/org/restlet/ext/mcp/internal/legacy/RestletMcpClientTransport.java @@ -41,8 +41,8 @@ import org.restlet.ext.mcp.McpProtocol; import org.restlet.ext.mcp.internal.McpHttpStatusException; import org.restlet.ext.mcp.internal.McpMessages; +import org.restlet.ext.mcp.internal.McpModernHttpTransport; import org.restlet.ext.mcp.internal.McpWorkers; -import org.restlet.representation.ByteArrayRepresentation; import org.restlet.representation.EventStreamReader; import reactor.core.publisher.Mono; @@ -214,10 +214,7 @@ private void exchange(McpSchema.JSONRPCMessage message, AtomicReference String sent = getSessionId(); byte[] bytes = McpMessages.encode(mapper, message, maxMessageBytes); Request request = - new Request( - Method.POST, - target, - new ByteArrayRepresentation(bytes, MediaType.APPLICATION_JSON)); + new Request(Method.POST, target, McpModernHttpTransport.repeatable(bytes)); customizer.accept(request); String version = negotiated; request.getHeaders() diff --git a/org.restlet.ext.mcp/src/test/java/org/restlet/ext/mcp/McpModernClientTestCase.java b/org.restlet.ext.mcp/src/test/java/org/restlet/ext/mcp/McpModernClientTestCase.java index 1ea170a49c..fa73e988ca 100644 --- a/org.restlet.ext.mcp/src/test/java/org/restlet/ext/mcp/McpModernClientTestCase.java +++ b/org.restlet.ext.mcp/src/test/java/org/restlet/ext/mcp/McpModernClientTestCase.java @@ -419,6 +419,33 @@ void aCallCustomizerGetsTheReactorContextOfItsCall() throws Exception { assertEquals(List.of("alice", "none", "bob"), runs); } + /** + * The body of a request can be read again, so a request customizer can hash it, as the signer + * of AWS Signature Version 4 does (#1777). + */ + @Test + void theBodyOfARequestCanBeReadAgainByItsCustomizer() throws Exception { + List hashed = new CopyOnWriteArrayList<>(); + start( + "HTTP2", + null, + configured -> + configured.setRequestCustomizer( + request -> { + try { + assertFalse(request.getEntity().isTransient()); + String first = request.getEntity().getText(); + assertEquals(first, request.getEntity().getText()); + hashed.add(first); + } catch (Exception e) { + throw new IllegalStateException(e); + } + })); + + assertFalse(hashed.isEmpty(), "the start sends a discovery"); + assertTrue(hashed.getFirst().contains("server/discover"), hashed.toString()); + } + /** Starts the server, if needed, and a modern client with an optional form handler. */ private McpAsyncClient start( String mode, McpAsyncElicitationHandler form)