spring-projects/spring-ai · error · IllegalArgumentException

Method must not be null

Error message

Method must not be null

What it means

validateMethod in AbstractMcpSamplingMethodCallback rejects a null Method reference before validating its return type and parameters. Sampling/elicitation callbacks must be built around a concrete annotated method.

Source

Thrown at mcp/mcp-annotations/src/main/java/org/springframework/ai/mcp/annotation/method/sampling/AbstractMcpSamplingMethodCallback.java:66

		Assert.notNull(method, "Method can't be null!");
		Assert.notNull(bean, "Bean can't be null!");

		this.method = method;
		this.bean = bean;
		this.validateMethod(this.method);
	}

	/**
	 * Validates that the method signature is compatible with the sampling callback.
	 * <p>
	 * This method checks that the return type is valid and that the parameters match the
	 * expected pattern.
	 * @param method The method to validate
	 * @throws IllegalArgumentException if the method signature is not compatible
	 */
	protected void validateMethod(Method method) {
		if (method == null) {
			throw new IllegalArgumentException("Method must not be null");
		}

		this.validateReturnType(method);
		this.validateParameters(method);
	}

	/**
	 * Validates that the method return type is compatible with the sampling callback.
	 * This method should be implemented by subclasses to handle specific return type
	 * validation.
	 * @param method The method to validate
	 * @throws IllegalArgumentException if the return type is not compatible
	 */
	protected abstract void validateReturnType(Method method);

	/**
	 * Validates method parameters. This method provides common validation logic and
	 * delegates exchange type checking to subclasses.

View on GitHub (pinned to 98a7beda4f)

Solutions

  1. Pass the resolved java.lang.reflect.Method of your @McpSampling/@McpElicitation annotated method
  2. Fix the reflection lookup (correct method name and parameter types) so it does not return null
  3. Ensure the annotated class is registered on the classpath and scanned by the framework

Example fix

// before
new AbstractMcpElicitationMethodCallback(null, bean, ...);
// after
Method m = MyHandlers.class.getDeclaredMethod("myElicitation", ...);
new AbstractMcpElicitationMethodCallback(m, bean, ...);
Defensive patterns

Strategy: type-guard

Validate before calling

if (method == null) {
    throw new IllegalArgumentException("Annotated sampling/elicitation method must be resolved before building callback");
}

Type guard

boolean isResolved(Method m) {
    return m != null && m.getDeclaringClass() != null;
}

Try / catch

try {
    Method m = type.getDeclaredMethod(name, paramTypes);
    return buildCallback(m, bean);
} catch (NoSuchMethodException e) {
    throw new IllegalStateException("Sampling method not found: " + name, e);
}

Prevention

When it happens

Trigger: Passing null to the sampling/elicitation callback constructor or to validateMethod, typically when reflection fails to resolve the annotated method or a builder is given no method.

Common situations: Custom registration code looking up a Method by name that does not exist; framework wiring bug where the annotated bean/method is absent; tests constructing the callback directly with null.

Related errors


AI-assisted analysis of spring-projects/spring-ai@98a7beda4f (2026-09-11). Data as JSON: /api/errors/4121a1d083fafcb5. Report an issue: GitHub.