|
| 1 | +/* |
| 2 | + * Copyright 2024 - 2024 the original author or authors. |
| 3 | + */ |
| 4 | + |
| 5 | +package io.modelcontextprotocol.server.transport; |
| 6 | + |
| 7 | +import io.modelcontextprotocol.client.McpClient; |
| 8 | +import io.modelcontextprotocol.client.transport.HttpClientSseClientTransport; |
| 9 | +import io.modelcontextprotocol.common.McpTransportContext; |
| 10 | +import io.modelcontextprotocol.json.McpJsonMapper; |
| 11 | +import io.modelcontextprotocol.server.McpServer; |
| 12 | +import io.modelcontextprotocol.server.TestUtil; |
| 13 | +import io.modelcontextprotocol.server.TomcatTestUtil; |
| 14 | +import io.modelcontextprotocol.spec.McpSchema; |
| 15 | +import org.apache.catalina.LifecycleException; |
| 16 | +import org.apache.catalina.LifecycleState; |
| 17 | +import org.junit.jupiter.api.AfterEach; |
| 18 | +import org.junit.jupiter.api.BeforeEach; |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | + |
| 21 | +import org.springframework.context.annotation.Bean; |
| 22 | +import org.springframework.context.annotation.Configuration; |
| 23 | +import org.springframework.web.servlet.config.annotation.EnableWebMvc; |
| 24 | +import org.springframework.web.servlet.function.RouterFunction; |
| 25 | +import org.springframework.web.servlet.function.ServerResponse; |
| 26 | + |
| 27 | +import static org.assertj.core.api.Assertions.assertThat; |
| 28 | + |
| 29 | +/** |
| 30 | + * Integration tests for WebMvcSseServerTransportProvider |
| 31 | + * |
| 32 | + * @author lance |
| 33 | + */ |
| 34 | +class WebMvcSseServerTransportProviderTests { |
| 35 | + |
| 36 | + private static final int PORT = TestUtil.findAvailablePort(); |
| 37 | + |
| 38 | + private static final String CUSTOM_CONTEXT_PATH = "/"; |
| 39 | + |
| 40 | + private static final String MESSAGE_ENDPOINT = "/mcp/message"; |
| 41 | + |
| 42 | + private WebMvcSseServerTransportProvider mcpServerTransportProvider; |
| 43 | + |
| 44 | + McpClient.SyncSpec clientBuilder; |
| 45 | + |
| 46 | + private TomcatTestUtil.TomcatServer tomcatServer; |
| 47 | + |
| 48 | + @BeforeEach |
| 49 | + public void before() { |
| 50 | + tomcatServer = TomcatTestUtil.createTomcatServer(CUSTOM_CONTEXT_PATH, PORT, TestConfig.class); |
| 51 | + |
| 52 | + try { |
| 53 | + tomcatServer.tomcat().start(); |
| 54 | + assertThat(tomcatServer.tomcat().getServer().getState()).isEqualTo(LifecycleState.STARTED); |
| 55 | + } |
| 56 | + catch (Exception e) { |
| 57 | + throw new RuntimeException("Failed to start Tomcat", e); |
| 58 | + } |
| 59 | + |
| 60 | + HttpClientSseClientTransport transport = HttpClientSseClientTransport.builder("http://localhost:" + PORT) |
| 61 | + .sseEndpoint(WebMvcSseServerTransportProvider.DEFAULT_SSE_ENDPOINT) |
| 62 | + .build(); |
| 63 | + |
| 64 | + clientBuilder = McpClient.sync(transport); |
| 65 | + mcpServerTransportProvider = tomcatServer.appContext().getBean(WebMvcSseServerTransportProvider.class); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + void validBaseUrl() { |
| 70 | + McpServer.async(mcpServerTransportProvider).serverInfo("test-server", "1.0.0").build(); |
| 71 | + try (var client = clientBuilder.clientInfo(new McpSchema.Implementation("Sample " + "client", "0.0.0")) |
| 72 | + .build()) { |
| 73 | + assertThat(client.initialize()).isNotNull(); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + @AfterEach |
| 78 | + public void after() { |
| 79 | + if (mcpServerTransportProvider != null) { |
| 80 | + mcpServerTransportProvider.closeGracefully().block(); |
| 81 | + } |
| 82 | + if (tomcatServer.appContext() != null) { |
| 83 | + tomcatServer.appContext().close(); |
| 84 | + } |
| 85 | + if (tomcatServer.tomcat() != null) { |
| 86 | + try { |
| 87 | + tomcatServer.tomcat().stop(); |
| 88 | + tomcatServer.tomcat().destroy(); |
| 89 | + } |
| 90 | + catch (LifecycleException e) { |
| 91 | + throw new RuntimeException("Failed to stop Tomcat", e); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + @Configuration |
| 97 | + @EnableWebMvc |
| 98 | + static class TestConfig { |
| 99 | + |
| 100 | + @Bean |
| 101 | + public WebMvcSseServerTransportProvider webMvcSseServerTransportProvider() { |
| 102 | + |
| 103 | + return WebMvcSseServerTransportProvider.builder() |
| 104 | + .baseUrl("http://localhost:" + PORT + "/") |
| 105 | + .messageEndpoint(MESSAGE_ENDPOINT) |
| 106 | + .sseEndpoint(WebMvcSseServerTransportProvider.DEFAULT_SSE_ENDPOINT) |
| 107 | + .jsonMapper(McpJsonMapper.getDefault()) |
| 108 | + .contextExtractor(req -> McpTransportContext.EMPTY) |
| 109 | + .build(); |
| 110 | + } |
| 111 | + |
| 112 | + @Bean |
| 113 | + public RouterFunction<ServerResponse> routerFunction(WebMvcSseServerTransportProvider transportProvider) { |
| 114 | + return transportProvider.getRouterFunction(); |
| 115 | + } |
| 116 | + |
| 117 | + } |
| 118 | + |
| 119 | +} |
0 commit comments