|
| 1 | +/* |
| 2 | + * Copyright 2024-2025 the original author or authors. |
| 3 | + */ |
| 4 | + |
| 5 | +package io.modelcontextprotocol.client.transport; |
| 6 | + |
| 7 | +import com.sun.net.httpserver.HttpServer; |
| 8 | +import io.modelcontextprotocol.spec.McpSchema; |
| 9 | +import org.junit.jupiter.api.AfterAll; |
| 10 | +import org.junit.jupiter.api.BeforeAll; |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | +import org.junit.jupiter.api.Timeout; |
| 13 | +import reactor.test.StepVerifier; |
| 14 | + |
| 15 | +import java.io.IOException; |
| 16 | +import java.net.InetSocketAddress; |
| 17 | +import java.net.URI; |
| 18 | +import java.net.URISyntaxException; |
| 19 | +import java.util.concurrent.TimeUnit; |
| 20 | +import java.util.function.Consumer; |
| 21 | + |
| 22 | +import static org.mockito.Mockito.mock; |
| 23 | +import static org.mockito.ArgumentMatchers.any; |
| 24 | +import static org.mockito.ArgumentMatchers.eq; |
| 25 | +import static org.mockito.Mockito.atLeastOnce; |
| 26 | +import static org.mockito.Mockito.verify; |
| 27 | + |
| 28 | +/** |
| 29 | + * Tests for the "Client failed to initialize by explicit API call" problem. |
| 30 | + * |
| 31 | + * @author codezkk |
| 32 | + */ |
| 33 | +public class HttpClientStreamableHttpTransportFor200Test { |
| 34 | + |
| 35 | + static String host = "http://localhost:3001"; |
| 36 | + |
| 37 | + static HttpServer server; |
| 38 | + |
| 39 | + @BeforeAll |
| 40 | + static void startContainer() throws IOException { |
| 41 | + server = HttpServer.create(new InetSocketAddress(3001), 0); |
| 42 | + server.createContext("/mcp", exchange -> { |
| 43 | + exchange.getResponseHeaders().set("Content-Type", "application/json"); |
| 44 | + exchange.sendResponseHeaders(200, 0); |
| 45 | + exchange.close(); |
| 46 | + }); |
| 47 | + server.setExecutor(null); |
| 48 | + server.start(); |
| 49 | + } |
| 50 | + |
| 51 | + @AfterAll |
| 52 | + static void stopContainer() { |
| 53 | + server.stop(1); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Regardless of the response (even if the response is null and the content-type is |
| 58 | + * present), notify should handle it correctly. |
| 59 | + * @throws URISyntaxException |
| 60 | + */ |
| 61 | + @Test |
| 62 | + @Timeout(value = 3, unit = TimeUnit.SECONDS) |
| 63 | + void testNotificationInitialized() throws URISyntaxException { |
| 64 | + |
| 65 | + var uri = new URI(host + "/mcp"); |
| 66 | + var mockRequestCustomizer = mock(SyncHttpRequestCustomizer.class); |
| 67 | + var transport = HttpClientStreamableHttpTransport.builder(host) |
| 68 | + .httpRequestCustomizer(mockRequestCustomizer) |
| 69 | + .build(); |
| 70 | + |
| 71 | + withTransport(transport, (t) -> { |
| 72 | + var testNotification = new McpSchema.JSONRPCNotification(McpSchema.JSONRPC_VERSION, |
| 73 | + McpSchema.METHOD_NOTIFICATION_INITIALIZED, null); |
| 74 | + |
| 75 | + StepVerifier.create(t.sendMessage(testNotification)).verifyComplete(); |
| 76 | + |
| 77 | + verify(mockRequestCustomizer, atLeastOnce()).customize(any(), eq("GET"), eq(uri), |
| 78 | + eq("{\"jsonrpc\":\"2.0\",\"method\":\"notifications/initialized\"}")); |
| 79 | + }); |
| 80 | + } |
| 81 | + |
| 82 | + void withTransport(HttpClientStreamableHttpTransport transport, Consumer<HttpClientStreamableHttpTransport> c) { |
| 83 | + try { |
| 84 | + c.accept(transport); |
| 85 | + } |
| 86 | + finally { |
| 87 | + StepVerifier.create(transport.closeGracefully()).verifyComplete(); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | +} |
0 commit comments