diff --git a/mcp/common/src/main/java/org/springframework/ai/mcp/McpToolUtils.java b/mcp/common/src/main/java/org/springframework/ai/mcp/McpToolUtils.java index 1e051dbb0..5925f1ebf 100644 --- a/mcp/common/src/main/java/org/springframework/ai/mcp/McpToolUtils.java +++ b/mcp/common/src/main/java/org/springframework/ai/mcp/McpToolUtils.java @@ -17,6 +17,7 @@ package org.springframework.ai.mcp; import java.util.List; +import io.micrometer.common.util.StringUtils; import io.modelcontextprotocol.client.McpAsyncClient; import io.modelcontextprotocol.client.McpSyncClient; import io.modelcontextprotocol.server.McpServerFeatures; @@ -58,12 +59,12 @@ public final class McpToolUtils { public static String prefixedToolName(String prefix, String toolName) { - String input = prefix + "-" + toolName; - - if (input == null || input.isEmpty()) { - throw new IllegalArgumentException("Input string cannot be null or empty"); + if (StringUtils.isEmpty(prefix) || StringUtils.isEmpty(toolName)) { + throw new IllegalArgumentException("Prefix or toolName cannot be null or empty"); } + String input = prefix + "-" + toolName; + // Replace any character that isn't alphanumeric, underscore, or hyphen with // concatenation String formatted = input.replaceAll("[^a-zA-Z0-9_-]", "");