@@ -483,14 +483,34 @@ public Mono<Void> sendMessage(McpSchema.JSONRPCMessage sentMessage) {
483483 transportSession .sessionId ().get ());
484484 }
485485
486+ // Extract method and params for Mcp-Name header
487+ String method = null ;
488+ Object params = null ;
489+ if (sentMessage instanceof McpSchema .JSONRPCRequest request ) {
490+ method = request .method ();
491+ params = request .params ();
492+ }
493+ else if (sentMessage instanceof McpSchema .JSONRPCNotification notification ) {
494+ method = notification .method ();
495+ params = notification .params ();
496+ }
497+
486498 var builder = requestBuilder .uri (uri )
487499 .header (HttpHeaders .ACCEPT , APPLICATION_JSON + ", " + TEXT_EVENT_STREAM )
488500 .header (HttpHeaders .CONTENT_TYPE , APPLICATION_JSON_UTF8 )
489501 .header (HttpHeaders .CACHE_CONTROL , "no-cache" )
490- .header (HttpHeaders .PROTOCOL_VERSION ,
491- ctx .getOrDefault (McpAsyncClient .NEGOTIATED_PROTOCOL_VERSION ,
492- this .latestSupportedProtocolVersion ))
493- .POST (HttpRequest .BodyPublishers .ofString (jsonBody ));
502+ .header (HttpHeaders .PROTOCOL_VERSION , ctx .getOrDefault (McpAsyncClient .NEGOTIATED_PROTOCOL_VERSION ,
503+ this .latestSupportedProtocolVersion ));
504+
505+ // Add Mcp-Name header if applicable
506+ if (method != null ) {
507+ String name = extractNameFromParams (method , params );
508+ if (name != null ) {
509+ builder = builder .header (HttpHeaders .MCP_NAME , name );
510+ }
511+ }
512+
513+ builder = builder .POST (HttpRequest .BodyPublishers .ofString (jsonBody ));
494514 var transportContext = ctx .getOrDefault (McpTransportContext .KEY , McpTransportContext .EMPTY );
495515 return Mono
496516 .from (this .httpRequestCustomizer .customize (builder , "POST" , uri , jsonBody , transportContext ));
@@ -675,6 +695,48 @@ public <T> T unmarshalFrom(Object data, TypeRef<T> typeRef) {
675695 return this .jsonMapper .convertValue (data , typeRef );
676696 }
677697
698+ /**
699+ * Extracts the name/URI from the request params based on the method type.
700+ * @param method the MCP method name
701+ * @param params the request parameters
702+ * @return the name/URI if applicable for the method, or null otherwise
703+ */
704+ private String extractNameFromParams (String method , Object params ) {
705+ if (params == null ) {
706+ return null ;
707+ }
708+
709+ try {
710+ switch (method ) {
711+ case McpSchema .METHOD_TOOLS_CALL -> {
712+ McpSchema .CallToolRequest request = this .jsonMapper .convertValue (params ,
713+ new TypeRef <McpSchema .CallToolRequest >() {
714+ });
715+ return request .name ();
716+ }
717+ case McpSchema .METHOD_RESOURCES_READ -> {
718+ McpSchema .ReadResourceRequest request = this .jsonMapper .convertValue (params ,
719+ new TypeRef <McpSchema .ReadResourceRequest >() {
720+ });
721+ return request .uri ();
722+ }
723+ case McpSchema .METHOD_PROMPT_GET -> {
724+ McpSchema .GetPromptRequest request = this .jsonMapper .convertValue (params ,
725+ new TypeRef <McpSchema .GetPromptRequest >() {
726+ });
727+ return request .name ();
728+ }
729+ default -> {
730+ return null ;
731+ }
732+ }
733+ }
734+ catch (Exception e ) {
735+ logger .debug ("Failed to extract name from params for method {}: {}" , method , e .getMessage ());
736+ return null ;
737+ }
738+ }
739+
678740 /**
679741 * Builder for {@link HttpClientStreamableHttpTransport}.
680742 */
0 commit comments