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 0484d6ab29 Mcp auto register bug fix (#6212)
0484d6ab29 is described below
commit 0484d6ab2940daa01a53914a670c96b425e87baf
Author: Yu Siheng <[email protected]>
AuthorDate: Thu Oct 23 10:07:48 2025 +0800
Mcp auto register bug fix (#6212)
* fix some bug
* fix some bug
* fix:autoRegister error config
* fix
* fix
---------
Co-authored-by: aias00 <[email protected]>
---
.../common/constants/RequestTemplateConstants.java | 7 +++-
.../mcp/generator/McpRequestConfigGenerator.java | 41 ++++++++++++++++------
.../src/main/resources/application.yml | 4 +--
.../server/handler/McpServerPluginDataHandler.java | 2 +-
.../mcp/server/manager/ShenyuMcpServerManager.java | 2 +-
.../mcp/server/request/RequestConfigHelper.java | 2 +-
.../server/request/RequestConfigHelperTest.java | 4 +--
7 files changed, 44 insertions(+), 18 deletions(-)
diff --git
a/shenyu-client/shenyu-client-mcp/shenyu-client-mcp-common/src/main/java/org/apache/shenyu/client/mcp/common/constants/RequestTemplateConstants.java
b/shenyu-client/shenyu-client-mcp/shenyu-client-mcp-common/src/main/java/org/apache/shenyu/client/mcp/common/constants/RequestTemplateConstants.java
index 2b3ee6ad2e..86542f978c 100644
---
a/shenyu-client/shenyu-client-mcp/shenyu-client-mcp-common/src/main/java/org/apache/shenyu/client/mcp/common/constants/RequestTemplateConstants.java
+++
b/shenyu-client/shenyu-client-mcp/shenyu-client-mcp-common/src/main/java/org/apache/shenyu/client/mcp/common/constants/RequestTemplateConstants.java
@@ -35,13 +35,18 @@ public class RequestTemplateConstants {
/**
* the bodyJson key.
*/
- public static final String BODY_JSON_KEY = "bodyJson";
+ public static final String BODY_JSON_KEY = "argsToJsonBody";
/**
* the headers key.
*/
public static final String HEADERS_KEY = "headers";
+ /**
+ * the argsPosition key.
+ */
+ public static final String ARGS_POSITION_KEY = "argsPosition";
+
/**
* the requestTemplate key.
*/
diff --git
a/shenyu-client/shenyu-client-mcp/shenyu-client-mcp-common/src/main/java/org/apache/shenyu/client/mcp/generator/McpRequestConfigGenerator.java
b/shenyu-client/shenyu-client-mcp/shenyu-client-mcp-common/src/main/java/org/apache/shenyu/client/mcp/generator/McpRequestConfigGenerator.java
index 529363ec27..9cd0978e7b 100644
---
a/shenyu-client/shenyu-client-mcp/shenyu-client-mcp-common/src/main/java/org/apache/shenyu/client/mcp/generator/McpRequestConfigGenerator.java
+++
b/shenyu-client/shenyu-client-mcp/shenyu-client-mcp-common/src/main/java/org/apache/shenyu/client/mcp/generator/McpRequestConfigGenerator.java
@@ -18,12 +18,14 @@
package org.apache.shenyu.client.mcp.generator;
import com.google.gson.JsonArray;
+import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import org.apache.shenyu.client.mcp.common.annotation.ShenyuMcpRequestConfig;
import org.apache.shenyu.client.mcp.common.constants.OpenApiConstants;
import org.apache.shenyu.client.mcp.common.constants.RequestTemplateConstants;
import java.util.Arrays;
+import java.util.Objects;
/**
@@ -33,19 +35,21 @@ public class McpRequestConfigGenerator {
public static JsonObject generateRequestConfig(final JsonObject
openApiJson, final ShenyuMcpRequestConfig shenyuMcpRequestConfig) {
+ // requestConfig
JsonObject root = new JsonObject();
+ // requestTemplate
+ JsonObject requestTemplate = new JsonObject();
+ root.add(RequestTemplateConstants.REQUEST_TEMPLATE_KEY,
requestTemplate);
+
// url
- JsonObject server =
openApiJson.get(OpenApiConstants.OPEN_API_SERVER_KEY).getAsJsonObject();
- String serverAddress =
server.get(OpenApiConstants.OPEN_API_SERVER_URL_KEY).getAsString();
JsonObject paths =
openApiJson.get(OpenApiConstants.OPEN_API_PATH_KEY).getAsJsonObject();
String path = null;
for (String methodKey : paths.keySet()) {
path = methodKey;
break;
}
- String url = serverAddress + path;
- root.addProperty(RequestTemplateConstants.URL_KEY, url);
+ requestTemplate.addProperty(RequestTemplateConstants.URL_KEY, path);
// method
JsonObject method = paths.get(path).getAsJsonObject();
@@ -54,14 +58,31 @@ public class McpRequestConfigGenerator {
methodType = methodKey;
break;
}
- root.addProperty(RequestTemplateConstants.METHOD_KEY, methodType);
+ requestTemplate.addProperty(RequestTemplateConstants.METHOD_KEY,
methodType);
- //bodyJson
- root.addProperty(RequestTemplateConstants.BODY_JSON_KEY,
shenyuMcpRequestConfig.bodyJson());
+ // argsPosition
+ JsonObject methodTypeJson = method.getAsJsonObject(methodType);
+ JsonArray parameters =
methodTypeJson.getAsJsonArray(OpenApiConstants.OPEN_API_PATH_OPERATION_METHOD_PARAMETERS_KEY);
+ if (Objects.nonNull(parameters)) {
+ JsonObject argsPosition = new JsonObject();
- // requestTemplate
- JsonObject requestTemplate = new JsonObject();
- root.add(RequestTemplateConstants.REQUEST_TEMPLATE_KEY,
requestTemplate);
+ for (JsonElement parameter : parameters) {
+ JsonObject paramObj = parameter.getAsJsonObject();
+
+ if
(paramObj.has(OpenApiConstants.OPEN_API_PATH_OPERATION_METHOD_PARAMETERS_NAME_KEY)
+ &&
paramObj.has(OpenApiConstants.OPEN_API_OPERATION_PATH_METHOD_PARAMETERS_IN_KEY))
{
+
+ String name =
paramObj.get(OpenApiConstants.OPEN_API_PATH_OPERATION_METHOD_PARAMETERS_NAME_KEY).getAsString();
+ String inValue =
paramObj.get(OpenApiConstants.OPEN_API_OPERATION_PATH_METHOD_PARAMETERS_IN_KEY).getAsString();
+
+ argsPosition.addProperty(name, inValue);
+ }
+ }
+ requestTemplate.add(RequestTemplateConstants.ARGS_POSITION_KEY,
argsPosition);
+ }
+
+ // argsToJsonBody
+ requestTemplate.addProperty(RequestTemplateConstants.BODY_JSON_KEY,
shenyuMcpRequestConfig.bodyJson());
// header
JsonArray headers = new JsonArray();
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 098b570028..b257d6d25e 100644
--- a/shenyu-examples/shenyu-examples-mcp/src/main/resources/application.yml
+++ b/shenyu-examples/shenyu-examples-mcp/src/main/resources/application.yml
@@ -31,5 +31,5 @@ shenyu:
client:
mcp:
props:
- contextPath: /mcp
- appName: mcp
+ contextPath: /order
+ appName: /order
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 d225f1bd5a..97c970d4fa 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
@@ -85,7 +85,7 @@ public class McpServerPluginDataHandler implements
PluginDataHandler {
String path = StringUtils.removeEnd(uri, SLASH);
path = StringUtils.removeEnd(path, STAR);
- ShenyuMcpServer shenyuMcpServer =
GsonUtils.getInstance().fromJson(Objects.isNull(selectorData.getHandle()) ?
DEFAULT_MESSAGE_ENDPOINT : selectorData.getHandle(), ShenyuMcpServer.class);
+ ShenyuMcpServer shenyuMcpServer =
GsonUtils.getInstance().fromJson(StringUtils.isBlank(selectorData.getHandle())
? DEFAULT_MESSAGE_ENDPOINT : selectorData.getHandle(), ShenyuMcpServer.class);
shenyuMcpServer.setPath(path);
CACHED_SERVER.get().cachedHandle(
selectorData.getId(),
diff --git
a/shenyu-plugin/shenyu-plugin-mcp-server/src/main/java/org/apache/shenyu/plugin/mcp/server/manager/ShenyuMcpServerManager.java
b/shenyu-plugin/shenyu-plugin-mcp-server/src/main/java/org/apache/shenyu/plugin/mcp/server/manager/ShenyuMcpServerManager.java
index 57c96b00b4..aaad6a9e72 100644
---
a/shenyu-plugin/shenyu-plugin-mcp-server/src/main/java/org/apache/shenyu/plugin/mcp/server/manager/ShenyuMcpServerManager.java
+++
b/shenyu-plugin/shenyu-plugin-mcp-server/src/main/java/org/apache/shenyu/plugin/mcp/server/manager/ShenyuMcpServerManager.java
@@ -368,7 +368,7 @@ public class ShenyuMcpServerManager {
*/
public synchronized void addTool(final String serverPath, final String
name, final String description,
final String requestTemplate, final String
inputSchema) {
- String normalizedPath = normalizeServerPath(serverPath);
+ String normalizedPath =
normalizeServerPath(extractBasePath(serverPath));
// Remove existing tool first
try {
diff --git
a/shenyu-plugin/shenyu-plugin-mcp-server/src/main/java/org/apache/shenyu/plugin/mcp/server/request/RequestConfigHelper.java
b/shenyu-plugin/shenyu-plugin-mcp-server/src/main/java/org/apache/shenyu/plugin/mcp/server/request/RequestConfigHelper.java
index 556b0cc490..1c70a3bccb 100644
---
a/shenyu-plugin/shenyu-plugin-mcp-server/src/main/java/org/apache/shenyu/plugin/mcp/server/request/RequestConfigHelper.java
+++
b/shenyu-plugin/shenyu-plugin-mcp-server/src/main/java/org/apache/shenyu/plugin/mcp/server/request/RequestConfigHelper.java
@@ -51,7 +51,7 @@ public class RequestConfigHelper {
* @return the argument position json object
*/
public JsonObject getArgsPosition() {
- return configJson.has("argsPosition") ?
configJson.getAsJsonObject("argsPosition") : new JsonObject();
+ return getRequestTemplate().get("argsPosition").getAsJsonObject();
}
/**
diff --git
a/shenyu-plugin/shenyu-plugin-mcp-server/src/test/java/org/apache/shenyu/plugin/mcp/server/request/RequestConfigHelperTest.java
b/shenyu-plugin/shenyu-plugin-mcp-server/src/test/java/org/apache/shenyu/plugin/mcp/server/request/RequestConfigHelperTest.java
index 5b7d02c43b..1d312eb4c2 100644
---
a/shenyu-plugin/shenyu-plugin-mcp-server/src/test/java/org/apache/shenyu/plugin/mcp/server/request/RequestConfigHelperTest.java
+++
b/shenyu-plugin/shenyu-plugin-mcp-server/src/test/java/org/apache/shenyu/plugin/mcp/server/request/RequestConfigHelperTest.java
@@ -33,7 +33,7 @@ class RequestConfigHelperTest {
@Test
void testBasicGetRequest() {
- String configStr =
"{\"requestTemplate\":{\"url\":\"/test\",\"method\":\"GET\"},\"argsPosition\":{}}";
+ String configStr =
"{\"requestTemplate\":{\"url\":\"/test\",\"method\":\"GET\",\"argsPosition\":{}}}";
RequestConfigHelper helper = new RequestConfigHelper(configStr);
assertEquals("/test", helper.getUrlTemplate());
@@ -47,7 +47,7 @@ class RequestConfigHelperTest {
void testPostRequestWithJsonBody() {
String configStr = "{\"requestTemplate\":{\"url\":\"/api/users\","
+ "\"method\":\"POST\",\"headers\":[{\"key\":\"Content-Type\","
- +
"\"value\":\"application/json\"}],\"argsToJsonBody\":true},\"argsPosition\":{\"name\":\"body\",\"email\":\"body\"}}";
+ +
"\"value\":\"application/json\"}],\"argsToJsonBody\":true,\"argsPosition\":{\"name\":\"body\",\"email\":\"body\"}}}";
RequestConfigHelper helper = new RequestConfigHelper(configStr);
assertEquals("/api/users", helper.getUrlTemplate());