|
16 | 16 |
|
17 | 17 | package org.springframework.ai.mcp.server.common.autoconfigure.annotations; |
18 | 18 |
|
| 19 | +import java.lang.reflect.Method; |
19 | 20 | import java.util.ArrayList; |
20 | 21 | import java.util.List; |
| 22 | +import java.util.Map; |
21 | 23 | import java.util.function.BiFunction; |
22 | 24 |
|
| 25 | +import com.fasterxml.jackson.databind.ObjectMapper; |
23 | 26 | import io.modelcontextprotocol.server.McpStatelessServerFeatures; |
24 | 27 | import io.modelcontextprotocol.spec.McpSchema; |
25 | 28 | import io.modelcontextprotocol.spec.McpTransportContext; |
26 | | -import org.reactivestreams.Publisher; |
| 29 | +import org.slf4j.Logger; |
| 30 | +import org.slf4j.LoggerFactory; |
| 31 | +import org.springaicommunity.mcp.annotation.McpTool; |
| 32 | +import org.springaicommunity.mcp.annotation.McpToolParam; |
27 | 33 | import reactor.core.publisher.Flux; |
28 | 34 | import reactor.core.publisher.Mono; |
29 | 35 |
|
| 36 | +import org.springframework.util.ReflectionUtils; |
| 37 | + |
30 | 38 | /** |
31 | | - * Post-processor that attempts to wrap AsyncToolSpecifications to handle Flux return |
32 | | - * types properly by collecting all elements. |
33 | | - * |
| 39 | + * Post-processor that wraps AsyncToolSpecifications to handle Flux return types properly |
| 40 | + * by collecting all elements before serialization. |
| 41 | + * |
34 | 42 | * <p> |
35 | | - * <strong>NOTE:</strong> This class demonstrates the intended fix for Issue #4542 where |
36 | | - * Flux-returning tool methods only return the first element. However, the actual fix |
37 | | - * cannot be applied at this level because the Flux has already been improperly consumed |
38 | | - * by the time the AsyncToolSpecification's callHandler is invoked. |
39 | | - * |
| 43 | + * <strong>Background:</strong> This class fixes Issue #4542 where Flux-returning @McpTool |
| 44 | + * methods only return the first element. The root cause is in the external {@code |
| 45 | + * org.springaicommunity.mcp.provider.tool.AsyncStatelessMcpToolProvider} library, which |
| 46 | + * treats Flux as a single-value Publisher and only takes the first element. |
| 47 | + * |
40 | 48 | * <p> |
41 | | - * The real fix needs to be in the {@code org.springaicommunity.mcp.provider.tool.AsyncStatelessMcpToolProvider} |
42 | | - * class (from the external {@code mcp-annotations} library), where the annotated method |
43 | | - * is invoked and its return value is processed. That code needs to check if the return |
44 | | - * value is a {@code Flux<T>} and call {@code .collectList()} before converting it to a |
45 | | - * {@code CallToolResult}. |
46 | | - * |
| 49 | + * <strong>Solution:</strong> This post-processor intercepts tool specifications and wraps |
| 50 | + * their call handlers. When a method returns a Flux, it collects all elements into a list |
| 51 | + * before passing the result to the MCP serialization layer. |
| 52 | + * |
47 | 53 | * <p> |
48 | | - * <strong>Workaround:</strong> Users should return {@code Mono<List<T>>} instead of |
49 | | - * {@code Flux<T>} from their {@code @McpTool} methods. See |
50 | | - * {@code FLUX-RETURN-TYPE-WORKAROUND.md} for details. |
51 | | - * |
| 54 | + * <strong>Note:</strong> Users can also work around this issue by returning {@code |
| 55 | + * Mono<List<T>>} instead of {@code Flux<T>} from their {@code @McpTool} methods. |
| 56 | + * |
| 57 | + * @author liugddx |
52 | 58 | * @since 1.1.0 |
53 | 59 | * @see <a href="https://github.com/spring-projects/spring-ai/issues/4542">Issue #4542</a> |
54 | 60 | */ |
55 | | -public class FluxToolSpecificationPostProcessor { |
| 61 | +public final class FluxToolSpecificationPostProcessor { |
| 62 | + |
| 63 | + private static final Logger logger = LoggerFactory.getLogger(FluxToolSpecificationPostProcessor.class); |
| 64 | + |
| 65 | + private static final ObjectMapper objectMapper = new ObjectMapper(); |
| 66 | + |
| 67 | + private FluxToolSpecificationPostProcessor() { |
| 68 | + // Utility class - no instances allowed |
| 69 | + } |
56 | 70 |
|
57 | 71 | /** |
58 | 72 | * Wraps tool specifications to properly handle Flux return types by collecting all |
59 | 73 | * elements into a list. |
60 | 74 | * @param originalSpecs the original tool specifications from the annotation provider |
| 75 | + * @param toolBeans the bean objects containing @McpTool annotated methods |
61 | 76 | * @return wrapped tool specifications that properly collect Flux elements |
62 | 77 | */ |
63 | 78 | public static List<McpStatelessServerFeatures.AsyncToolSpecification> processToolSpecifications( |
64 | | - List<McpStatelessServerFeatures.AsyncToolSpecification> originalSpecs) { |
| 79 | + List<McpStatelessServerFeatures.AsyncToolSpecification> originalSpecs, List<Object> toolBeans) { |
65 | 80 |
|
66 | | - List<McpStatelessServerFeatures.AsyncToolSpecification> wrappedSpecs = new ArrayList<>(); |
| 81 | + List<McpStatelessServerFeatures.AsyncToolSpecification> processedSpecs = new ArrayList<>(); |
67 | 82 |
|
68 | 83 | for (McpStatelessServerFeatures.AsyncToolSpecification spec : originalSpecs) { |
69 | | - McpStatelessServerFeatures.AsyncToolSpecification wrappedSpec = wrapToolSpecification(spec); |
70 | | - wrappedSpecs.add(wrappedSpec); |
| 84 | + ToolMethodInfo methodInfo = findToolMethod(toolBeans, spec.tool().name()); |
| 85 | + if (methodInfo != null && methodInfo.returnsFlux()) { |
| 86 | + logger.info("Detected Flux return type for MCP tool '{}', applying collection wrapper", |
| 87 | + spec.tool().name()); |
| 88 | + McpStatelessServerFeatures.AsyncToolSpecification wrappedSpec = wrapToolSpecificationForFlux(spec, |
| 89 | + methodInfo); |
| 90 | + processedSpecs.add(wrappedSpec); |
| 91 | + } |
| 92 | + else { |
| 93 | + processedSpecs.add(spec); |
| 94 | + } |
71 | 95 | } |
72 | 96 |
|
73 | | - return wrappedSpecs; |
| 97 | + return processedSpecs; |
74 | 98 | } |
75 | 99 |
|
76 | | - private static McpStatelessServerFeatures.AsyncToolSpecification wrapToolSpecification( |
77 | | - McpStatelessServerFeatures.AsyncToolSpecification original) { |
| 100 | + /** |
| 101 | + * Finds the method annotated with @McpTool that matches the given tool name. |
| 102 | + * @param toolBeans the bean objects containing @McpTool annotated methods |
| 103 | + * @param toolName the name of the tool to find |
| 104 | + * @return the ToolMethodInfo object, or null if not found |
| 105 | + */ |
| 106 | + private static ToolMethodInfo findToolMethod(List<Object> toolBeans, String toolName) { |
| 107 | + for (Object bean : toolBeans) { |
| 108 | + Class<?> clazz = bean.getClass(); |
| 109 | + Method[] methods = ReflectionUtils.getAllDeclaredMethods(clazz); |
| 110 | + for (Method method : methods) { |
| 111 | + McpTool annotation = method.getAnnotation(McpTool.class); |
| 112 | + if (annotation != null && annotation.name().equals(toolName)) { |
| 113 | + return new ToolMethodInfo(bean, method); |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + return null; |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Wraps a tool specification to collect all Flux elements before serialization. |
| 122 | + * @param original the original tool specification |
| 123 | + * @param methodInfo the method information including bean and method |
| 124 | + * @return the wrapped tool specification |
| 125 | + */ |
| 126 | + private static McpStatelessServerFeatures.AsyncToolSpecification wrapToolSpecificationForFlux( |
| 127 | + McpStatelessServerFeatures.AsyncToolSpecification original, ToolMethodInfo methodInfo) { |
78 | 128 |
|
79 | 129 | BiFunction<McpTransportContext, McpSchema.CallToolRequest, Mono<McpSchema.CallToolResult>> originalHandler = original |
80 | 130 | .callHandler(); |
81 | 131 |
|
82 | 132 | BiFunction<McpTransportContext, McpSchema.CallToolRequest, Mono<McpSchema.CallToolResult>> wrappedHandler = ( |
83 | 133 | context, request) -> { |
84 | | - Mono<McpSchema.CallToolResult> result = originalHandler.apply(context, request); |
85 | | - |
86 | | - // The issue is that if the underlying implementation returns a Flux but |
87 | | - // only emits the first element, we need to ensure all elements are |
88 | | - // collected. |
89 | | - // However, at this level, we receive a Mono<CallToolResult>, so the damage |
90 | | - // is already done. |
91 | | - // The fix needs to be in the org.springaicommunity.mcp library itself. |
92 | | - |
93 | | - return result; |
| 134 | + try { |
| 135 | + // Invoke the method directly to get access to the Flux |
| 136 | + Object[] args = buildMethodArguments(methodInfo.method(), request.arguments()); |
| 137 | + Object result = ReflectionUtils.invokeMethod(methodInfo.method(), methodInfo.bean(), args); |
| 138 | + |
| 139 | + if (result instanceof Flux) { |
| 140 | + // Collect all Flux elements into a list |
| 141 | + Flux<?> flux = (Flux<?>) result; |
| 142 | + return flux.collectList().flatMap(list -> { |
| 143 | + // Serialize the list to JSON |
| 144 | + try { |
| 145 | + String jsonContent = objectMapper.writeValueAsString(list); |
| 146 | + return Mono.just(new McpSchema.CallToolResult( |
| 147 | + List.of(new McpSchema.TextContent(jsonContent)), false)); |
| 148 | + } |
| 149 | + catch (Exception e) { |
| 150 | + logger.error("Failed to serialize Flux result for tool '{}'", original.tool().name(), e); |
| 151 | + return Mono.just(new McpSchema.CallToolResult( |
| 152 | + List.of(new McpSchema.TextContent("Error: " + e.getMessage())), true)); |
| 153 | + } |
| 154 | + }); |
| 155 | + } |
| 156 | + else { |
| 157 | + // Fall back to original handler for non-Flux results |
| 158 | + return originalHandler.apply(context, request); |
| 159 | + } |
| 160 | + } |
| 161 | + catch (Exception e) { |
| 162 | + logger.error("Failed to invoke tool method '{}'", original.tool().name(), e); |
| 163 | + return Mono.just( |
| 164 | + new McpSchema.CallToolResult(List.of(new McpSchema.TextContent("Error: " + e.getMessage())), |
| 165 | + true)); |
| 166 | + } |
94 | 167 | }; |
95 | 168 |
|
96 | 169 | return new McpStatelessServerFeatures.AsyncToolSpecification(original.tool(), wrappedHandler); |
97 | 170 | } |
98 | 171 |
|
99 | 172 | /** |
100 | | - * Helper method to detect if a Publisher might be a Flux that needs special |
101 | | - * handling. |
102 | | - * @param publisher the publisher to check |
103 | | - * @return true if the publisher is a Flux (multi-element stream) |
| 173 | + * Builds method arguments from the request arguments map. |
| 174 | + * @param method the method to invoke |
| 175 | + * @param requestArgs the arguments from the CallToolRequest |
| 176 | + * @return array of method arguments |
104 | 177 | */ |
105 | | - private static boolean isFlux(Publisher<?> publisher) { |
106 | | - return publisher instanceof Flux; |
| 178 | + private static Object[] buildMethodArguments(Method method, Map<String, Object> requestArgs) { |
| 179 | + java.lang.reflect.Parameter[] parameters = method.getParameters(); |
| 180 | + Object[] args = new Object[parameters.length]; |
| 181 | + |
| 182 | + for (int i = 0; i < parameters.length; i++) { |
| 183 | + java.lang.reflect.Parameter param = parameters[i]; |
| 184 | + McpToolParam paramAnnotation = param.getAnnotation(McpToolParam.class); |
| 185 | + |
| 186 | + if (paramAnnotation != null) { |
| 187 | + String paramName = paramAnnotation.name().isEmpty() ? param.getName() : paramAnnotation.name(); |
| 188 | + Object value = requestArgs.get(paramName); |
| 189 | + |
| 190 | + // Type conversion if needed |
| 191 | + if (value != null) { |
| 192 | + args[i] = objectMapper.convertValue(value, param.getType()); |
| 193 | + } |
| 194 | + else if (!paramAnnotation.required()) { |
| 195 | + args[i] = null; |
| 196 | + } |
| 197 | + else { |
| 198 | + throw new IllegalArgumentException("Required parameter '" + paramName + "' is missing"); |
| 199 | + } |
| 200 | + } |
| 201 | + else { |
| 202 | + // Try to match by parameter name |
| 203 | + Object value = requestArgs.get(param.getName()); |
| 204 | + if (value != null) { |
| 205 | + args[i] = objectMapper.convertValue(value, param.getType()); |
| 206 | + } |
| 207 | + else { |
| 208 | + args[i] = null; |
| 209 | + } |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + return args; |
107 | 214 | } |
108 | 215 |
|
109 | 216 | /** |
110 | | - * Attempts to collect all elements from a Flux into a list. If the publisher is |
111 | | - * already a Mono, returns it as-is. |
112 | | - * @param publisher the publisher |
113 | | - * @return a Mono containing either the single value or a list of all values |
| 217 | + * Holds information about a tool method. |
114 | 218 | */ |
115 | | - @SuppressWarnings("unchecked") |
116 | | - private static <T> Mono<T> collectIfFlux(Publisher<T> publisher) { |
117 | | - if (publisher instanceof Flux) { |
118 | | - // Collect all elements into a list |
119 | | - return ((Flux<T>) publisher).collectList().map(list -> (T) list); |
| 219 | + private static class ToolMethodInfo { |
| 220 | + |
| 221 | + private final Object bean; |
| 222 | + |
| 223 | + private final Method method; |
| 224 | + |
| 225 | + ToolMethodInfo(Object bean, Method method) { |
| 226 | + this.bean = bean; |
| 227 | + this.method = method; |
| 228 | + ReflectionUtils.makeAccessible(method); |
120 | 229 | } |
121 | | - else if (publisher instanceof Mono) { |
122 | | - return (Mono<T>) publisher; |
| 230 | + |
| 231 | + Object bean() { |
| 232 | + return this.bean; |
123 | 233 | } |
124 | | - else { |
125 | | - // Generic Publisher - convert to Flux and collect |
126 | | - return Flux.from(publisher).collectList().map(list -> (T) list); |
| 234 | + |
| 235 | + Method method() { |
| 236 | + return this.method; |
127 | 237 | } |
| 238 | + |
| 239 | + boolean returnsFlux() { |
| 240 | + return Flux.class.isAssignableFrom(this.method.getReturnType()); |
| 241 | + } |
| 242 | + |
128 | 243 | } |
129 | 244 |
|
130 | 245 | } |
131 | | - |
|
0 commit comments