|
| 1 | +/* |
| 2 | + * Copyright 2024-2025 the original author or authors. |
| 3 | + */ |
| 4 | +package io.modelcontextprotocol.client.transport; |
| 5 | + |
| 6 | +import io.modelcontextprotocol.spec.McpSchema; |
| 7 | +import org.junit.jupiter.api.AfterAll; |
| 8 | +import org.junit.jupiter.api.BeforeAll; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | +import org.testcontainers.containers.GenericContainer; |
| 11 | +import org.testcontainers.containers.wait.strategy.Wait; |
| 12 | +import reactor.test.StepVerifier; |
| 13 | + |
| 14 | +import org.springframework.web.reactive.function.client.WebClient; |
| 15 | + |
| 16 | +class WebClientStreamableHttpTransportTest { |
| 17 | + |
| 18 | + static String host = "http://localhost:3001"; |
| 19 | + |
| 20 | + static WebClient.Builder builder; |
| 21 | + |
| 22 | + @SuppressWarnings("resource") |
| 23 | + static GenericContainer<?> container = new GenericContainer<>("docker.io/tzolov/mcp-everything-server:v3") |
| 24 | + .withCommand("node dist/index.js streamableHttp") |
| 25 | + .withLogConsumer(outputFrame -> System.out.println(outputFrame.getUtf8String())) |
| 26 | + .withExposedPorts(3001) |
| 27 | + .waitingFor(Wait.forHttp("/").forStatusCode(404)); |
| 28 | + |
| 29 | + @BeforeAll |
| 30 | + static void startContainer() { |
| 31 | + container.start(); |
| 32 | + int port = container.getMappedPort(3001); |
| 33 | + host = "http://" + container.getHost() + ":" + port; |
| 34 | + builder = WebClient.builder().baseUrl(host); |
| 35 | + } |
| 36 | + |
| 37 | + @AfterAll |
| 38 | + static void stopContainer() { |
| 39 | + container.stop(); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + void testCloseUninitialized() { |
| 44 | + var transport = WebClientStreamableHttpTransport.builder(builder).build(); |
| 45 | + |
| 46 | + StepVerifier.create(transport.closeGracefully()).verifyComplete(); |
| 47 | + |
| 48 | + var initializeRequest = new McpSchema.InitializeRequest(McpSchema.LATEST_PROTOCOL_VERSION, |
| 49 | + McpSchema.ClientCapabilities.builder().roots(true).build(), |
| 50 | + new McpSchema.Implementation("Spring AI MCP Client", "0.3.1")); |
| 51 | + var testMessage = new McpSchema.JSONRPCRequest(McpSchema.JSONRPC_VERSION, McpSchema.METHOD_INITIALIZE, |
| 52 | + "test-id", initializeRequest); |
| 53 | + |
| 54 | + StepVerifier.create(transport.sendMessage(testMessage)) |
| 55 | + .expectErrorMessage("MCP session has been closed") |
| 56 | + .verify(); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + void testCloseInitialized() { |
| 61 | + var transport = WebClientStreamableHttpTransport.builder(builder).build(); |
| 62 | + |
| 63 | + var initializeRequest = new McpSchema.InitializeRequest(McpSchema.LATEST_PROTOCOL_VERSION, |
| 64 | + McpSchema.ClientCapabilities.builder().roots(true).build(), |
| 65 | + new McpSchema.Implementation("Spring AI MCP Client", "0.3.1")); |
| 66 | + var testMessage = new McpSchema.JSONRPCRequest(McpSchema.JSONRPC_VERSION, McpSchema.METHOD_INITIALIZE, |
| 67 | + "test-id", initializeRequest); |
| 68 | + |
| 69 | + StepVerifier.create(transport.sendMessage(testMessage)).verifyComplete(); |
| 70 | + StepVerifier.create(transport.closeGracefully()).verifyComplete(); |
| 71 | + |
| 72 | + StepVerifier.create(transport.sendMessage(testMessage)) |
| 73 | + .expectErrorMatches(err -> err.getMessage().matches("MCP session with ID [a-zA-Z0-9-]* has been closed")) |
| 74 | + .verify(); |
| 75 | + } |
| 76 | + |
| 77 | +} |
0 commit comments