@@ -126,6 +126,10 @@ public class HttpServletStreamableServerTransportProvider extends HttpServlet
126126 */
127127 private final ServerTransportSecurityValidator securityValidator ;
128128
129+ private final boolean requireMcpNameHeader ;
130+
131+ private final boolean requireMcpMethodHeader ;
132+
129133 /**
130134 * Constructs a new HttpServletStreamableServerTransportProvider instance.
131135 * @param jsonMapper The JsonMapper to use for JSON serialization/deserialization of
@@ -141,7 +145,8 @@ public class HttpServletStreamableServerTransportProvider extends HttpServlet
141145 */
142146 private HttpServletStreamableServerTransportProvider (McpJsonMapper jsonMapper , String mcpEndpoint ,
143147 boolean disallowDelete , McpTransportContextExtractor <HttpServletRequest > contextExtractor ,
144- Duration keepAliveInterval , ServerTransportSecurityValidator securityValidator ) {
148+ Duration keepAliveInterval , ServerTransportSecurityValidator securityValidator ,
149+ boolean requireMcpNameHeader , boolean requireMcpMethodHeader ) {
145150 Assert .notNull (jsonMapper , "JsonMapper must not be null" );
146151 Assert .notNull (mcpEndpoint , "MCP endpoint must not be null" );
147152 Assert .notNull (contextExtractor , "Context extractor must not be null" );
@@ -152,6 +157,8 @@ private HttpServletStreamableServerTransportProvider(McpJsonMapper jsonMapper, S
152157 this .disallowDelete = disallowDelete ;
153158 this .contextExtractor = contextExtractor ;
154159 this .securityValidator = securityValidator ;
160+ this .requireMcpNameHeader = requireMcpNameHeader ;
161+ this .requireMcpMethodHeader = requireMcpMethodHeader ;
155162
156163 if (keepAliveInterval != null ) {
157164
@@ -390,6 +397,101 @@ public void onStartAsync(jakarta.servlet.AsyncEvent event) throws IOException {
390397 * @throws ServletException If a servlet-specific error occurs
391398 * @throws IOException If an I/O error occurs
392399 */
400+ private String extractNameFromParams (String method , Object params ) {
401+ if (params == null ) {
402+ return null ;
403+ }
404+
405+ try {
406+ switch (method ) {
407+ case McpSchema .METHOD_TOOLS_CALL -> {
408+ McpSchema .CallToolRequest request = jsonMapper .convertValue (params ,
409+ new TypeRef <McpSchema .CallToolRequest >() {
410+ });
411+ return request .name ();
412+ }
413+ case McpSchema .METHOD_RESOURCES_READ -> {
414+ McpSchema .ReadResourceRequest request = jsonMapper .convertValue (params ,
415+ new TypeRef <McpSchema .ReadResourceRequest >() {
416+ });
417+ return request .uri ();
418+ }
419+ case McpSchema .METHOD_PROMPT_GET -> {
420+ McpSchema .GetPromptRequest request = jsonMapper .convertValue (params ,
421+ new TypeRef <McpSchema .GetPromptRequest >() {
422+ });
423+ return request .name ();
424+ }
425+ default -> {
426+ return null ;
427+ }
428+ }
429+ }
430+ catch (Exception e ) {
431+ logger .debug ("Failed to extract name from params for method {}: {}" , method , e .getMessage ());
432+ return null ;
433+ }
434+ }
435+
436+ private boolean validateMcpMethodHeader (HttpServletRequest request , HttpServletResponse response , String method )
437+ throws IOException {
438+ if (!this .requireMcpMethodHeader ) {
439+ return true ;
440+ }
441+
442+ String headerMethod = request .getHeader (HttpHeaders .MCP_METHOD );
443+ if (headerMethod == null || headerMethod .isBlank ()) {
444+ this .responseError (response , HttpServletResponse .SC_BAD_REQUEST ,
445+ McpError .builder (McpSchema .ErrorCodes .INVALID_REQUEST )
446+ .message ("Mcp-Method header required for method " + method )
447+ .build ());
448+ return false ;
449+ }
450+
451+ if (!method .equals (headerMethod )) {
452+ this .responseError (response , HttpServletResponse .SC_BAD_REQUEST ,
453+ McpError .builder (McpSchema .ErrorCodes .INVALID_REQUEST )
454+ .message ("Mcp-Method header mismatch: expected '" + method + "' but was '" + headerMethod + "'" )
455+ .build ());
456+ return false ;
457+ }
458+
459+ return true ;
460+ }
461+
462+ private boolean validateMcpNameHeader (HttpServletRequest request , HttpServletResponse response , String method ,
463+ Object params ) throws IOException {
464+ if (!this .requireMcpNameHeader ) {
465+ return true ;
466+ }
467+
468+ String expectedName = extractNameFromParams (method , params );
469+ String headerName = request .getHeader (HttpHeaders .MCP_NAME );
470+ if (headerName == null || headerName .isBlank ()) {
471+ this .responseError (response , HttpServletResponse .SC_BAD_REQUEST ,
472+ McpError .builder (McpSchema .ErrorCodes .INVALID_REQUEST )
473+ .message ("Mcp-Name header required for method " + method )
474+ .build ());
475+ return false ;
476+ }
477+
478+ if (expectedName == null || !headerName .equals (expectedName )) {
479+ this .responseError (response , HttpServletResponse .SC_BAD_REQUEST ,
480+ McpError .builder (McpSchema .ErrorCodes .INVALID_REQUEST )
481+ .message ("Mcp-Name header mismatch: expected '" + expectedName + "' but was '" + headerName
482+ + "'" )
483+ .build ());
484+ return false ;
485+ }
486+
487+ return true ;
488+ }
489+
490+ private boolean isNameHeaderMethod (String method ) {
491+ return McpSchema .METHOD_TOOLS_CALL .equals (method ) || McpSchema .METHOD_RESOURCES_READ .equals (method )
492+ || McpSchema .METHOD_PROMPT_GET .equals (method );
493+ }
494+
393495 @ Override
394496 protected void doPost (HttpServletRequest request , HttpServletResponse response )
395497 throws ServletException , IOException {
@@ -436,6 +538,23 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
436538
437539 McpSchema .JSONRPCMessage message = McpSchema .deserializeJsonRpcMessage (jsonMapper , body .toString ());
438540
541+ if (message instanceof McpSchema .JSONRPCRequest jsonrpcRequest ) {
542+ String method = jsonrpcRequest .method ();
543+ if (!validateMcpMethodHeader (request , response , method )) {
544+ return ;
545+ }
546+ if (isNameHeaderMethod (method )
547+ && !validateMcpNameHeader (request , response , method , jsonrpcRequest .params ())) {
548+ return ;
549+ }
550+ }
551+ else if (message instanceof McpSchema .JSONRPCNotification jsonrpcNotification ) {
552+ String method = jsonrpcNotification .method ();
553+ if (!validateMcpMethodHeader (request , response , method )) {
554+ return ;
555+ }
556+ }
557+
439558 // Handle initialization request
440559 if (message instanceof McpSchema .JSONRPCRequest jsonrpcRequest
441560 && jsonrpcRequest .method ().equals (McpSchema .METHOD_INITIALIZE )) {
@@ -830,6 +949,10 @@ public static class Builder {
830949
831950 private boolean disallowDelete = false ;
832951
952+ private boolean requireMcpNameHeader = false ;
953+
954+ private boolean requireMcpMethodHeader = false ;
955+
833956 private McpTransportContextExtractor <HttpServletRequest > contextExtractor = (
834957 serverRequest ) -> McpTransportContext .EMPTY ;
835958
@@ -908,6 +1031,30 @@ public Builder securityValidator(ServerTransportSecurityValidator securityValida
9081031 return this ;
9091032 }
9101033
1034+ /**
1035+ * Sets whether the server should reject requests that do not include the
1036+ * {@code Mcp-Name} header for methods that may include it.
1037+ * @param requireMcpNameHeader true to reject missing headers, false to accept
1038+ * requests from older clients by default
1039+ * @return this builder instance
1040+ */
1041+ public Builder requireMcpNameHeader (boolean requireMcpNameHeader ) {
1042+ this .requireMcpNameHeader = requireMcpNameHeader ;
1043+ return this ;
1044+ }
1045+
1046+ /**
1047+ * Sets whether the server should reject requests that do not include the
1048+ * {@code Mcp-Method} header for requests and notifications.
1049+ * @param requireMcpMethodHeader true to reject missing or mismatched method
1050+ * headers, false to accept requests from older clients by default
1051+ * @return this builder instance
1052+ */
1053+ public Builder requireMcpMethodHeader (boolean requireMcpMethodHeader ) {
1054+ this .requireMcpMethodHeader = requireMcpMethodHeader ;
1055+ return this ;
1056+ }
1057+
9111058 /**
9121059 * Builds a new instance of {@link HttpServletStreamableServerTransportProvider}
9131060 * with the configured settings.
@@ -918,7 +1065,8 @@ public HttpServletStreamableServerTransportProvider build() {
9181065 Assert .notNull (this .mcpEndpoint , "MCP endpoint must be set" );
9191066 return new HttpServletStreamableServerTransportProvider (
9201067 jsonMapper == null ? McpJsonDefaults .getMapper () : jsonMapper , mcpEndpoint , disallowDelete ,
921- contextExtractor , keepAliveInterval , securityValidator );
1068+ contextExtractor , keepAliveInterval , securityValidator , requireMcpNameHeader ,
1069+ requireMcpMethodHeader );
9221070 }
9231071
9241072 }
0 commit comments