|
| 1 | +/* |
| 2 | + * Copyright 2026-2026 the original author or authors. |
| 3 | + */ |
| 4 | + |
| 5 | +package io.modelcontextprotocol.server.transport; |
| 6 | + |
| 7 | +import java.net.URI; |
| 8 | +import java.net.http.HttpClient; |
| 9 | +import java.net.http.HttpRequest; |
| 10 | +import java.net.http.HttpResponse; |
| 11 | +import java.time.Duration; |
| 12 | +import java.util.stream.Stream; |
| 13 | + |
| 14 | +import io.modelcontextprotocol.server.McpServer; |
| 15 | +import io.modelcontextprotocol.spec.McpSchema; |
| 16 | +import io.modelcontextprotocol.spec.ProtocolVersions; |
| 17 | +import jakarta.servlet.http.HttpServlet; |
| 18 | +import org.apache.catalina.LifecycleException; |
| 19 | +import org.apache.catalina.LifecycleState; |
| 20 | +import org.apache.catalina.startup.Tomcat; |
| 21 | +import org.junit.jupiter.api.AfterAll; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | +import org.junit.jupiter.params.BeforeParameterizedClassInvocation; |
| 24 | +import org.junit.jupiter.params.Parameter; |
| 25 | +import org.junit.jupiter.params.ParameterizedClass; |
| 26 | +import org.junit.jupiter.params.provider.Arguments; |
| 27 | +import org.junit.jupiter.params.provider.MethodSource; |
| 28 | + |
| 29 | +import static org.assertj.core.api.Assertions.assertThat; |
| 30 | +import static org.junit.jupiter.api.Named.named; |
| 31 | +import static org.junit.jupiter.params.provider.Arguments.arguments; |
| 32 | + |
| 33 | +/** |
| 34 | + * Validates Content-Type and protocol version enforcement in HTTP servlet transports. |
| 35 | + * |
| 36 | + * @author Gorre Surya |
| 37 | + */ |
| 38 | +@ParameterizedClass |
| 39 | +@MethodSource("transports") |
| 40 | +class HttpTransportValidationTests { |
| 41 | + |
| 42 | + private static final String ACCEPT_HEADER = "application/json, text/event-stream"; |
| 43 | + |
| 44 | + private static final String INITIALIZE_BODY = """ |
| 45 | + {"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"%s","capabilities":{},"clientInfo":{"name":"test","version":"1.0"}}} |
| 46 | + """ |
| 47 | + .formatted(ProtocolVersions.MCP_2025_11_25) |
| 48 | + .strip(); |
| 49 | + |
| 50 | + @Parameter |
| 51 | + private static TransportServer transportServer; |
| 52 | + |
| 53 | + private static Tomcat tomcat; |
| 54 | + |
| 55 | + private static String baseUrl; |
| 56 | + |
| 57 | + private static HttpClient httpClient; |
| 58 | + |
| 59 | + @BeforeParameterizedClassInvocation |
| 60 | + static void setUp(TransportServer transport) { |
| 61 | + transportServer = transport; |
| 62 | + var port = TomcatTestUtil.findAvailablePort(); |
| 63 | + baseUrl = "http://localhost:" + port; |
| 64 | + tomcat = TomcatTestUtil.createTomcatServer("", port, transportServer.servlet()); |
| 65 | + try { |
| 66 | + tomcat.start(); |
| 67 | + assertThat(tomcat.getServer().getState()).isEqualTo(LifecycleState.STARTED); |
| 68 | + } |
| 69 | + catch (Exception e) { |
| 70 | + throw new RuntimeException("Failed to start Tomcat", e); |
| 71 | + } |
| 72 | + httpClient = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(5)).build(); |
| 73 | + } |
| 74 | + |
| 75 | + @AfterAll |
| 76 | + static void tearDown() { |
| 77 | + if (tomcat != null) { |
| 78 | + try { |
| 79 | + tomcat.stop(); |
| 80 | + tomcat.destroy(); |
| 81 | + } |
| 82 | + catch (LifecycleException e) { |
| 83 | + throw new RuntimeException("Failed to stop Tomcat", e); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + void postWithNonJsonContentTypeReturns415() throws Exception { |
| 90 | + var request = HttpRequest.newBuilder() |
| 91 | + .uri(URI.create(baseUrl + "/mcp")) |
| 92 | + .header("Content-Type", "text/plain") |
| 93 | + .header("Accept", ACCEPT_HEADER) |
| 94 | + .POST(HttpRequest.BodyPublishers.ofString(INITIALIZE_BODY)) |
| 95 | + .build(); |
| 96 | + |
| 97 | + var response = httpClient.send(request, HttpResponse.BodyHandlers.ofString()); |
| 98 | + |
| 99 | + assertThat(response.statusCode()).isEqualTo(415); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + void postWithMissingContentTypeReturns415() throws Exception { |
| 104 | + var request = HttpRequest.newBuilder() |
| 105 | + .uri(URI.create(baseUrl + "/mcp")) |
| 106 | + .header("Accept", ACCEPT_HEADER) |
| 107 | + .POST(HttpRequest.BodyPublishers.ofString(INITIALIZE_BODY)) |
| 108 | + .build(); |
| 109 | + |
| 110 | + var response = httpClient.send(request, HttpResponse.BodyHandlers.ofString()); |
| 111 | + |
| 112 | + assertThat(response.statusCode()).isEqualTo(415); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + void postWithJsonContentTypeIncludingCharsetSucceeds() throws Exception { |
| 117 | + var request = HttpRequest.newBuilder() |
| 118 | + .uri(URI.create(baseUrl + "/mcp")) |
| 119 | + .header("Content-Type", "application/json; charset=utf-8") |
| 120 | + .header("Accept", ACCEPT_HEADER) |
| 121 | + .POST(HttpRequest.BodyPublishers.ofString(INITIALIZE_BODY)) |
| 122 | + .build(); |
| 123 | + |
| 124 | + var response = httpClient.send(request, HttpResponse.BodyHandlers.ofString()); |
| 125 | + |
| 126 | + assertThat(response.statusCode()).isIn(200, 202); |
| 127 | + } |
| 128 | + |
| 129 | + static Stream<Arguments> transports() { |
| 130 | + return Stream.of(arguments(named("Streamable HTTP", new StreamableHttpTransportServer())), |
| 131 | + arguments(named("Stateless", new StatelessTransportServer()))); |
| 132 | + } |
| 133 | + |
| 134 | + interface TransportServer { |
| 135 | + |
| 136 | + HttpServlet servlet(); |
| 137 | + |
| 138 | + } |
| 139 | + |
| 140 | + static class StreamableHttpTransportServer implements TransportServer { |
| 141 | + |
| 142 | + private final HttpServletStreamableServerTransportProvider transport; |
| 143 | + |
| 144 | + StreamableHttpTransportServer() { |
| 145 | + transport = HttpServletStreamableServerTransportProvider.builder().build(); |
| 146 | + McpServer.sync(transport) |
| 147 | + .serverInfo("test-server", "1.0.0") |
| 148 | + .capabilities(McpSchema.ServerCapabilities.builder().tools(true).build()) |
| 149 | + .build(); |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + public HttpServlet servlet() { |
| 154 | + return transport; |
| 155 | + } |
| 156 | + |
| 157 | + } |
| 158 | + |
| 159 | + static class StatelessTransportServer implements TransportServer { |
| 160 | + |
| 161 | + private final HttpServletStatelessServerTransport transport; |
| 162 | + |
| 163 | + StatelessTransportServer() { |
| 164 | + transport = HttpServletStatelessServerTransport.builder().build(); |
| 165 | + McpServer.sync(transport) |
| 166 | + .serverInfo("test-server", "1.0.0") |
| 167 | + .capabilities(McpSchema.ServerCapabilities.builder().tools(true).build()) |
| 168 | + .build(); |
| 169 | + } |
| 170 | + |
| 171 | + @Override |
| 172 | + public HttpServlet servlet() { |
| 173 | + return transport; |
| 174 | + } |
| 175 | + |
| 176 | + } |
| 177 | + |
| 178 | +} |
0 commit comments