Skip to content

Commit 6f5ffe8

Browse files
authored
Merge branch 'master' into feat/improve
2 parents 72648c5 + 3c29cc3 commit 6f5ffe8

File tree

1 file changed

+61
-16
lines changed
  • shenyu-plugin/shenyu-plugin-mcp-server/src/main/java/org/apache/shenyu/plugin/mcp/server/callback

1 file changed

+61
-16
lines changed

shenyu-plugin/shenyu-plugin-mcp-server/src/main/java/org/apache/shenyu/plugin/mcp/server/callback/ShenyuToolCallback.java

Lines changed: 61 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.apache.shenyu.plugin.mcp.server.callback;
1919

2020
import com.google.common.collect.Maps;
21+
import com.google.gson.JsonArray;
2122
import com.google.gson.JsonElement;
2223
import com.google.gson.JsonObject;
2324
import io.modelcontextprotocol.server.McpSyncServerExchange;
@@ -55,6 +56,8 @@
5556
import java.util.Objects;
5657
import java.util.concurrent.CompletableFuture;
5758
import java.util.concurrent.TimeUnit;
59+
import java.util.regex.Matcher;
60+
import java.util.regex.Pattern;
5861

5962
/**
6063
* Tool Callback Implementation for Shenyu Gateway MCP Integration.
@@ -92,6 +95,11 @@ public class ShenyuToolCallback implements ToolCallback {
9295
*/
9396
private static final String STREAMABLE_HTTP_PATH = "/streamablehttp";
9497

98+
/**
99+
* Regex pattern for template variable interpolation in tool inputs.
100+
**/
101+
private static final Pattern TEMPLATE_VARIABLE_PATTERN = Pattern.compile("\\{\\{\\.(.*?)\\}\\}");
102+
95103
private final ToolDefinition toolDefinition;
96104

97105
/**
@@ -467,25 +475,62 @@ private ServerHttpRequest buildDecoratedRequest(final ServerWebExchange originEx
467475
*/
468476
private void addCustomHeaders(final ServerHttpRequest.Builder requestBuilder,
469477
final RequestConfig requestConfig) {
470-
if (requestConfig.getRequestTemplate().has("headers")) {
471-
for (final var headerElem : requestConfig.getRequestTemplate().getAsJsonArray("headers")) {
472-
final JsonObject headerObj = headerElem.getAsJsonObject();
473-
String headerKey = headerObj.get("key").getAsString();
474-
String headerValue = headerObj.get("value").getAsString();
475-
if (headerValue.contains("{{.")) {
476-
JsonObject inputJson = requestConfig.getInputJson();
477-
if (Objects.nonNull(inputJson)) {
478-
for (String key : inputJson.keySet()) {
479-
if (inputJson.get(key).isJsonPrimitive() && inputJson.get(key).getAsJsonPrimitive().isString()) {
480-
String value = inputJson.get(key).getAsString();
481-
headerValue = headerValue.replace("{{." + key + "}}", value);
482-
}
483-
}
484-
}
478+
if (!requestConfig.getRequestTemplate().has("headers")) {
479+
return;
480+
}
481+
482+
JsonArray headersArray = requestConfig.getRequestTemplate().getAsJsonArray("headers");
483+
if (Objects.isNull(headersArray) || headersArray.isEmpty()) {
484+
return;
485+
}
486+
487+
JsonObject inputJson = requestConfig.getInputJson();
488+
for (JsonElement headerElem : headersArray) {
489+
if (!headerElem.isJsonObject()) {
490+
continue;
491+
}
492+
493+
JsonObject headerObj = headerElem.getAsJsonObject();
494+
if (!headerObj.has("key") || !headerObj.has("value")
495+
|| !headerObj.get("key").isJsonPrimitive() || !headerObj.get("value").isJsonPrimitive()) {
496+
continue;
497+
}
498+
499+
String headerKey = headerObj.get("key").getAsString();
500+
String headerValue = headerObj.get("value").getAsString();
501+
502+
// Process template variables if present
503+
if (headerValue.contains("{{.") && Objects.nonNull(inputJson)) {
504+
headerValue = resolveTemplateVariables(headerValue, inputJson);
505+
}
506+
507+
requestBuilder.header(headerKey, headerValue);
508+
}
509+
}
510+
511+
/**
512+
* Resolves template variables in the format {{.variableName}} with values from the input JSON.
513+
*
514+
* @param templateValue the template string containing variables
515+
* @param inputJson the JSON object containing values for substitution
516+
* @return the resolved string with variables replaced
517+
*/
518+
private String resolveTemplateVariables(final String templateValue, final JsonObject inputJson) {
519+
String result = templateValue;
520+
Matcher matcher = TEMPLATE_VARIABLE_PATTERN.matcher(templateValue);
521+
522+
while (matcher.find()) {
523+
String variableName = matcher.group(1);
524+
if (inputJson.has(variableName)) {
525+
JsonElement element = inputJson.get(variableName);
526+
if (element.isJsonPrimitive()) {
527+
String value = element.getAsString();
528+
result = result.replace("{{." + variableName + "}}", value);
485529
}
486-
requestBuilder.header(headerKey, headerValue);
487530
}
488531
}
532+
533+
return result;
489534
}
490535

491536
/**

0 commit comments

Comments
 (0)