Skip to content

feat(mcp): webflux support custom http header #169

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 @@ -5,6 +5,7 @@

import java.io.IOException;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;

import com.fasterxml.jackson.core.type.TypeReference;
Expand All @@ -16,6 +17,7 @@
import io.modelcontextprotocol.util.Assert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import reactor.core.Disposable;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
Expand Down Expand Up @@ -235,6 +237,24 @@ else if (MESSAGE_EVENT_TYPE.equals(event.event())) {
*/
@Override
public Mono<Void> sendMessage(JSONRPCMessage message) {
return sendMessage(message, httpHeaders -> {
});
}

/**
* Sends a JSON-RPC message to the server using the endpoint provided during
* connection.
*
* <p>
* Messages are sent via HTTP POST requests to the server-provided endpoint URI. The
* message is serialized to JSON before transmission. If the transport is in the
* process of closing, the message send operation is skipped gracefully.
* @param message the JSON-RPC message to send
* @param modifiableHeaders a consumer that allows modifying the HTTP headers
* @return a Mono that completes when the message has been sent successfully
* @throws RuntimeException if message serialization fails
*/
public Mono<Void> sendMessage(JSONRPCMessage message, Consumer<HttpHeaders> modifiableHeaders) {
// The messageEndpoint is the endpoint URI to send the messages
// It is provided by the server as part of the endpoint event
return messageEndpointSink.asMono().flatMap(messageEndpointUri -> {
Expand All @@ -245,6 +265,7 @@ public Mono<Void> sendMessage(JSONRPCMessage message) {
String jsonText = this.objectMapper.writeValueAsString(message);
return webClient.post()
.uri(messageEndpointUri)
.headers(modifiableHeaders)
.contentType(MediaType.APPLICATION_JSON)
.bodyValue(jsonText)
.retrieve()
Expand Down