Class AbstractMcpResourceMethodCallback
java.lang.Object
org.springaicommunity.mcp.method.resource.AbstractMcpResourceMethodCallback
- Direct Known Subclasses:
AsyncMcpResourceMethodCallback,AsyncStatelessMcpResourceMethodCallback,SyncMcpResourceMethodCallback,SyncStatelessMcpResourceMethodCallback
Abstract base class for creating callbacks around resource methods.
This class provides common functionality for both synchronous and asynchronous resource
method callbacks. It contains shared logic for method validation, argument building,
and other common operations.
- Author:
- Christian Tzolov
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static classAbstractMcpResourceMethodCallback.AbstractBuilder<T extends AbstractMcpResourceMethodCallback.AbstractBuilder<T,R>, R> Abstract builder for creating McpResourceMethodCallback instances.static enumContent type of the resource. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final Objectprotected final AbstractMcpResourceMethodCallback.ContentTypeprotected final Stringprotected final Methodprotected final Stringprotected final Stringprotected final McpReadResourceResultConverterprotected final Stringprotected final io.modelcontextprotocol.util.McpUriTemplateManager -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedAbstractMcpResourceMethodCallback(Method method, Object bean, String uri, String name, String description, String mimeType, McpReadResourceResultConverter resultConverter, io.modelcontextprotocol.util.McpUriTemplateManagerFactory uriTemplateMangerFactory, AbstractMcpResourceMethodCallback.ContentType contentType) Constructor for AbstractMcpResourceMethodCallback. -
Method Summary
Modifier and TypeMethodDescriptionprotected abstract ObjectassignExchangeType(Class<?> paramType, Object exchange) protected Object[]buildArgs(Method method, Object exchange, io.modelcontextprotocol.spec.McpSchema.ReadResourceRequest request, Map<String, String> uriVariableValues) Builds the arguments array for invoking the method.protected voidbuildArgsWithoutUriVariables(Parameter[] parameters, Object[] args, Object exchange, io.modelcontextprotocol.spec.McpSchema.ReadResourceRequest request) Builds arguments for methods without URI variables.protected voidbuildArgsWithUriVariables(Parameter[] parameters, Object[] args, Object exchange, io.modelcontextprotocol.spec.McpSchema.ReadResourceRequest request, Map<String, String> uriVariableValues) Builds arguments for methods with URI variables.Returns the content type of the resource.protected abstract booleanisExchangeOrContextType(Class<?> paramType) Checks if a parameter type is compatible with the exchange type.protected voidvalidateMethod(Method method) Validates that the method signature is compatible with the resource callback.protected voidValidates method parameters when no URI variables are present.protected voidValidates method parameters when URI variables are present.protected voidvalidateParamType(Class<?> paramType) protected abstract voidvalidateReturnType(Method method) Validates that the method return type is compatible with the resource callback.
-
Field Details
-
method
-
bean
-
uri
-
name
-
description
-
mimeType
-
uriVariables
-
resultConverter
-
uriTemplateManager
protected final io.modelcontextprotocol.util.McpUriTemplateManager uriTemplateManager -
contentType
-
-
Constructor Details
-
AbstractMcpResourceMethodCallback
protected AbstractMcpResourceMethodCallback(Method method, Object bean, String uri, String name, String description, String mimeType, McpReadResourceResultConverter resultConverter, io.modelcontextprotocol.util.McpUriTemplateManagerFactory uriTemplateMangerFactory, AbstractMcpResourceMethodCallback.ContentType contentType) Constructor for AbstractMcpResourceMethodCallback.- Parameters:
method- The method to create a callback forbean- The bean instance that contains the methoduri- The URI for the resourcename- The name of the resource (optional)description- The description of the resource (optional)mimeType- The MIME type of the resource (optional)resultConverter- The result converteruriTemplateMangerFactory- The URI template manager factorycontentType- The content type
-
-
Method Details
-
validateMethod
Validates that the method signature is compatible with the resource callback.This method checks that the return type is valid and that the parameters match the expected pattern based on whether URI variables are present.
- Parameters:
method- The method to validate- Throws:
IllegalArgumentException- if the method signature is not compatible
-
validateReturnType
Validates that the method return type is compatible with the resource callback. This method should be implemented by subclasses to handle specific return type validation.- Parameters:
method- The method to validate- Throws:
IllegalArgumentException- if the return type is not compatible
-
validateParametersWithoutUriVariables
Validates method parameters when no URI variables are present. This method provides common validation logic and delegates exchange type checking to subclasses.- Parameters:
method- The method to validate- Throws:
IllegalArgumentException- if the parameters are not compatible
-
validateParamType
-
validateParametersWithUriVariables
Validates method parameters when URI variables are present. This method provides common validation logic and delegates exchange type checking to subclasses.- Parameters:
method- The method to validate- Throws:
IllegalArgumentException- if the parameters are not compatible
-
assignExchangeType
-
buildArgs
protected Object[] buildArgs(Method method, Object exchange, io.modelcontextprotocol.spec.McpSchema.ReadResourceRequest request, Map<String, String> uriVariableValues) Builds the arguments array for invoking the method.This method constructs an array of arguments based on the method's parameter types and the available values (exchange, request, URI variables, progress token).
- Parameters:
method- The method to build arguments forexchange- The server exchangerequest- The resource requesturiVariableValues- Map of URI variable names to their values- Returns:
- An array of arguments for the method invocation
-
buildArgsWithUriVariables
protected void buildArgsWithUriVariables(Parameter[] parameters, Object[] args, Object exchange, io.modelcontextprotocol.spec.McpSchema.ReadResourceRequest request, Map<String, String> uriVariableValues) Builds arguments for methods with URI variables. This method provides common argument building logic for methods with URI variables.- Parameters:
parameters- The method parametersargs- The arguments array to populateexchange- The server exchangerequest- The resource requesturiVariableValues- Map of URI variable names to their values
-
buildArgsWithoutUriVariables
protected void buildArgsWithoutUriVariables(Parameter[] parameters, Object[] args, Object exchange, io.modelcontextprotocol.spec.McpSchema.ReadResourceRequest request) Builds arguments for methods without URI variables. This method provides common argument building logic for methods without URI variables.- Parameters:
parameters- The method parametersargs- The arguments array to populateexchange- The server exchangerequest- The resource request
-
isExchangeOrContextType
Checks if a parameter type is compatible with the exchange type. This method should be implemented by subclasses to handle specific exchange type checking.- Parameters:
paramType- The parameter type to check- Returns:
- true if the parameter type is compatible with the exchange type, false otherwise
-
contentType
Returns the content type of the resource.- Returns:
- the content type
-