5
5
6
6
import java .io .IOException ;
7
7
import java .util .function .BiConsumer ;
8
+ import java .util .function .Consumer ;
8
9
import java .util .function .Function ;
9
10
10
11
import com .fasterxml .jackson .core .type .TypeReference ;
16
17
import io .modelcontextprotocol .util .Assert ;
17
18
import org .slf4j .Logger ;
18
19
import org .slf4j .LoggerFactory ;
20
+ import org .springframework .http .HttpHeaders ;
19
21
import reactor .core .Disposable ;
20
22
import reactor .core .publisher .Flux ;
21
23
import reactor .core .publisher .Mono ;
@@ -235,6 +237,24 @@ else if (MESSAGE_EVENT_TYPE.equals(event.event())) {
235
237
*/
236
238
@ Override
237
239
public Mono <Void > sendMessage (JSONRPCMessage message ) {
240
+ return sendMessage (message , httpHeaders -> {
241
+ });
242
+ }
243
+
244
+ /**
245
+ * Sends a JSON-RPC message to the server using the endpoint provided during
246
+ * connection.
247
+ *
248
+ * <p>
249
+ * Messages are sent via HTTP POST requests to the server-provided endpoint URI. The
250
+ * message is serialized to JSON before transmission. If the transport is in the
251
+ * process of closing, the message send operation is skipped gracefully.
252
+ * @param message the JSON-RPC message to send
253
+ * @param modifiableHeaders a consumer that allows modifying the HTTP headers
254
+ * @return a Mono that completes when the message has been sent successfully
255
+ * @throws RuntimeException if message serialization fails
256
+ */
257
+ public Mono <Void > sendMessage (JSONRPCMessage message , Consumer <HttpHeaders > modifiableHeaders ) {
238
258
// The messageEndpoint is the endpoint URI to send the messages
239
259
// It is provided by the server as part of the endpoint event
240
260
return messageEndpointSink .asMono ().flatMap (messageEndpointUri -> {
@@ -245,6 +265,7 @@ public Mono<Void> sendMessage(JSONRPCMessage message) {
245
265
String jsonText = this .objectMapper .writeValueAsString (message );
246
266
return webClient .post ()
247
267
.uri (messageEndpointUri )
268
+ .headers (modifiableHeaders )
248
269
.contentType (MediaType .APPLICATION_JSON )
249
270
.bodyValue (jsonText )
250
271
.retrieve ()
0 commit comments