FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Send Mcp-Method header on Streamable HTTP client requests (SEP-2243) by nikita-kibitkin · Pull Request #1026 · modelcontextprotocol/java-sdk · GitHub

Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .java  (3) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,13 @@ public Mono<Void> sendMessage(McpSchema.JSONRPCMessage sentMessage) {
transportSession.sessionId().get());
}

if (sentMessage instanceof McpSchema.JSONRPCRequest request) {
requestBuilder = requestBuilder.header(HttpHeaders.MCP_METHOD, request.method());
}
else if (sentMessage instanceof McpSchema.JSONRPCNotification notification) {
requestBuilder = requestBuilder.header(HttpHeaders.MCP_METHOD, notification.method());
}

var builder = requestBuilder.uri(uri)
.header(HttpHeaders.ACCEPT, APPLICATION_JSON + ", " + TEXT_EVENT_STREAM)
.header(HttpHeaders.CONTENT_TYPE, APPLICATION_JSON_UTF8)
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ public interface HttpHeaders {
*/
String PROTOCOL_VERSION = "MCP-Protocol-Version";

/**
* Mirrors the JSON-RPC method of the request or notification carried in the body.
* @see <a href=
* "https://modelcontextprotocol.io/seps/2243-http-standardization">SEP-2243</a>
*/
String MCP_METHOD = "Mcp-Method";

/**
* The HTTP Content-Length header.
* @see <a href=
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,27 @@
import io.modelcontextprotocol.client.transport.customizer.McpAsyncHttpClientRequestCustomizer;
import io.modelcontextprotocol.client.transport.customizer.McpSyncHttpClientRequestCustomizer;
import io.modelcontextprotocol.common.McpTransportContext;
import io.modelcontextprotocol.spec.HttpHeaders;
import io.modelcontextprotocol.spec.McpSchema;
import io.modelcontextprotocol.spec.McpTransportSessionClosedException;
import io.modelcontextprotocol.spec.ProtocolVersions;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.http.HttpRequest;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.wait.strategy.Wait;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.atLeastOnce;
Expand Down Expand Up @@ -166,4 +170,99 @@ void testCloseInitialized() {
.verify();
}

@Test
void testMcpMethodHeaderOnRequest() throws URISyntaxException {
var uri = new URI(host + "/mcp");
var mockRequestCustomizer = mock(McpSyncHttpClientRequestCustomizer.class);

var transport = HttpClientStreamableHttpTransport.builder(host)
.httpRequestCustomizer(mockRequestCustomizer)
.build();

withTransport(transport, (t) -> {
var initializeRequest = McpSchema.InitializeRequest
.builder(ProtocolVersions.MCP_2025_11_25, McpSchema.ClientCapabilities.builder().roots(true).build(),
McpSchema.Implementation.builder("MCP Client", "0.3.1").build())
.build();
var testMessage = new McpSchema.JSONRPCRequest(McpSchema.METHOD_INITIALIZE, "test-id", initializeRequest);

StepVerifier
.create(t.sendMessage(testMessage).contextWrite(ctx -> ctx.put(McpTransportContext.KEY, context)))
.verifyComplete();

var requestCaptor = ArgumentCaptor.forClass(HttpRequest.Builder.class);
verify(mockRequestCustomizer, atLeastOnce()).customize(requestCaptor.capture(), eq("POST"), eq(uri), any(),
eq(context));
assertThat(requestCaptor.getValue().build().headers().firstValue(HttpHeaders.MCP_METHOD))
.hasValue(McpSchema.METHOD_INITIALIZE);
});
}

@Test
void testMcpMethodHeaderOnNotification() throws URISyntaxException {
var uri = new URI(host + "/mcp");
var mockRequestCustomizer = mock(McpSyncHttpClientRequestCustomizer.class);

var transport = HttpClientStreamableHttpTransport.builder(host)
.httpRequestCustomizer(mockRequestCustomizer)
.build();

withTransport(transport, (t) -> {
var initializeRequest = McpSchema.InitializeRequest
.builder(ProtocolVersions.MCP_2025_11_25, McpSchema.ClientCapabilities.builder().roots(true).build(),
McpSchema.Implementation.builder("MCP Client", "0.3.1").build())
.build();
var initializeMessage = new McpSchema.JSONRPCRequest(McpSchema.METHOD_INITIALIZE, "test-id",
initializeRequest);
StepVerifier
.create(t.sendMessage(initializeMessage).contextWrite(ctx -> ctx.put(McpTransportContext.KEY, context)))
.verifyComplete();

var testMessage = new McpSchema.JSONRPCNotification(McpSchema.METHOD_NOTIFICATION_INITIALIZED);

StepVerifier
.create(t.sendMessage(testMessage).contextWrite(ctx -> ctx.put(McpTransportContext.KEY, context)))
.verifyComplete();

var requestCaptor = ArgumentCaptor.forClass(HttpRequest.Builder.class);
verify(mockRequestCustomizer, atLeastOnce()).customize(requestCaptor.capture(), eq("POST"), eq(uri), any(),
eq(context));
assertThat(requestCaptor.getValue().build().headers().firstValue(HttpHeaders.MCP_METHOD))
.hasValue(McpSchema.METHOD_NOTIFICATION_INITIALIZED);
});
}

@Test
void testNoMcpMethodHeaderOnResponse() throws URISyntaxException {
var uri = new URI(host + "/mcp");
var mockRequestCustomizer = mock(McpSyncHttpClientRequestCustomizer.class);

var transport = HttpClientStreamableHttpTransport.builder(host)
.httpRequestCustomizer(mockRequestCustomizer)
.build();

withTransport(transport, (t) -> {
var initializeRequest = McpSchema.InitializeRequest
.builder(ProtocolVersions.MCP_2025_11_25, McpSchema.ClientCapabilities.builder().roots(true).build(),
McpSchema.Implementation.builder("MCP Client", "0.3.1").build())
.build();
var initializeMessage = new McpSchema.JSONRPCRequest(McpSchema.METHOD_INITIALIZE, "test-id",
initializeRequest);
StepVerifier
.create(t.sendMessage(initializeMessage).contextWrite(ctx -> ctx.put(McpTransportContext.KEY, context)))
.verifyComplete();

var testMessage = McpSchema.JSONRPCResponse.result("test-id-2", Map.of());

StepVerifier
.create(t.sendMessage(testMessage).contextWrite(ctx -> ctx.put(McpTransportContext.KEY, context)))
.verifyComplete();

var requestCaptor = ArgumentCaptor.forClass(HttpRequest.Builder.class);
verify(mockRequestCustomizer, atLeastOnce()).customize(requestCaptor.capture(), eq("POST"), eq(uri), any(),
eq(context));
assertThat(requestCaptor.getValue().build().headers().firstValue(HttpHeaders.MCP_METHOD)).isEmpty();
});
}

}

Back | FazBrowse Home | New Git URL