|
4 | 4 |
|
5 | 5 | package io.modelcontextprotocol.common; |
6 | 6 |
|
| 7 | +import java.net.URI; |
| 8 | +import java.net.http.HttpClient; |
| 9 | +import java.net.http.HttpRequest; |
| 10 | +import java.net.http.HttpResponse; |
7 | 11 | import java.util.List; |
8 | 12 | import java.util.Map; |
9 | 13 | import java.util.function.BiFunction; |
|
24 | 28 | import org.apache.catalina.startup.Tomcat; |
25 | 29 | import org.junit.jupiter.api.AfterEach; |
26 | 30 | import org.junit.jupiter.api.Test; |
| 31 | +import org.junit.jupiter.params.ParameterizedTest; |
| 32 | +import org.junit.jupiter.params.provider.ValueSource; |
27 | 33 |
|
28 | 34 | import static org.assertj.core.api.Assertions.assertThat; |
29 | 35 |
|
@@ -122,6 +128,55 @@ void usesServerSupportedVersion() { |
122 | 128 | mcpServer.close(); |
123 | 129 | } |
124 | 130 |
|
| 131 | + @ParameterizedTest |
| 132 | + @ValueSource(strings = { "1900-01-01", "not-a-version" }) |
| 133 | + void rejectsUnsupportedProtocolVersionHeaders(String protocolVersion) throws Exception { |
| 134 | + startTomcat(); |
| 135 | + |
| 136 | + HttpClient httpClient = HttpClient.newHttpClient(); |
| 137 | + String endpoint = "http://localhost:" + PORT + "/mcp"; |
| 138 | + |
| 139 | + HttpResponse<String> initializeResponse = httpClient.send(HttpRequest.newBuilder() |
| 140 | + .uri(URI.create(endpoint)) |
| 141 | + .header("Content-Type", "application/json") |
| 142 | + .header("Accept", "application/json, text/event-stream") |
| 143 | + .header("MCP-Protocol-Version", ProtocolVersions.MCP_2025_11_25) |
| 144 | + .POST(HttpRequest.BodyPublishers.ofString( |
| 145 | + """ |
| 146 | + {"jsonrpc":"2.0","id":"init-1","method":"initialize","params":{"protocolVersion":"2025-11-25","capabilities":{},"clientInfo":{"name":"test-client","version":"1.0.0"}}} |
| 147 | + """)) |
| 148 | + .build(), HttpResponse.BodyHandlers.ofString()); |
| 149 | + |
| 150 | + assertThat(initializeResponse.statusCode()).isEqualTo(200); |
| 151 | + String sessionId = initializeResponse.headers().firstValue("Mcp-Session-Id").orElseThrow(); |
| 152 | + |
| 153 | + HttpResponse<String> initializedResponse = httpClient.send(HttpRequest.newBuilder() |
| 154 | + .uri(URI.create(endpoint)) |
| 155 | + .header("Content-Type", "application/json") |
| 156 | + .header("Accept", "application/json, text/event-stream") |
| 157 | + .header("MCP-Protocol-Version", ProtocolVersions.MCP_2025_11_25) |
| 158 | + .header("Mcp-Session-Id", sessionId) |
| 159 | + .POST(HttpRequest.BodyPublishers |
| 160 | + .ofString("{\"jsonrpc\":\"2.0\",\"method\":\"notifications/initialized\",\"params\":{}}")) |
| 161 | + .build(), HttpResponse.BodyHandlers.ofString()); |
| 162 | + |
| 163 | + assertThat(initializedResponse.statusCode()).isEqualTo(202); |
| 164 | + |
| 165 | + HttpResponse<String> badVersionResponse = httpClient.send(HttpRequest.newBuilder() |
| 166 | + .uri(URI.create(endpoint)) |
| 167 | + .header("Content-Type", "application/json") |
| 168 | + .header("Accept", "application/json, text/event-stream") |
| 169 | + .header("MCP-Protocol-Version", protocolVersion) |
| 170 | + .header("Mcp-Session-Id", sessionId) |
| 171 | + .POST(HttpRequest.BodyPublishers |
| 172 | + .ofString("{\"jsonrpc\":\"2.0\",\"id\":\"bad-version-1\",\"method\":\"tools/list\",\"params\":{}}")) |
| 173 | + .build(), HttpResponse.BodyHandlers.ofString()); |
| 174 | + |
| 175 | + assertThat(badVersionResponse.statusCode()).isEqualTo(400); |
| 176 | + assertThat(badVersionResponse.body()).contains("Unsupported or invalid MCP protocol version"); |
| 177 | + mcpServer.close(); |
| 178 | + } |
| 179 | + |
125 | 180 | private void startTomcat() { |
126 | 181 | tomcat = TomcatTestUtil.createTomcatServer("", PORT, transport, requestRecordingFilter); |
127 | 182 | try { |
|
0 commit comments