Class AbstractMcpToolMethodCallback<T,RC extends McpRequestContextTypes<?>>
java.lang.Object
org.springaicommunity.mcp.method.tool.AbstractMcpToolMethodCallback<T,RC>
- Type Parameters:
T- The type of the context parameter (e.g., McpTransportContext, McpSyncServerExchange, or McpAsyncServerExchange)
- Direct Known Subclasses:
AbstractAsyncMcpToolMethodCallback
public abstract class AbstractMcpToolMethodCallback<T,RC extends McpRequestContextTypes<?>>
extends Object
Abstract base class for creating Function callbacks around tool methods.
This class provides common functionality for converting methods annotated with
McpTool into callback functions that can be used to handle tool requests. It
contains all the shared logic between synchronous and asynchronous implementations.- Author:
- Christian Tzolov
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final ReturnModeprotected final Methodprotected final Object -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedAbstractMcpToolMethodCallback(ReturnMode returnMode, Method toolMethod, Object toolObject) -
Method Summary
Modifier and TypeMethodDescriptionprotected Object[]buildMethodArguments(T exchangeOrContext, Map<String, Object> toolInputArguments, io.modelcontextprotocol.spec.McpSchema.CallToolRequest request) Builds the method arguments from the context, tool input arguments, and optionally the full request.protected ObjectbuildTypedArgument(Object value, Type type) Builds a typed argument from a raw value and type information.protected ObjectcallMethod(Object[] methodArguments) Invokes the tool method with the provided arguments.protected io.modelcontextprotocol.spec.McpSchema.CallToolResultconvertValueToCallToolResult(Object result) Converts a method result value to a CallToolResult based on the return mode and type.protected StringCreates the base error message for exceptions that occur during method invocation.protected abstract RCcreateRequestContext(T exchange, io.modelcontextprotocol.spec.McpSchema.CallToolRequest request) protected ThrowablefindCauseUsingPlainJava(Throwable throwable) protected abstract booleanisExchangeOrContextType(Class<?> paramType) Determines if the given parameter type is an exchange or context type that should be injected.
-
Field Details
-
toolMethod
-
toolObject
-
returnMode
-
-
Constructor Details
-
AbstractMcpToolMethodCallback
protected AbstractMcpToolMethodCallback(ReturnMode returnMode, Method toolMethod, Object toolObject)
-
-
Method Details
-
callMethod
Invokes the tool method with the provided arguments.- Parameters:
methodArguments- The arguments to pass to the method- Returns:
- The result of the method invocation
- Throws:
IllegalStateException- if the method cannot be accessedRuntimeException- if there's an error invoking the method
-
buildMethodArguments
protected Object[] buildMethodArguments(T exchangeOrContext, Map<String, Object> toolInputArguments, io.modelcontextprotocol.spec.McpSchema.CallToolRequest request) Builds the method arguments from the context, tool input arguments, and optionally the full request.- Parameters:
exchangeOrContext- The exchange or context object (e.g., McpSyncServerExchange, McpAsyncServerExchange, or McpTransportContext)toolInputArguments- The input arguments from the tool requestrequest- The full CallToolRequest (optional, can be null)- Returns:
- An array of method arguments
-
buildTypedArgument
Builds a typed argument from a raw value and type information.- Parameters:
value- The raw valuetype- The target type- Returns:
- The typed argument
-
convertValueToCallToolResult
protected io.modelcontextprotocol.spec.McpSchema.CallToolResult convertValueToCallToolResult(Object result) Converts a method result value to a CallToolResult based on the return mode and type. This method contains the common logic for processing results that is shared between synchronous and asynchronous implementations.- Parameters:
result- The result value to convert- Returns:
- A CallToolResult representing the processed result
-
createErrorMessage
Creates the base error message for exceptions that occur during method invocation.- Parameters:
e- The exception that occurred- Returns:
- The error message string
-
isExchangeOrContextType
Determines if the given parameter type is an exchange or context type that should be injected. Subclasses must implement this method to specify which types are considered exchange or context types.- Parameters:
paramType- The parameter type to check- Returns:
- true if the parameter type is an exchange or context type, false otherwise
-
findCauseUsingPlainJava
-
createRequestContext
-