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 fbc3c4bf49 fix mcp config shenyu context error (#6266)
fbc3c4bf49 is described below

commit fbc3c4bf4975055da422b2d8ae9d0df52321925d
Author: MaMengzhen <[email protected]>
AuthorDate: Mon Jan 19 18:06:13 2026 +0800

    fix mcp config shenyu context error (#6266)
    
    * fix mcp config shenyu context error
    
    * fix mcp config shenyu context error
    
    ---------
    
    Co-authored-by: aias00 <[email protected]>
---
 .../mcp/server/callback/ShenyuToolCallback.java    | 39 +++++++++++++++-------
 1 file changed, 27 insertions(+), 12 deletions(-)

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 de044c9421..e77649d809 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
@@ -633,20 +633,18 @@ public class ShenyuToolCallback implements ToolCallback {
                 decoratedExchange.getAttributes().put(Constants.META_DATA, 
metaData);
                 shenyuContext.setRpcType(metaData.getRpcType());
             }
-            try {
-                URI uri = new URI(decoratedPath);
-                shenyuContext.setPath(uri.getRawPath());
-            } catch (URISyntaxException ignore) {
-                shenyuContext.setPath(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);
+
+            shenyuContext.setPath(extractRawPath(decoratedPath));
+
+            final Map<String, Object> attributes = 
decoratedExchange.getAttributes();
+            final String contextPath = (String) 
attributes.getOrDefault(Constants.CONTEXT_PATH, 
org.apache.commons.lang3.StringUtils.EMPTY);
+            if (org.apache.commons.lang3.StringUtils.isEmpty(contextPath) || 
!decoratedPath.startsWith(contextPath)) {
+                shenyuContext.setRealUrl(extractRawPath(decoratedPath));
             } else {
-                String realURI = decoratedPath.substring(contextPath.length());
-                shenyuContext.setRealUrl(realURI);
+                final String realURI = 
decoratedPath.substring(contextPath.length());
+                shenyuContext.setRealUrl(extractRawPath(realURI));
             }
+
             LOG.debug("Configured RpcType to HTTP for tool call, session: {}", 
sessionId);
 
             decoratedExchange.getAttributes().put(Constants.CONTEXT, 
shenyuContext);
@@ -657,6 +655,23 @@ public class ShenyuToolCallback implements ToolCallback {
         }
     }
 
+    /**
+     * Safely extracts raw path from a URI string, falling back to original 
string on failure.
+     *
+     * @param path the path string to process
+     * @return raw path if URI parsing succeeds, otherwise the original string
+     */
+    private String extractRawPath(final String path) {
+        if (StringUtils.isEmpty(path)) {
+            return path;
+        }
+        try {
+            return new URI(path).getRawPath();
+        } catch (final URISyntaxException e) {
+            return path;
+        }
+    }
+
     /**
      * Configures metadata for the request if available.
      *

Reply via email to