Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compatible with spring.webflux.base-path #102

Closed
wants to merge 1 commit into from
Closed
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 @@ -82,8 +82,15 @@ public class WebFluxSseServerTransportProvider implements McpServerTransportProv
*/
public static final String DEFAULT_SSE_ENDPOINT = "/sse";

public static final String DEFAULT_BASE_PATH = "";

private final ObjectMapper objectMapper;

/**
* the project base path
*/
private final String basePath;

private final String messageEndpoint;

private final String sseEndpoint;
Expand All @@ -102,6 +109,7 @@ public class WebFluxSseServerTransportProvider implements McpServerTransportProv
*/
private volatile boolean isClosing = false;


/**
* Constructs a new WebFlux SSE server transport provider instance.
* @param objectMapper The ObjectMapper to use for JSON serialization/deserialization
Expand All @@ -112,17 +120,33 @@ public class WebFluxSseServerTransportProvider implements McpServerTransportProv
* @throws IllegalArgumentException if either parameter is null
*/
public WebFluxSseServerTransportProvider(ObjectMapper objectMapper, String messageEndpoint, String sseEndpoint) {
this(objectMapper, DEFAULT_BASE_PATH, messageEndpoint, sseEndpoint);
}


/**
* Constructs a new WebFlux SSE server transport provider instance.
* @param objectMapper The ObjectMapper to use for JSON serialization/deserialization
* of MCP messages. Must not be null.
* @param basePath webflux base path
* @param messageEndpoint The endpoint URI where clients should send their JSON-RPC
* messages. This endpoint will be communicated to clients during SSE connection
* setup. Must not be null.
* @throws IllegalArgumentException if either parameter is null
*/
public WebFluxSseServerTransportProvider(ObjectMapper objectMapper, String basePath, String messageEndpoint, String sseEndpoint) {
Assert.notNull(objectMapper, "ObjectMapper must not be null");
Assert.notNull(messageEndpoint, "Message endpoint must not be null");
Assert.notNull(sseEndpoint, "SSE endpoint must not be null");

this.objectMapper = objectMapper;
this.basePath = basePath;
this.messageEndpoint = messageEndpoint;
this.sseEndpoint = sseEndpoint;
this.routerFunction = RouterFunctions.route()
.GET(this.sseEndpoint, this::handleSseConnection)
.POST(this.messageEndpoint, this::handleMessage)
.build();
.GET(this.sseEndpoint, this::handleSseConnection)
.POST(this.messageEndpoint, this::handleMessage)
.build();
}

/**
Expand Down Expand Up @@ -245,7 +269,7 @@ private Mono<ServerResponse> handleSseConnection(ServerRequest request) {
logger.debug("Sending initial endpoint event to session: {}", sessionId);
sink.next(ServerSentEvent.builder()
.event(ENDPOINT_EVENT_TYPE)
.data(messageEndpoint + "?sessionId=" + sessionId)
.data(basePath + messageEndpoint + "?sessionId=" + sessionId)
.build());
sink.onCancel(() -> {
logger.debug("Session {} cancelled", sessionId);
Expand Down Expand Up @@ -360,6 +384,8 @@ public static class Builder {

private ObjectMapper objectMapper;

private String basePath = DEFAULT_BASE_PATH;

private String messageEndpoint;

private String sseEndpoint = DEFAULT_SSE_ENDPOINT;
Expand All @@ -377,6 +403,18 @@ public Builder objectMapper(ObjectMapper objectMapper) {
return this;
}

/**
* Sets the project basePath as endpoint prefix where clients should send their JSON-RPC messages
* @param basePath the project basePath . Must not be null.
* @return this builder instance
* @throws IllegalArgumentException if basePath is null
*/
public Builder basePath(String basePath) {
Assert.notNull(basePath, "basePath must not be null");
this.basePath = basePath;
return this;
}

/**
* Sets the endpoint URI where clients should send their JSON-RPC messages.
* @param messageEndpoint The message endpoint URI. Must not be null.
Expand Down Expand Up @@ -411,7 +449,7 @@ public WebFluxSseServerTransportProvider build() {
Assert.notNull(objectMapper, "ObjectMapper must be set");
Assert.notNull(messageEndpoint, "Message endpoint must be set");

return new WebFluxSseServerTransportProvider(objectMapper, messageEndpoint, sseEndpoint);
return new WebFluxSseServerTransportProvider(objectMapper, basePath, messageEndpoint, sseEndpoint);
}

}
Expand Down