This is an automated email from the ASF dual-hosted git repository.

liuhongyu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shenyu.git


The following commit(s) were added to refs/heads/master by this push:
     new 13f175be50 Fix mcp server real url error (#6228)
13f175be50 is described below

commit 13f175be50f8d25825b380b9d3e85f65065125f3
Author: Yu Siheng <[email protected]>
AuthorDate: Thu Nov 20 10:58:11 2025 +0800

    Fix mcp server real url error (#6228)
    
    * fix:shenyu-examples-mcp
    
    * fix some bug
    
    * fix: fix the mcp client and mcp examples bug
    
    * fix: mcp server plugin realUrl error
    
    * fix
    
    * add null judgment
    
    * fix: shenyu mcp client url concat error
    
    * fix
    
    * fix
    
    ---------
    
    Co-authored-by: aias00 <[email protected]>
---
 .../apache/shenyu/client/mcp/McpServiceEventListener.java | 15 +++++++++++++--
 .../src/main/resources/application.yml                    |  2 ++
 .../plugin/mcp/server/callback/ShenyuToolCallback.java    | 12 +++++++++---
 .../starter/client/mcp/ShenyuMcpClientConfiguration.java  |  7 +++++--
 4 files changed, 29 insertions(+), 7 deletions(-)

diff --git 
a/shenyu-client/shenyu-client-mcp/shenyu-client-mcp-register/src/main/java/org/apache/shenyu/client/mcp/McpServiceEventListener.java
 
b/shenyu-client/shenyu-client-mcp/shenyu-client-mcp-register/src/main/java/org/apache/shenyu/client/mcp/McpServiceEventListener.java
index 61cb7e2700..4b333b3a69 100644
--- 
a/shenyu-client/shenyu-client-mcp/shenyu-client-mcp-register/src/main/java/org/apache/shenyu/client/mcp/McpServiceEventListener.java
+++ 
b/shenyu-client/shenyu-client-mcp/shenyu-client-mcp-register/src/main/java/org/apache/shenyu/client/mcp/McpServiceEventListener.java
@@ -44,6 +44,7 @@ import org.slf4j.LoggerFactory;
 import org.springframework.aop.support.AopUtils;
 import org.springframework.context.ApplicationContext;
 import org.springframework.core.annotation.AnnotatedElementUtils;
+import org.springframework.core.env.Environment;
 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.RequestMapping;
 
@@ -65,14 +66,20 @@ public class McpServiceEventListener extends 
AbstractContextRefreshedEventListen
 
     private static final Logger log = 
LoggerFactory.getLogger(McpServiceEventListener.class);
 
+    private final Environment env;
+
     /**
      * Instantiates a new context refreshed event listener.
      *
      * @param clientConfig                   the shenyu client config
      * @param shenyuClientRegisterRepository the shenyuClientRegisterRepository
+     * @param env                            the spring environment
      */
-    public McpServiceEventListener(final ShenyuClientConfig clientConfig, 
final ShenyuClientRegisterRepository shenyuClientRegisterRepository) {
+    public McpServiceEventListener(final ShenyuClientConfig clientConfig,
+                                   final ShenyuClientRegisterRepository 
shenyuClientRegisterRepository,
+                                   final Environment env) {
         super(clientConfig, shenyuClientRegisterRepository);
+        this.env = env;
     }
 
     @Override
@@ -267,10 +274,14 @@ public class McpServiceEventListener extends 
AbstractContextRefreshedEventListen
         }
 
         List<String> combinedPaths = new ArrayList<>();
+        final String servletPath = 
StringUtils.defaultString(this.env.getProperty("spring.mvc.servlet.path"), "");
+        final String servletContextPath = 
StringUtils.defaultString(this.env.getProperty("server.servlet.context-path"), 
"");
+        final String rootPath = concatPaths(servletContextPath, servletPath);
         for (String cp : classPaths) {
             for (String mp : methodPaths) {
                 String path = concatPaths(cp, mp);
-                String finalPath = concatPaths(getContextPath(), path);
+                String prefix = concatPaths(getContextPath(), rootPath);
+                String finalPath = concatPaths(prefix, path);
                 combinedPaths.add(finalPath);
             }
         }
diff --git 
a/shenyu-examples/shenyu-examples-mcp/src/main/resources/application.yml 
b/shenyu-examples/shenyu-examples-mcp/src/main/resources/application.yml
index 333b9e099e..accdbbb2ef 100644
--- a/shenyu-examples/shenyu-examples-mcp/src/main/resources/application.yml
+++ b/shenyu-examples/shenyu-examples-mcp/src/main/resources/application.yml
@@ -38,3 +38,5 @@ shenyu:
       props:
         contextPath: /mcp
         appName: mcp
+        host: 127.0.0.1
+        port: 8150
diff --git 
a/shenyu-plugin/shenyu-plugin-mcp-server/src/main/java/org/apache/shenyu/plugin/mcp/server/callback/ShenyuToolCallback.java
 
b/shenyu-plugin/shenyu-plugin-mcp-server/src/main/java/org/apache/shenyu/plugin/mcp/server/callback/ShenyuToolCallback.java
index 7522e0d45f..10839c9f39 100644
--- 
a/shenyu-plugin/shenyu-plugin-mcp-server/src/main/java/org/apache/shenyu/plugin/mcp/server/callback/ShenyuToolCallback.java
+++ 
b/shenyu-plugin/shenyu-plugin-mcp-server/src/main/java/org/apache/shenyu/plugin/mcp/server/callback/ShenyuToolCallback.java
@@ -51,6 +51,7 @@ import org.springframework.web.server.ServerWebExchange;
 
 import java.net.URI;
 import java.net.URISyntaxException;
+import java.util.Map;
 import java.util.Objects;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.TimeUnit;
@@ -583,9 +584,14 @@ public class ShenyuToolCallback implements ToolCallback {
             } catch (URISyntaxException ignore) {
                 shenyuContext.setPath(decoratedPath);
             }
-
-            shenyuContext.setRealUrl(decoratedPath);
-
+            Map<String, Object> attributes = decoratedExchange.getAttributes();
+            String contextPath = (String) 
attributes.get(Constants.CONTEXT_PATH);
+            if (org.apache.commons.lang3.StringUtils.isBlank(contextPath)) {
+                shenyuContext.setRealUrl(decoratedPath);
+            } else {
+                String realURI = decoratedPath.substring(contextPath.length());
+                shenyuContext.setRealUrl(realURI);
+            }
             LOG.debug("Configured RpcType to HTTP for tool call, session: {}", 
sessionId);
 
             decoratedExchange.getAttributes().put(Constants.CONTEXT, 
shenyuContext);
diff --git 
a/shenyu-spring-boot-starter/shenyu-spring-boot-starter-client/shenyu-spring-boot-starter-client-mcp/src/main/java/org/apache/shenyu/springboot/starter/client/mcp/ShenyuMcpClientConfiguration.java
 
b/shenyu-spring-boot-starter/shenyu-spring-boot-starter-client/shenyu-spring-boot-starter-client-mcp/src/main/java/org/apache/shenyu/springboot/starter/client/mcp/ShenyuMcpClientConfiguration.java
index 4bda8db748..15cea78138 100644
--- 
a/shenyu-spring-boot-starter/shenyu-spring-boot-starter-client/shenyu-spring-boot-starter-client-mcp/src/main/java/org/apache/shenyu/springboot/starter/client/mcp/ShenyuMcpClientConfiguration.java
+++ 
b/shenyu-spring-boot-starter/shenyu-spring-boot-starter-client/shenyu-spring-boot-starter-client-mcp/src/main/java/org/apache/shenyu/springboot/starter/client/mcp/ShenyuMcpClientConfiguration.java
@@ -26,6 +26,7 @@ import 
org.springframework.boot.autoconfigure.ImportAutoConfiguration;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
+import org.springframework.core.env.Environment;
 
 /**
  * The type shenyu apache mcp client configuration.
@@ -45,12 +46,14 @@ public class ShenyuMcpClientConfiguration {
      *
      * @param clientConfig the client config
      * @param shenyuClientRegisterRepository the shenyu client register 
repository
+     * @param env the spring environment
      * @return the apache mcp service bean listener
      */
     @Bean
     public McpServiceEventListener mcpServiceEventListener(final 
ShenyuClientConfig clientConfig,
-                                                                  final 
ShenyuClientRegisterRepository shenyuClientRegisterRepository) {
-        return new McpServiceEventListener(clientConfig, 
shenyuClientRegisterRepository);
+                                                           final 
ShenyuClientRegisterRepository shenyuClientRegisterRepository,
+                                                           final Environment 
env) {
+        return new McpServiceEventListener(clientConfig, 
shenyuClientRegisterRepository, env);
     }
 
 }

Reply via email to