Skip to content

WIP Streamable HTTP #1

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
Show file tree
Hide file tree
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 @@ -3,6 +3,8 @@
*/
package io.modelcontextprotocol.client.transport;

import static io.modelcontextprotocol.spec.McpStreamableHttpClient.*;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
Expand Down Expand Up @@ -120,12 +122,28 @@ public FlowSseClient(HttpClient httpClient, HttpRequest.Builder requestBuilder)
* notifications
* @throws RuntimeException if the connection fails with a non-200 status code
*/
public void subscribe(String url, SseEventHandler eventHandler) {
HttpRequest request = this.requestBuilder.uri(URI.create(url))
.header("Accept", "text/event-stream")
public void subscribe(String url, String mcpSessionId, SseEventHandler eventHandler) {
HttpRequest.Builder requestBuilder = this.requestBuilder.copy()
.uri(URI.create(url))
.header("Cache-Control", "no-cache")
.GET()
.build();
.GET();
if (mcpSessionId != null) { // Using StreamableHTTP Transport
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (complexity): Consider refactoring header-setting logic to build the request only twice and apply missing headers in a single loop.

// 1. Copy & set the URI/GET only once
HttpRequest.Builder rb = this.requestBuilder.copy()
    .uri(URI.create(url))
    .header("Cache-Control", "no-cache")
    .GET();

// 2. Snapshot existing header names in one build()
Set<String> existing = rb.build().headers().map().keySet();

// 3. Prepare the defaults per transport
List<Map.Entry<String,String>> defaults;
if (mcpSessionId != null) {
    defaults = List.of(
      Map.entry(ACCEPT_HEADER_NAME,      ACCEPT_HEADER_VALUE),
      Map.entry(CONTENT_TYPE_HEADER_NAME, CONTENT_TYPE_HEADER_VALUE),
      Map.entry(MCP_SESSION_ID_HEADER_NAME, mcpSessionId)
    );
} else {
    defaults = List.of(
      Map.entry(ACCEPT_HEADER_NAME, "text/event-stream")
    );
}

// 4. Apply only the missing headers
for (var hdr : defaults) {
  if (!existing.contains(hdr.getKey())) {
    rb.header(hdr.getKey(), hdr.getValue());
  }
}

// 5. Final build
HttpRequest request = rb.build();

Benefits:

  • Builds the request only twice (once for header-snapshot, once final) instead of N times.
  • Flattens nested if-blocks into a single loop over defaults.
  • Maintains identical header logic and ordering.

if (!requestBuilder.build().headers().map().containsKey(ACCEPT_HEADER_NAME)) {
requestBuilder.header(ACCEPT_HEADER_NAME, ACCEPT_HEADER_GET_VALUE);
}
if (!requestBuilder.build().headers().map().containsKey(CONTENT_TYPE_HEADER_NAME)) {
requestBuilder.header(CONTENT_TYPE_HEADER_NAME, CONTENT_TYPE_HEADER_VALUE);
}
if (!requestBuilder.build().headers().map().containsKey(MCP_SESSION_ID_HEADER_NAME)) {
requestBuilder.header(MCP_SESSION_ID_HEADER_NAME, mcpSessionId);
}
}
else { // Using HTTP+SSE Transport
if (!requestBuilder.build().headers().map().containsKey(ACCEPT_HEADER_NAME)) {
requestBuilder.header(ACCEPT_HEADER_NAME, "text/event-stream");
}
}
HttpRequest request = requestBuilder.build();

StringBuilder eventBuilder = new StringBuilder();
AtomicReference<String> currentEventId = new AtomicReference<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ public Mono<Void> connect(Function<Mono<JSONRPCMessage>, Mono<JSONRPCMessage>> h
connectionFuture.set(future);

URI clientUri = Utils.resolveUri(this.baseUri, this.sseEndpoint);
sseClient.subscribe(clientUri.toString(), new FlowSseClient.SseEventHandler() {
sseClient.subscribe(clientUri.toString(), null, new FlowSseClient.SseEventHandler() {
@Override
public void onEvent(SseEvent event) {
if (isClosing) {
Expand Down
Loading