Skip to content

Commit 5436fc1

Browse files
committed
fix: introduce ServerHttpHeaderValidator with non-breaking deprecation path
Deprecates ServerTransportSecurityValidator (kept as-is, still @FunctionalInterface, fully binary-compatible) and introduces ServerHttpHeaderValidator, which validates via the new HeaderAccessor abstraction instead of a pre-built Map. DefaultServerTransportSecurityValidator now implements both. A static toHttpHeaderValidator(ServerTransportSecurityValidator) adapter lets old validators plug into the new API. The three servlet transports (SSE, stateless, streamable) now use ServerHttpHeaderValidator internally; their deprecated securityValidator(...) builder methods wrap existing validators through the adapter so no existing caller breaks. Addresses review feedback from #982.
1 parent c5d0be3 commit 5436fc1

9 files changed

Lines changed: 215 additions & 192 deletions

File tree

‎mcp-core/src/main/java/io/modelcontextprotocol/server/transport/DefaultServerTransportSecurityValidator.java‎

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import java.util.ArrayList;
88
import java.util.List;
9+
import java.util.Map;
910

1011
import io.modelcontextprotocol.util.Assert;
1112

@@ -21,7 +22,8 @@
2122
* @see ServerTransportSecurityValidator
2223
* @see ServerTransportSecurityException
2324
*/
24-
public final class DefaultServerTransportSecurityValidator implements ServerTransportSecurityValidator {
25+
public final class DefaultServerTransportSecurityValidator
26+
implements ServerTransportSecurityValidator, ServerHttpHeaderValidator {
2527

2628
private static final String ORIGIN_HEADER = "Origin";
2729

@@ -46,14 +48,35 @@ private DefaultServerTransportSecurityValidator(List<String> allowedOrigins, Lis
4648
}
4749

4850
@Override
49-
public void validateHeaders(HeaderAccessor accessor) throws ServerTransportSecurityException {
50-
List<String> originValues = accessor.getHeader(ORIGIN_HEADER);
51+
@Deprecated
52+
public void validateHeaders(Map<String, List<String>> headers) throws ServerTransportSecurityException {
53+
validate(new HeaderAccessor() {
54+
@Override
55+
public List<String> getHeader(String name) {
56+
return headers.entrySet()
57+
.stream()
58+
.filter(entry -> entry.getKey().equalsIgnoreCase(name))
59+
.map(Map.Entry::getValue)
60+
.findFirst()
61+
.orElse(List.of());
62+
}
63+
64+
@Override
65+
public List<String> getHeaderNames() {
66+
return List.copyOf(headers.keySet());
67+
}
68+
});
69+
}
70+
71+
@Override
72+
public void validate(HeaderAccessor headerAccessor) throws ServerTransportSecurityException {
73+
List<String> originValues = headerAccessor.getHeader(ORIGIN_HEADER);
5174
if (originValues != null && !originValues.isEmpty()) {
5275
validateOrigin(originValues.get(0));
5376
}
5477

5578
if (!allowedHosts.isEmpty()) {
56-
List<String> hostValues = accessor.getHeader(HOST_HEADER);
79+
List<String> hostValues = headerAccessor.getHeader(HOST_HEADER);
5780
if (hostValues == null || hostValues.isEmpty()) {
5881
throw new ServerTransportSecurityException(421, "Invalid Host header");
5982
}

‎mcp-core/src/main/java/io/modelcontextprotocol/server/transport/HeaderAccessor.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*
1414
* @author Neeraj Bhatt
1515
* @since 0.16.0
16-
* @see ServerTransportSecurityValidator
16+
* @see ServerHttpHeaderValidator
1717
*/
1818
public interface HeaderAccessor {
1919

‎mcp-core/src/main/java/io/modelcontextprotocol/server/transport/HttpServletSseServerTransportProvider.java‎

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public class HttpServletSseServerTransportProvider extends HttpServlet implement
161161
/**
162162
* Security validator for validating HTTP requests.
163163
*/
164-
private final ServerTransportSecurityValidator securityValidator;
164+
private final ServerHttpHeaderValidator httpHeaderValidator;
165165

166166
/**
167167
* Creates a new HttpServletSseServerTransportProvider instance with a custom SSE
@@ -174,28 +174,28 @@ public class HttpServletSseServerTransportProvider extends HttpServlet implement
174174
* @param keepAliveInterval The interval for keep-alive pings, or null to disable
175175
* keep-alive functionality
176176
* @param contextExtractor The extractor for transport context from the request.
177-
* @param securityValidator The security validator for validating HTTP requests.
177+
* @param httpHeaderValidator The HTTP header validator for validating HTTP requests.
178178
* @param requestMaxSize The maximum size, in bytes, of a single request body. Must be
179179
* positive.
180180
*/
181181
private HttpServletSseServerTransportProvider(McpJsonMapper jsonMapper, String baseUrl, String messageEndpoint,
182182
String sseEndpoint, Duration keepAliveInterval,
183183
McpTransportContextExtractor<HttpServletRequest> contextExtractor,
184-
ServerTransportSecurityValidator securityValidator, int requestMaxSize) {
184+
ServerHttpHeaderValidator httpHeaderValidator, int requestMaxSize) {
185185

186186
Assert.notNull(jsonMapper, "JsonMapper must not be null");
187187
Assert.notNull(messageEndpoint, "messageEndpoint must not be null");
188188
Assert.notNull(sseEndpoint, "sseEndpoint must not be null");
189189
Assert.notNull(contextExtractor, "Context extractor must not be null");
190-
Assert.notNull(securityValidator, "Security validator must not be null");
190+
Assert.notNull(httpHeaderValidator, "HTTP header validator must not be null");
191191
Assert.isTrue(requestMaxSize > 0, "requestMaxSize must be positive");
192192

193193
this.jsonMapper = jsonMapper;
194194
this.baseUrl = baseUrl;
195195
this.messageEndpoint = messageEndpoint;
196196
this.sseEndpoint = sseEndpoint;
197197
this.contextExtractor = contextExtractor;
198-
this.securityValidator = securityValidator;
198+
this.httpHeaderValidator = httpHeaderValidator;
199199
this.requestMaxSize = requestMaxSize;
200200

201201
if (keepAliveInterval != null) {
@@ -293,7 +293,7 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response)
293293
}
294294

295295
try {
296-
this.securityValidator.validateHeaders(new HttpServletHeaderAccessor(request));
296+
this.httpHeaderValidator.validate(new HttpServletHeaderAccessor(request));
297297
}
298298
catch (ServerTransportSecurityException e) {
299299
response.sendError(e.getStatusCode(), e.getMessage());
@@ -370,7 +370,7 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
370370
}
371371

372372
try {
373-
this.securityValidator.validateHeaders(new HttpServletHeaderAccessor(request));
373+
this.httpHeaderValidator.validate(new HttpServletHeaderAccessor(request));
374374
}
375375
catch (ServerTransportSecurityException e) {
376376
response.sendError(e.getStatusCode(), e.getMessage());
@@ -617,7 +617,7 @@ public static class Builder {
617617

618618
private Duration keepAliveInterval;
619619

620-
private ServerTransportSecurityValidator securityValidator = ServerTransportSecurityValidator.NOOP;
620+
private ServerHttpHeaderValidator httpHeaderValidator = ServerHttpHeaderValidator.NOOP;
621621

622622
private int requestMaxSize = DEFAULT_REQUEST_MAX_SIZE;
623623

@@ -700,10 +700,25 @@ public Builder keepAliveInterval(Duration keepAliveInterval) {
700700
* @param securityValidator The security validator to use. Must not be null.
701701
* @return This builder instance
702702
* @throws IllegalArgumentException if securityValidator is null
703+
* @deprecated Use {@link #httpHeaderValidator(ServerHttpHeaderValidator)}
704+
* instead.
703705
*/
706+
@Deprecated
704707
public Builder securityValidator(ServerTransportSecurityValidator securityValidator) {
705708
Assert.notNull(securityValidator, "Security validator must not be null");
706-
this.securityValidator = securityValidator;
709+
this.httpHeaderValidator = ServerTransportSecurityValidator.toHttpHeaderValidator(securityValidator);
710+
return this;
711+
}
712+
713+
/**
714+
* Sets the HTTP header validator for validating HTTP requests.
715+
* @param httpHeaderValidator The HTTP header validator to use. Must not be null.
716+
* @return This builder instance
717+
* @throws IllegalArgumentException if httpHeaderValidator is null
718+
*/
719+
public Builder httpHeaderValidator(ServerHttpHeaderValidator httpHeaderValidator) {
720+
Assert.notNull(httpHeaderValidator, "HTTP header validator must not be null");
721+
this.httpHeaderValidator = httpHeaderValidator;
707722
return this;
708723
}
709724

@@ -732,7 +747,7 @@ public HttpServletSseServerTransportProvider build() {
732747
}
733748
return new HttpServletSseServerTransportProvider(
734749
jsonMapper == null ? McpJsonDefaults.getMapper() : jsonMapper, baseUrl, messageEndpoint,
735-
sseEndpoint, keepAliveInterval, contextExtractor, securityValidator, requestMaxSize);
750+
sseEndpoint, keepAliveInterval, contextExtractor, httpHeaderValidator, requestMaxSize);
736751
}
737752

738753
}

‎mcp-core/src/main/java/io/modelcontextprotocol/server/transport/HttpServletStatelessServerTransport.java‎

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public class HttpServletStatelessServerTransport extends HttpServlet implements
6767
/**
6868
* Security validator for validating HTTP requests.
6969
*/
70-
private final ServerTransportSecurityValidator securityValidator;
70+
private final ServerHttpHeaderValidator httpHeaderValidator;
7171

7272
/**
7373
* Maximum size, in bytes, of a single request body accepted by this transport.
@@ -81,24 +81,24 @@ public class HttpServletStatelessServerTransport extends HttpServlet implements
8181
* @param mcpEndpoint The endpoint URI where clients should send their JSON-RPC
8282
* messages.
8383
* @param contextExtractor The extractor for transport context from the request.
84-
* @param securityValidator The security validator for validating HTTP requests.
84+
* @param httpHeaderValidator The HTTP header validator for validating HTTP requests.
8585
* @param requestMaxSize The maximum size, in bytes, of a single request body. Must be
8686
* positive.
8787
* @throws IllegalArgumentException if any parameter is null
8888
*/
8989
private HttpServletStatelessServerTransport(McpJsonMapper jsonMapper, String mcpEndpoint,
9090
McpTransportContextExtractor<HttpServletRequest> contextExtractor,
91-
ServerTransportSecurityValidator securityValidator, int requestMaxSize) {
91+
ServerHttpHeaderValidator httpHeaderValidator, int requestMaxSize) {
9292
Assert.notNull(jsonMapper, "jsonMapper must not be null");
9393
Assert.notNull(mcpEndpoint, "mcpEndpoint must not be null");
9494
Assert.notNull(contextExtractor, "contextExtractor must not be null");
95-
Assert.notNull(securityValidator, "Security validator must not be null");
95+
Assert.notNull(httpHeaderValidator, "HTTP header validator must not be null");
9696
Assert.isTrue(requestMaxSize > 0, "requestMaxSize must be positive");
9797

9898
this.jsonMapper = jsonMapper;
9999
this.mcpEndpoint = mcpEndpoint;
100100
this.contextExtractor = contextExtractor;
101-
this.securityValidator = securityValidator;
101+
this.httpHeaderValidator = httpHeaderValidator;
102102
this.requestMaxSize = requestMaxSize;
103103
}
104104

@@ -161,7 +161,7 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
161161
}
162162

163163
try {
164-
this.securityValidator.validateHeaders(new HttpServletHeaderAccessor(request));
164+
this.httpHeaderValidator.validate(new HttpServletHeaderAccessor(request));
165165
}
166166
catch (ServerTransportSecurityException e) {
167167
response.sendError(e.getStatusCode(), e.getMessage());
@@ -298,7 +298,7 @@ public static class Builder {
298298
private McpTransportContextExtractor<HttpServletRequest> contextExtractor = (
299299
serverRequest) -> McpTransportContext.EMPTY;
300300

301-
private ServerTransportSecurityValidator securityValidator = ServerTransportSecurityValidator.NOOP;
301+
private ServerHttpHeaderValidator httpHeaderValidator = ServerHttpHeaderValidator.NOOP;
302302

303303
private int requestMaxSize = DEFAULT_REQUEST_MAX_SIZE;
304304

@@ -352,10 +352,25 @@ public Builder contextExtractor(McpTransportContextExtractor<HttpServletRequest>
352352
* @param securityValidator The security validator to use. Must not be null.
353353
* @return this builder instance
354354
* @throws IllegalArgumentException if securityValidator is null
355+
* @deprecated Use {@link #httpHeaderValidator(ServerHttpHeaderValidator)}
356+
* instead.
355357
*/
358+
@Deprecated
356359
public Builder securityValidator(ServerTransportSecurityValidator securityValidator) {
357360
Assert.notNull(securityValidator, "Security validator must not be null");
358-
this.securityValidator = securityValidator;
361+
this.httpHeaderValidator = ServerTransportSecurityValidator.toHttpHeaderValidator(securityValidator);
362+
return this;
363+
}
364+
365+
/**
366+
* Sets the HTTP header validator for validating HTTP requests.
367+
* @param httpHeaderValidator The HTTP header validator to use. Must not be null.
368+
* @return this builder instance
369+
* @throws IllegalArgumentException if httpHeaderValidator is null
370+
*/
371+
public Builder httpHeaderValidator(ServerHttpHeaderValidator httpHeaderValidator) {
372+
Assert.notNull(httpHeaderValidator, "HTTP header validator must not be null");
373+
this.httpHeaderValidator = httpHeaderValidator;
359374
return this;
360375
}
361376

@@ -382,7 +397,7 @@ public HttpServletStatelessServerTransport build() {
382397
Assert.notNull(mcpEndpoint, "Message endpoint must be set");
383398
return new HttpServletStatelessServerTransport(
384399
jsonMapper == null ? McpJsonDefaults.getMapper() : jsonMapper, mcpEndpoint, contextExtractor,
385-
securityValidator, requestMaxSize);
400+
httpHeaderValidator, requestMaxSize);
386401
}
387402

388403
}

‎mcp-core/src/main/java/io/modelcontextprotocol/server/transport/HttpServletStreamableServerTransportProvider.java‎

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public class HttpServletStreamableServerTransportProvider extends HttpServlet
129129
/**
130130
* Security validator for validating HTTP requests.
131131
*/
132-
private final ServerTransportSecurityValidator securityValidator;
132+
private final ServerHttpHeaderValidator httpHeaderValidator;
133133

134134
/**
135135
* Constructs a new HttpServletStreamableServerTransportProvider instance.
@@ -141,25 +141,25 @@ public class HttpServletStreamableServerTransportProvider extends HttpServlet
141141
* @param contextExtractor The extractor for transport context from the request.
142142
* @param keepAliveInterval The interval for keep-alive pings. If null, no keep-alive
143143
* will be scheduled.
144-
* @param securityValidator The security validator for validating HTTP requests.
144+
* @param httpHeaderValidator The HTTP header validator for validating HTTP requests.
145145
* @param requestMaxSize The maximum size, in bytes, of a single request body. Must be
146146
* positive.
147147
* @throws IllegalArgumentException if any parameter is null
148148
*/
149149
private HttpServletStreamableServerTransportProvider(McpJsonMapper jsonMapper, String mcpEndpoint,
150150
boolean disallowDelete, McpTransportContextExtractor<HttpServletRequest> contextExtractor,
151-
Duration keepAliveInterval, ServerTransportSecurityValidator securityValidator, int requestMaxSize) {
151+
Duration keepAliveInterval, ServerHttpHeaderValidator httpHeaderValidator, int requestMaxSize) {
152152
Assert.notNull(jsonMapper, "JsonMapper must not be null");
153153
Assert.notNull(mcpEndpoint, "MCP endpoint must not be null");
154154
Assert.notNull(contextExtractor, "Context extractor must not be null");
155-
Assert.notNull(securityValidator, "Security validator must not be null");
155+
Assert.notNull(httpHeaderValidator, "HTTP header validator must not be null");
156156
Assert.isTrue(requestMaxSize > 0, "requestMaxSize must be positive");
157157

158158
this.jsonMapper = jsonMapper;
159159
this.mcpEndpoint = mcpEndpoint;
160160
this.disallowDelete = disallowDelete;
161161
this.contextExtractor = contextExtractor;
162-
this.securityValidator = securityValidator;
162+
this.httpHeaderValidator = httpHeaderValidator;
163163
this.requestMaxSize = requestMaxSize;
164164

165165
if (keepAliveInterval != null) {
@@ -273,7 +273,7 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response)
273273
}
274274

275275
try {
276-
this.securityValidator.validateHeaders(new HttpServletHeaderAccessor(request));
276+
this.httpHeaderValidator.validate(new HttpServletHeaderAccessor(request));
277277
}
278278
catch (ServerTransportSecurityException e) {
279279
response.sendError(e.getStatusCode(), e.getMessage());
@@ -412,7 +412,7 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
412412
}
413413

414414
try {
415-
this.securityValidator.validateHeaders(new HttpServletHeaderAccessor(request));
415+
this.httpHeaderValidator.validate(new HttpServletHeaderAccessor(request));
416416
}
417417
catch (ServerTransportSecurityException e) {
418418
response.sendError(e.getStatusCode(), e.getMessage());
@@ -590,7 +590,7 @@ protected void doDelete(HttpServletRequest request, HttpServletResponse response
590590
}
591591

592592
try {
593-
this.securityValidator.validateHeaders(new HttpServletHeaderAccessor(request));
593+
this.httpHeaderValidator.validate(new HttpServletHeaderAccessor(request));
594594
}
595595
catch (ServerTransportSecurityException e) {
596596
response.sendError(e.getStatusCode(), e.getMessage());
@@ -837,7 +837,7 @@ public static class Builder {
837837

838838
private Duration keepAliveInterval;
839839

840-
private ServerTransportSecurityValidator securityValidator = ServerTransportSecurityValidator.NOOP;
840+
private ServerHttpHeaderValidator httpHeaderValidator = ServerHttpHeaderValidator.NOOP;
841841

842842
private int requestMaxSize = DEFAULT_REQUEST_MAX_SIZE;
843843

@@ -905,10 +905,25 @@ public Builder keepAliveInterval(Duration keepAliveInterval) {
905905
* @param securityValidator The security validator to use. Must not be null.
906906
* @return this builder instance
907907
* @throws IllegalArgumentException if securityValidator is null
908+
* @deprecated Use {@link #httpHeaderValidator(ServerHttpHeaderValidator)}
909+
* instead.
908910
*/
911+
@Deprecated
909912
public Builder securityValidator(ServerTransportSecurityValidator securityValidator) {
910913
Assert.notNull(securityValidator, "Security validator must not be null");
911-
this.securityValidator = securityValidator;
914+
this.httpHeaderValidator = ServerTransportSecurityValidator.toHttpHeaderValidator(securityValidator);
915+
return this;
916+
}
917+
918+
/**
919+
* Sets the HTTP header validator for validating HTTP requests.
920+
* @param httpHeaderValidator The HTTP header validator to use. Must not be null.
921+
* @return this builder instance
922+
* @throws IllegalArgumentException if httpHeaderValidator is null
923+
*/
924+
public Builder httpHeaderValidator(ServerHttpHeaderValidator httpHeaderValidator) {
925+
Assert.notNull(httpHeaderValidator, "HTTP header validator must not be null");
926+
this.httpHeaderValidator = httpHeaderValidator;
912927
return this;
913928
}
914929

@@ -935,7 +950,7 @@ public HttpServletStreamableServerTransportProvider build() {
935950
Assert.notNull(this.mcpEndpoint, "MCP endpoint must be set");
936951
return new HttpServletStreamableServerTransportProvider(
937952
jsonMapper == null ? McpJsonDefaults.getMapper() : jsonMapper, mcpEndpoint, disallowDelete,
938-
contextExtractor, keepAliveInterval, securityValidator, requestMaxSize);
953+
contextExtractor, keepAliveInterval, httpHeaderValidator, requestMaxSize);
939954
}
940955

941956
}

0 commit comments

Comments
 (0)