spring-projects/spring-ai · error · IllegalArgumentException

Method must not be null

Error message

Method must not be null

What it means

AbstractMcpResourceMethodCallback.validateMethod was invoked with a null Method reference. Every registered resource method must be a resolved java.lang.reflect.Method for the framework to validate its signature and build invocation arguments, so a null is rejected with IllegalArgumentException.

Source

Thrown at mcp/mcp-annotations/src/main/java/org/springframework/ai/mcp/annotation/method/resource/AbstractMcpResourceMethodCallback.java:144

		this.uriTemplateManager = uriTemplateMangerFactory.create(this.uri);

		this.uriVariables = this.uriTemplateManager.getVariableNames();

		this.contentType = contentType;
		this.meta = meta;
	}

	/**
	 * Validates that the method signature is compatible with the resource callback.
	 * <p>
	 * This method checks that the return type is valid and that the parameters match the
	 * expected pattern based on whether URI variables are present.
	 * @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);

		if (this.uriVariables.isEmpty()) {
			this.validateParametersWithoutUriVariables(method);
		}
		else {
			this.validateParametersWithUriVariables(method);
		}
	}

	/**
	 * 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.
	 * @param method The method to validate
	 * @throws IllegalArgumentException if the return type is not compatible

View on GitHub (pinned to 98a7beda4f)

Solutions

  1. Pass the actual annotated Method object when building the callback (e.g. getClass().getDeclaredMethod(...)).
  2. Check the builder chain for a missing .method(...) call before execute/build.
  3. Fix any reflection code returning null and add a null check before registration.

Example fix

// before
new McpResourceMethodCallback.Builder().uriVariables(uris).build(); // method missing

// after
Method m = MyResources.class.getDeclaredMethod("readFile", String.class);
new McpResourceMethodCallback.Builder().method(m).uriVariables(uris).build();
Defensive patterns

Strategy: validation

Validate before calling

Objects.requireNonNull(method, "Method must be resolved before building McpResourceMethodCallback");

Try / catch

Method m = resolveMethod(resourceClass);
if (m == null) {
    throw new IllegalStateException("@McpResource method not found");
}
callback = new McpResourceMethodCallback.Builder().method(m).build();

Prevention

When it happens

Trigger: Programmatically constructing an McpResourceMethodCallback (builder or constructor) without calling .method(...)/passing null; reflection lookups that fail silently and return null before registration.

Common situations: Manual wiring of resource method callbacks instead of using annotation scanning; tests assembling callbacks by hand; refactors that removed the method setter call.

Related errors


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