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

Aias00 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 510f65a361 fix(mcp): remove tools by the configured name on rule 
removal (#7428)
510f65a361 is described below

commit 510f65a3619fe42c85766b0129444e9f6b3f1bd5
Author: Sean-Walker0 <[email protected]>
AuthorDate: Thu Oct 8 09:32:35 2026 +0800

    fix(mcp): remove tools by the configured name on rule removal (#7428)
    
    handlerRule() registers an MCP tool under the rule handle's tool name
    when one is configured, falling back to the rule name otherwise.
    removeRule() always removed by ruleData.getName(), so whenever the
    handle carried a custom tool name the live MCP server kept the stale
    tool registered after the rule was deleted or updated.
    
    Resolve the removal name with the same handle-first fallback used at
    registration. Regression test registers a tool with a custom name and
    asserts removal targets that name (red on the previous code, which
    removed by the rule name).
    
    Fixes #6473
    
    Co-authored-by: Sean-Walker0 
<[email protected]>
---
 .../server/handler/McpServerPluginDataHandler.java |  4 +++-
 .../handler/McpServerPluginDataHandlerTest.java    | 26 ++++++++++++++++++++--
 2 files changed, 27 insertions(+), 3 deletions(-)

diff --git 
a/shenyu-plugin/shenyu-plugin-mcp-server/src/main/java/org/apache/shenyu/plugin/mcp/server/handler/McpServerPluginDataHandler.java
 
b/shenyu-plugin/shenyu-plugin-mcp-server/src/main/java/org/apache/shenyu/plugin/mcp/server/handler/McpServerPluginDataHandler.java
index be8a7af7f9..b083dd21d6 100644
--- 
a/shenyu-plugin/shenyu-plugin-mcp-server/src/main/java/org/apache/shenyu/plugin/mcp/server/handler/McpServerPluginDataHandler.java
+++ 
b/shenyu-plugin/shenyu-plugin-mcp-server/src/main/java/org/apache/shenyu/plugin/mcp/server/handler/McpServerPluginDataHandler.java
@@ -158,9 +158,11 @@ public class McpServerPluginDataHandler implements 
PluginDataHandler {
         }
         Optional.ofNullable(ruleData.getHandle()).ifPresent(s -> {
             
CACHED_TOOL.get().removeHandle(CacheKeyUtils.INST.getKey(ruleData));
+            ShenyuMcpServerTool mcpServerTool = 
GsonUtils.getInstance().fromJson(s, ShenyuMcpServerTool.class);
             ShenyuMcpServer server = 
CACHED_SERVER.get().obtainHandle(ruleData.getSelectorId());
             if (Objects.nonNull(server) && 
StringUtils.isNotBlank(server.getPath())) {
-                shenyuMcpServerManager.removeTool(server.getPath(), 
ruleData.getName());
+                shenyuMcpServerManager.removeTool(server.getPath(),
+                        StringUtils.isBlank(mcpServerTool.getName()) ? 
ruleData.getName() : mcpServerTool.getName());
             }
         });
         MetaDataCache.getInstance().clean();
diff --git 
a/shenyu-plugin/shenyu-plugin-mcp-server/src/test/java/org/apache/shenyu/plugin/mcp/server/handler/McpServerPluginDataHandlerTest.java
 
b/shenyu-plugin/shenyu-plugin-mcp-server/src/test/java/org/apache/shenyu/plugin/mcp/server/handler/McpServerPluginDataHandlerTest.java
index ad7f35e4ea..7d44fa9052 100644
--- 
a/shenyu-plugin/shenyu-plugin-mcp-server/src/test/java/org/apache/shenyu/plugin/mcp/server/handler/McpServerPluginDataHandlerTest.java
+++ 
b/shenyu-plugin/shenyu-plugin-mcp-server/src/test/java/org/apache/shenyu/plugin/mcp/server/handler/McpServerPluginDataHandlerTest.java
@@ -230,9 +230,31 @@ class McpServerPluginDataHandlerTest {
         RuleData ruleData = new RuleData();
         ruleData.setId("rule1");
         ruleData.setHandle(null);
-        
+
         dataHandler.removeRule(ruleData);
-        
+
         verify(shenyuMcpServerManager, never()).removeTool(anyString(), 
anyString());
     }
+
+    @Test
+    void testRemoveRuleRemovesToolByConfiguredName() {
+        RuleData ruleData = new RuleData();
+        ruleData.setId("rule1");
+        ruleData.setSelectorId("selector1");
+        ruleData.setName("testTool");
+        ruleData.setHandle("{\"name\":\"customTool\",\"description\":\"A test 
tool\",\"requestConfig\":\"{\\\"url\\\":\\\"/test\\\",\\\"method\\\":\\\"GET\\\"}\",\"parameters\":[]}");
+
+        org.apache.shenyu.plugin.mcp.server.model.ShenyuMcpServer server = new 
org.apache.shenyu.plugin.mcp.server.model.ShenyuMcpServer();
+        server.setPath("/mcp/test");
+        
McpServerPluginDataHandler.CACHED_SERVER.get().cachedHandle("selector1", 
server);
+
+        dataHandler.handlerRule(ruleData);
+        verify(shenyuMcpServerManager).addTool(eq("/mcp/test"), 
eq("customTool"), anyString(), anyString(), anyString());
+
+        doNothing().when(shenyuMcpServerManager).removeTool(anyString(), 
anyString());
+        dataHandler.removeRule(ruleData);
+
+        verify(shenyuMcpServerManager).removeTool(eq("/mcp/test"), 
eq("customTool"));
+        verify(shenyuMcpServerManager, never()).removeTool(eq("/mcp/test"), 
eq("testTool"));
+    }
 }

Reply via email to