Class ModernReactiveSupport
java.lang.Object
org.springframework.ai.mcp.annotation.common.ModernReactiveSupport
Return-value handling shared by the asynchronous method callbacks of a 2026-07-28
("modern") MCP server. An MCP request has one result, so a method answers with a single
value, and
ReactiveAdapterRegistry tells which return types can do that:
- A
Monoor aCompletionStageis single-valued. - A
Flux, or any other concrete multi-valued type the registry knows, is rejected at startup byrejectMultiValuedReturnType(Method). - A method declared to return
Publisher,Flow.PublisherorObjectis resolved from the value it returns. A publisher that emits more than one element fails the call, rather than losing elements or buffering them. - Any other value is a plain result, and the method runs through
McpModernAsyncUtils.fromBlocking(io.modelcontextprotocol.modern.server.McpRequestContext, java.util.concurrent.Callable<T>).
- Since:
- 2.1.0
- Author:
- Christian Tzolov
-
Method Summary
Modifier and TypeMethodDescriptionstatic @Nullable ObjectInvokes the method, surfacing the cause of anInvocationTargetException: aRuntimeExceptionorErroras is, a checked exception wrapped in anUndeclaredThrowableException.static reactor.core.publisher.Mono<Object>invokeToMono(io.modelcontextprotocol.modern.server.McpRequestContext context, Method method, Supplier<@Nullable Object> invocation) Runs a method invocation and normalises what it returns into aMono.static booleanisReactive(Method method) Whether the declared return type is a reactive or asynchronous type: aMono, aPublisher, aCompletionStageand the like.static @Nullable Class<?>outcomeClass(Method method) The raw class ofoutcomeType(Method): the erased return type of a non-reactive method, and for a reactive one the class of its type argument, ornullwhen that is not known or is not a class (a wildcard or a type variable).static @Nullable TypeoutcomeType(Method method) The type the method's outcome has: the type argument of a reactive return, or the generic return type otherwise.static voidrejectMultiValuedReturnType(Method method) Rejects a declared return type that is multi-valued by definition.
-
Method Details
-
isReactive
Whether the declared return type is a reactive or asynchronous type: aMono, aPublisher, aCompletionStageand the like. -
rejectMultiValuedReturnType
Rejects a declared return type that is multi-valued by definition.PublisherandFlow.Publisherare accepted because they say nothing about the number of elements; the returned value decides.- Parameters:
method- the method to check- Throws:
IllegalArgumentException- if the method returns aFluxor another concrete multi-valued type
-
outcomeType
The type the method's outcome has: the type argument of a reactive return, or the generic return type otherwise. A raw reactive return has no known outcome type. -
outcomeClass
The raw class ofoutcomeType(Method): the erased return type of a non-reactive method, and for a reactive one the class of its type argument, ornullwhen that is not known or is not a class (a wildcard or a type variable). -
invoke
Invokes the method, surfacing the cause of anInvocationTargetException: aRuntimeExceptionorErroras is, a checked exception wrapped in anUndeclaredThrowableException. The caller makes the method accessible once, when its callback is built. -
invokeToMono
public static reactor.core.publisher.Mono<Object> invokeToMono(io.modelcontextprotocol.modern.server.McpRequestContext context, Method method, Supplier<@Nullable Object> invocation) Runs a method invocation and normalises what it returns into aMono. A method with a reactive return type is expected not to block, so it is invoked on subscription on the calling thread; any other goes throughMcpModernAsyncUtils.fromBlocking(io.modelcontextprotocol.modern.server.McpRequestContext, java.util.concurrent.Callable<T>).- Parameters:
context- the request contextmethod- the method being invokedinvocation- the invocation of the method- Returns:
- the deferred result, empty when the method answers with nothing
-