|
| 1 | +/* |
| 2 | + * Copyright 2024-2025 the original author or authors. |
| 3 | + */ |
| 4 | + |
| 5 | +package io.modelcontextprotocol.client.transport; |
| 6 | + |
| 7 | +import static org.mockito.ArgumentMatchers.any; |
| 8 | +import static org.mockito.ArgumentMatchers.eq; |
| 9 | +import static org.mockito.Mockito.atLeastOnce; |
| 10 | +import static org.mockito.Mockito.mock; |
| 11 | +import static org.mockito.Mockito.verify; |
| 12 | + |
| 13 | +import java.io.IOException; |
| 14 | +import java.net.InetSocketAddress; |
| 15 | +import java.net.URI; |
| 16 | +import java.net.URISyntaxException; |
| 17 | + |
| 18 | +import org.junit.jupiter.api.AfterAll; |
| 19 | +import org.junit.jupiter.api.BeforeAll; |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | +import org.junit.jupiter.api.Timeout; |
| 22 | + |
| 23 | +import com.sun.net.httpserver.HttpServer; |
| 24 | + |
| 25 | +import io.modelcontextprotocol.server.transport.TomcatTestUtil; |
| 26 | +import io.modelcontextprotocol.spec.McpSchema; |
| 27 | +import io.modelcontextprotocol.spec.ProtocolVersions; |
| 28 | +import reactor.test.StepVerifier; |
| 29 | + |
| 30 | +/** |
| 31 | + * Handles emplty application/json response with 200 OK status code. |
| 32 | + * |
| 33 | + * @author codezkk |
| 34 | + */ |
| 35 | +public class HttpClientStreamableHttpTransportEmptyJsonResponseTest { |
| 36 | + |
| 37 | + static int PORT = TomcatTestUtil.findAvailablePort(); |
| 38 | + |
| 39 | + static String host = "http://localhost:" + PORT; |
| 40 | + |
| 41 | + static HttpServer server; |
| 42 | + |
| 43 | + @BeforeAll |
| 44 | + static void startContainer() throws IOException { |
| 45 | + |
| 46 | + server = HttpServer.create(new InetSocketAddress(PORT), 0); |
| 47 | + |
| 48 | + // Empty, 200 OK response for the /mcp endpoint |
| 49 | + server.createContext("/mcp", exchange -> { |
| 50 | + exchange.getResponseHeaders().set("Content-Type", "application/json"); |
| 51 | + exchange.sendResponseHeaders(200, 0); |
| 52 | + exchange.close(); |
| 53 | + }); |
| 54 | + |
| 55 | + server.setExecutor(null); |
| 56 | + server.start(); |
| 57 | + } |
| 58 | + |
| 59 | + @AfterAll |
| 60 | + static void stopContainer() { |
| 61 | + server.stop(1); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Regardless of the response (even if the response is null and the content-type is |
| 66 | + * present), notify should handle it correctly. |
| 67 | + */ |
| 68 | + @Test |
| 69 | + @Timeout(3) |
| 70 | + void testNotificationInitialized() throws URISyntaxException { |
| 71 | + |
| 72 | + var uri = new URI(host + "/mcp"); |
| 73 | + var mockRequestCustomizer = mock(SyncHttpRequestCustomizer.class); |
| 74 | + var transport = HttpClientStreamableHttpTransport.builder(host) |
| 75 | + .httpRequestCustomizer(mockRequestCustomizer) |
| 76 | + .build(); |
| 77 | + |
| 78 | + var initializeRequest = new McpSchema.InitializeRequest(ProtocolVersions.MCP_2025_03_26, |
| 79 | + McpSchema.ClientCapabilities.builder().roots(true).build(), |
| 80 | + new McpSchema.Implementation("Spring AI MCP Client", "0.3.1")); |
| 81 | + var testMessage = new McpSchema.JSONRPCRequest(McpSchema.JSONRPC_VERSION, McpSchema.METHOD_INITIALIZE, |
| 82 | + "test-id", initializeRequest); |
| 83 | + |
| 84 | + StepVerifier.create(transport.sendMessage(testMessage)).verifyComplete(); |
| 85 | + |
| 86 | + // Verify the customizer was called |
| 87 | + verify(mockRequestCustomizer, atLeastOnce()).customize(any(), eq("GET"), eq(uri), eq( |
| 88 | + "{\"jsonrpc\":\"2.0\",\"method\":\"initialize\",\"id\":\"test-id\",\"params\":{\"protocolVersion\":\"2025-03-26\",\"capabilities\":{\"roots\":{\"listChanged\":true}},\"clientInfo\":{\"name\":\"Spring AI MCP Client\",\"version\":\"0.3.1\"}}}")); |
| 89 | + |
| 90 | + } |
| 91 | + |
| 92 | +} |
0 commit comments