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
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,9 @@ private String generateRequestId() {
*/
@Override
public <T> Mono<T> sendRequest(String method, Object requestParams, TypeRef<T> typeRef) {
String requestId = this.generateRequestId();

return Mono.deferContextual(ctx -> Mono.<McpSchema.JSONRPCResponse>create(pendingResponseSink -> {
// A new id per subscription, so that a retried request does not reuse one
String requestId = this.generateRequestId();
logger.debug("Sending message for method {}", method);
this.pendingResponses.put(requestId, pendingResponseSink);
McpSchema.JSONRPCRequest jsonrpcRequest = new McpSchema.JSONRPCRequest(method, requestId, requestParams);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
package io.modelcontextprotocol.spec;

import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

Expand Down Expand Up @@ -69,6 +71,33 @@ void testSendRequest() {
session.close();
}

@Test
void testResubscribedRequestUsesNewRequestId() {
List<Object> sentRequestIds = new ArrayList<>();
var transport = new MockMcpClientTransport((t, message) -> {
if (message instanceof McpSchema.JSONRPCRequest request) {
sentRequestIds.add(request.id());
// Fail the first attempt, answer the retry
McpSchema.JSONRPCResponse response = sentRequestIds.size() == 1
? McpSchema.JSONRPCResponse.error(request.id(),
new McpSchema.JSONRPCResponse.JSONRPCError(McpSchema.ErrorCodes.INTERNAL_ERROR,
"Temporary failure"))
: McpSchema.JSONRPCResponse.result(request.id(), "test response");
t.simulateIncomingMessage(response);
}
});
var session = new McpClientSession(TIMEOUT, transport, Map.of(), Map.of(), Function.identity());

StepVerifier.create(session.sendRequest(TEST_METHOD, "test", responseType).retry(1))
.expectNext("test response")
.verifyComplete();

// JSON-RPC request ids must not be reused within a session
assertThat(sentRequestIds).hasSize(2).doesNotHaveDuplicates();

session.close();
}

@Test
void testSendRequestWithError() {
var transport = new MockMcpClientTransport();
Expand Down