@@ -82,8 +82,16 @@ public class WebFluxSseServerTransportProvider implements McpServerTransportProv
82
82
*/
83
83
public static final String DEFAULT_SSE_ENDPOINT = "/sse" ;
84
84
85
+ public static final String DEFAULT_BASE_URL = "" ;
86
+
85
87
private final ObjectMapper objectMapper ;
86
88
89
+ /**
90
+ * Base URL for the message endpoint. This is used to construct the full URL for
91
+ * clients to send their JSON-RPC messages.
92
+ */
93
+ private final String baseUrl ;
94
+
87
95
private final String messageEndpoint ;
88
96
89
97
private final String sseEndpoint ;
@@ -102,6 +110,20 @@ public class WebFluxSseServerTransportProvider implements McpServerTransportProv
102
110
*/
103
111
private volatile boolean isClosing = false ;
104
112
113
+ /**
114
+ * Constructs a new WebFlux SSE server transport provider instance with the default
115
+ * SSE endpoint.
116
+ * @param objectMapper The ObjectMapper to use for JSON serialization/deserialization
117
+ * of MCP messages. Must not be null.
118
+ * @param messageEndpoint The endpoint URI where clients should send their JSON-RPC
119
+ * messages. This endpoint will be communicated to clients during SSE connection
120
+ * setup. Must not be null.
121
+ * @throws IllegalArgumentException if either parameter is null
122
+ */
123
+ public WebFluxSseServerTransportProvider (ObjectMapper objectMapper , String messageEndpoint ) {
124
+ this (objectMapper , messageEndpoint , DEFAULT_SSE_ENDPOINT );
125
+ }
126
+
105
127
/**
106
128
* Constructs a new WebFlux SSE server transport provider instance.
107
129
* @param objectMapper The ObjectMapper to use for JSON serialization/deserialization
@@ -112,11 +134,28 @@ public class WebFluxSseServerTransportProvider implements McpServerTransportProv
112
134
* @throws IllegalArgumentException if either parameter is null
113
135
*/
114
136
public WebFluxSseServerTransportProvider (ObjectMapper objectMapper , String messageEndpoint , String sseEndpoint ) {
137
+ this (objectMapper , DEFAULT_BASE_URL , messageEndpoint , sseEndpoint );
138
+ }
139
+
140
+ /**
141
+ * Constructs a new WebFlux SSE server transport provider instance.
142
+ * @param objectMapper The ObjectMapper to use for JSON serialization/deserialization
143
+ * of MCP messages. Must not be null.
144
+ * @param baseUrl webflux messag base path
145
+ * @param messageEndpoint The endpoint URI where clients should send their JSON-RPC
146
+ * messages. This endpoint will be communicated to clients during SSE connection
147
+ * setup. Must not be null.
148
+ * @throws IllegalArgumentException if either parameter is null
149
+ */
150
+ public WebFluxSseServerTransportProvider (ObjectMapper objectMapper , String baseUrl , String messageEndpoint ,
151
+ String sseEndpoint ) {
115
152
Assert .notNull (objectMapper , "ObjectMapper must not be null" );
153
+ Assert .notNull (baseUrl , "Message base path must not be null" );
116
154
Assert .notNull (messageEndpoint , "Message endpoint must not be null" );
117
155
Assert .notNull (sseEndpoint , "SSE endpoint must not be null" );
118
156
119
157
this .objectMapper = objectMapper ;
158
+ this .baseUrl = baseUrl ;
120
159
this .messageEndpoint = messageEndpoint ;
121
160
this .sseEndpoint = sseEndpoint ;
122
161
this .routerFunction = RouterFunctions .route ()
@@ -125,20 +164,6 @@ public WebFluxSseServerTransportProvider(ObjectMapper objectMapper, String messa
125
164
.build ();
126
165
}
127
166
128
- /**
129
- * Constructs a new WebFlux SSE server transport provider instance with the default
130
- * SSE endpoint.
131
- * @param objectMapper The ObjectMapper to use for JSON serialization/deserialization
132
- * of MCP messages. Must not be null.
133
- * @param messageEndpoint The endpoint URI where clients should send their JSON-RPC
134
- * messages. This endpoint will be communicated to clients during SSE connection
135
- * setup. Must not be null.
136
- * @throws IllegalArgumentException if either parameter is null
137
- */
138
- public WebFluxSseServerTransportProvider (ObjectMapper objectMapper , String messageEndpoint ) {
139
- this (objectMapper , messageEndpoint , DEFAULT_SSE_ENDPOINT );
140
- }
141
-
142
167
@ Override
143
168
public void setSessionFactory (McpServerSession .Factory sessionFactory ) {
144
169
this .sessionFactory = sessionFactory ;
@@ -179,7 +204,8 @@ public Mono<Void> notifyClients(String method, Map<String, Object> params) {
179
204
.then ();
180
205
}
181
206
182
- // FIXME: This javadoc makes claims about using isClosing flag but it's not actually
207
+ // FIXME: This javadoc makes claims about using isClosing flag but it's not
208
+ // actually
183
209
// doing that.
184
210
/**
185
211
* Initiates a graceful shutdown of all the sessions. This method ensures all active
@@ -245,7 +271,7 @@ private Mono<ServerResponse> handleSseConnection(ServerRequest request) {
245
271
logger .debug ("Sending initial endpoint event to session: {}" , sessionId );
246
272
sink .next (ServerSentEvent .builder ()
247
273
.event (ENDPOINT_EVENT_TYPE )
248
- .data (messageEndpoint + "?sessionId=" + sessionId )
274
+ .data (this . baseUrl + this . messageEndpoint + "?sessionId=" + sessionId )
249
275
.build ());
250
276
sink .onCancel (() -> {
251
277
logger .debug ("Session {} cancelled" , sessionId );
@@ -360,6 +386,8 @@ public static class Builder {
360
386
361
387
private ObjectMapper objectMapper ;
362
388
389
+ private String baseUrl = DEFAULT_BASE_URL ;
390
+
363
391
private String messageEndpoint ;
364
392
365
393
private String sseEndpoint = DEFAULT_SSE_ENDPOINT ;
@@ -377,6 +405,19 @@ public Builder objectMapper(ObjectMapper objectMapper) {
377
405
return this ;
378
406
}
379
407
408
+ /**
409
+ * Sets the project basePath as endpoint prefix where clients should send their
410
+ * JSON-RPC messages
411
+ * @param baseUrl the message basePath . Must not be null.
412
+ * @return this builder instance
413
+ * @throws IllegalArgumentException if basePath is null
414
+ */
415
+ public Builder basePath (String baseUrl ) {
416
+ Assert .notNull (baseUrl , "basePath must not be null" );
417
+ this .baseUrl = baseUrl ;
418
+ return this ;
419
+ }
420
+
380
421
/**
381
422
* Sets the endpoint URI where clients should send their JSON-RPC messages.
382
423
* @param messageEndpoint The message endpoint URI. Must not be null.
@@ -411,7 +452,7 @@ public WebFluxSseServerTransportProvider build() {
411
452
Assert .notNull (objectMapper , "ObjectMapper must be set" );
412
453
Assert .notNull (messageEndpoint , "Message endpoint must be set" );
413
454
414
- return new WebFluxSseServerTransportProvider (objectMapper , messageEndpoint , sseEndpoint );
455
+ return new WebFluxSseServerTransportProvider (objectMapper , baseUrl , messageEndpoint , sseEndpoint );
415
456
}
416
457
417
458
}
0 commit comments