Skip to content

Commit 7893d2b

Browse files
committed
Fix subscribe in non-blocking context
Signed-off-by: JermaineHua <[email protected]>
1 parent 1bee8d4 commit 7893d2b

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

mcp/src/main/java/io/modelcontextprotocol/spec/DefaultMcpSession.java

+10-9
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import reactor.core.Disposable;
1818
import reactor.core.publisher.Mono;
1919
import reactor.core.publisher.MonoSink;
20+
import reactor.core.scheduler.Schedulers;
2021

2122
/**
2223
* Default implementation of the MCP (Model Context Protocol) session that manages
@@ -135,18 +136,18 @@ public DefaultMcpSession(Duration requestTimeout, McpTransport transport,
135136
}
136137
else if (message instanceof McpSchema.JSONRPCRequest request) {
137138
logger.debug("Received request: {}", request);
138-
handleIncomingRequest(request).subscribe(response -> transport.sendMessage(response).subscribe(),
139-
error -> {
140-
var errorResponse = new McpSchema.JSONRPCResponse(McpSchema.JSONRPC_VERSION, request.id(),
141-
null, new McpSchema.JSONRPCResponse.JSONRPCError(
142-
McpSchema.ErrorCodes.INTERNAL_ERROR, error.getMessage(), null));
143-
transport.sendMessage(errorResponse).subscribe();
144-
});
139+
handleIncomingRequest(request).subscribeOn(Schedulers.boundedElastic())
140+
.subscribe(response -> transport.sendMessage(response).subscribe(), error -> {
141+
var errorResponse = new McpSchema.JSONRPCResponse(McpSchema.JSONRPC_VERSION, request.id(), null,
142+
new McpSchema.JSONRPCResponse.JSONRPCError(McpSchema.ErrorCodes.INTERNAL_ERROR,
143+
error.getMessage(), null));
144+
transport.sendMessage(errorResponse).subscribe();
145+
});
145146
}
146147
else if (message instanceof McpSchema.JSONRPCNotification notification) {
147148
logger.debug("Received notification: {}", notification);
148-
handleIncomingNotification(notification).subscribe(null,
149-
error -> logger.error("Error handling notification: {}", error.getMessage()));
149+
handleIncomingNotification(notification).subscribeOn(Schedulers.boundedElastic())
150+
.subscribe(null, error -> logger.error("Error handling notification: {}", error.getMessage()));
150151
}
151152
})).subscribe();
152153
}

0 commit comments

Comments
 (0)