Skip to content

[WIP] Streamable HTTP #289

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

Draft
wants to merge 1 commit into
base: wip-str-h
Choose a base branch
from
Draft
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
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