Class MethodInvoker

java.lang.Object
com.github.thought2code.mcp.annotated.reflect.MethodInvoker

public final class MethodInvoker extends Object
A utility class for performing reflection operations including instance creation and method invocation.

This class provides static methods for:

  • Creating instances of classes using reflection
  • Invoking methods with various parameter configurations
  • Handling method invocation results and exceptions

All method invocations are wrapped with proper error handling, and results are encapsulated in Invocation objects for consistent error reporting. The class follows the utility class pattern with a private constructor to prevent instantiation.

Author:
codeboyzhou
See Also:
  • Method Details

    • createInstance

      public static Object createInstance(Class<?> clazz)
      Creates a new instance of the specified class using reflection.

      This static method uses reflection to create a new instance of the given class by calling its no-argument constructor. The method requires that the class has a public no-argument constructor.

      Parameters:
      clazz - the class to instantiate
      Returns:
      a new instance of the specified class
      Throws:
      McpServerException - if the instance creation fails due to any reason
      See Also:
    • invoke

      public static Invocation invoke(Object instance, MethodCache methodCache, List<Object> params)
      Invokes the method represented by the specified method cache on the given instance with the provided parameters.

      This method uses reflection to invoke the specified method on the target instance, passing the provided parameters. The method handles various return types and null values:

      • Void return types: Returns a success message indicating the method completed
      • Null return values: Returns a message indicating the method succeeded but returned null
      • Non-null return values: Returns the actual result

      All exceptions are caught and wrapped in an Invocation with appropriate error messages. The method signature is logged for debugging purposes when an error occurs.

      Parameters:
      instance - the instance on which to invoke the method
      methodCache - the method cache containing the method metadata
      params - the list of parameters to pass to the method
      Returns:
      an InvocationResult containing the method result or error information
      See Also:
    • invoke

      public static Invocation invoke(Object instance, MethodCache methodCache)
      Invokes the method represented by the specified method cache on the given instance with no parameters.

      This is a convenience method that invokes a method with an empty parameter list. It delegates to invoke(Object, MethodCache, List) with an empty list.

      Parameters:
      instance - the instance on which to invoke the method
      methodCache - the method cache containing the method metadata
      Returns:
      an InvocationResult containing the method result or error information
      See Also:
    • invoke

      public static Invocation invoke(Object instance, MethodCache methodCache, io.modelcontextprotocol.spec.McpSchema.CompleteRequest.CompleteArgument argument)
      Invokes the method represented by the specified method cache on the given instance with the provided completion argument.

      This is a convenience method for invoking methods that take a single McpSchema.CompleteRequest.CompleteArgument parameter. It wraps the argument in a list and delegates to invoke(Object, MethodCache, List).

      This method is typically used for MCP completion operations where a single completion argument needs to be passed to the method.

      Parameters:
      instance - the instance on which to invoke the method
      methodCache - the method cache containing the method metadata
      argument - the completion argument to pass to the method
      Returns:
      an InvocationResult containing the method result or error information
      See Also: