Skip to content

Commit 75ade36

Browse files
authored
fix: handle tool not found error and update requestConfig type to JsonElement (#6284)
1 parent dff154a commit 75ade36

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

shenyu-plugin/shenyu-plugin-mcp-server/src/main/java/org/apache/shenyu/plugin/mcp/server/manager/ShenyuMcpServerManager.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,13 +432,30 @@ public void removeTool(final String serverPath, final String name) {
432432
LOG.info("Removed tool '{}' from shared server for path: {} (removed from protocols: {})",
433433
name, normalizedPath, protocols);
434434
} catch (Exception e) {
435+
if (isToolNotFoundError(e)) {
436+
LOG.debug("Tool '{}' not found on shared server for path: {} (skip removal)",
437+
name, normalizedPath);
438+
return;
439+
}
435440
LOG.error("Failed to remove tool '{}' from shared server for path: {}", name, normalizedPath, e);
436441
}
437442
} else {
438443
LOG.warn("No shared server found for path: {}", normalizedPath);
439444
}
440445
}
441446

447+
private boolean isToolNotFoundError(final Throwable error) {
448+
Throwable current = error;
449+
while (Objects.nonNull(current)) {
450+
String message = current.getMessage();
451+
if (Objects.nonNull(message) && message.contains("Tool with name") && message.contains("not found")) {
452+
return true;
453+
}
454+
current = current.getCause();
455+
}
456+
return false;
457+
}
458+
442459
/**
443460
* Get supported protocols for a server path.
444461
*

shenyu-plugin/shenyu-plugin-mcp-server/src/main/java/org/apache/shenyu/plugin/mcp/server/model/ShenyuMcpServerTool.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
package org.apache.shenyu.plugin.mcp.server.model;
1919

2020
import com.google.common.collect.Lists;
21+
import com.google.gson.JsonElement;
22+
import com.google.gson.JsonPrimitive;
2123

2224
import java.util.ArrayList;
2325
import java.util.List;
@@ -43,7 +45,7 @@ public class ShenyuMcpServerTool {
4345
/**
4446
* requestTemplate of the tool .
4547
*/
46-
private String requestConfig;
48+
private JsonElement requestConfig;
4749

4850
/**
4951
* Parameters of the tool .
@@ -93,7 +95,13 @@ public void setDescription(final String description) {
9395
* @return requestConfig
9496
*/
9597
public String getRequestConfig() {
96-
return requestConfig;
98+
if (Objects.isNull(requestConfig) || requestConfig.isJsonNull()) {
99+
return null;
100+
}
101+
if (requestConfig.isJsonPrimitive() && requestConfig.getAsJsonPrimitive().isString()) {
102+
return requestConfig.getAsString();
103+
}
104+
return requestConfig.toString();
97105
}
98106

99107
/**
@@ -102,6 +110,19 @@ public String getRequestConfig() {
102110
* @param requestConfig requestConfig
103111
*/
104112
public void setRequestConfig(final String requestConfig) {
113+
if (Objects.isNull(requestConfig)) {
114+
this.requestConfig = null;
115+
return;
116+
}
117+
this.requestConfig = new JsonPrimitive(requestConfig);
118+
}
119+
120+
/**
121+
* Setter for requestConfig.
122+
*
123+
* @param requestConfig requestConfig
124+
*/
125+
public void setRequestConfig(final JsonElement requestConfig) {
105126
this.requestConfig = requestConfig;
106127
}
107128

@@ -157,7 +178,7 @@ public String toString() {
157178
return String.format("McpServerPluginRuleHandle: name: %s, description: %s, requestConfig: %s, parameters: %s",
158179
name,
159180
description,
160-
requestConfig,
181+
getRequestConfig(),
161182
parameters);
162183
}
163184
}

0 commit comments

Comments
 (0)