spring-projects/spring-ai · error · IllegalArgumentException

Required no-arg constructor not found in

Error message

Required no-arg constructor not found in 

What it means

MetaUtils.getMeta instantiates a MetaProvider class reflectively and requires a no-arg constructor. This error is thrown (wrapping a NoSuchMethodException) when the configured MetaProvider class has no accessible no-argument constructor, so the library cannot create an instance to read the metadata map.

Source

Thrown at mcp/mcp-annotations/src/main/java/org/springframework/ai/mcp/annotation/common/MetaUtils.java:79

	 * @throws IllegalArgumentException if a no-arg constructor is missing or the instance
	 * cannot be created
	 */
	public static Map<String, Object> getMeta(Class<? extends MetaProvider> metaProviderClass) {

		if (metaProviderClass == null) {
			return null;
		}

		String className = metaProviderClass.getName();
		MetaProvider metaProvider;
		try {
			// Prefer a public no-arg constructor; fall back to a declared no-arg if
			// accessible
			Constructor<? extends MetaProvider> constructor = getConstructor(metaProviderClass);
			metaProvider = constructor.newInstance();
		}
		catch (NoSuchMethodException e) {
			throw new IllegalArgumentException("Required no-arg constructor not found in " + className, e);
		}
		catch (InvocationTargetException | InstantiationException | IllegalAccessException e) {
			throw new IllegalArgumentException(className + " instantiation failed", e);
		}

		Map<String, Object> meta = metaProvider.getMeta();
		return meta == null ? null : Collections.unmodifiableMap(meta);
	}

	/**
	 * Locate a no-argument constructor on the given class: prefer public, otherwise fall
	 * back to a declared no-arg constructor.
	 * @param metaProviderClass the class to inspect
	 * @return the resolved no-arg constructor
	 * @throws NoSuchMethodException if the class does not declare any no-arg constructor
	 */
	private static Constructor<? extends MetaProvider> getConstructor(Class<? extends MetaProvider> metaProviderClass)
			throws NoSuchMethodException {

View on GitHub (pinned to 98a7beda4f)

Solutions

  1. Add a public no-arg constructor to the MetaProvider implementation
  2. Make an inner class static (Java) so it has an implicit no-arg constructor
  3. Move dependency injection out of the constructor and into getMeta() or setters
  4. Verify the class name passed to MetaUtils is the intended concrete class, not an abstract one

Example fix

// before
class MyMetaProvider implements MetaProvider {
    private final String env;
    MyMetaProvider(String env) { this.env = env; }
    public Map<String, Object> getMeta() { return Map.of("env", env); }
}

// after
class MyMetaProvider implements MetaProvider {
    public Map<String, Object> getMeta() {
        return Map.of("env", System.getenv("ENV"));
    }
}
Defensive patterns

Strategy: try-catch

Validate before calling

Class<?> c = metaProviderClass;
while (c != null && !c.isAnonymousClass()) { c = c.getSuperclass(); }
boolean hasNoArgCtor = Arrays.stream(metaProviderClass.getConstructors())
        .anyMatch(ctor -> ctor.getParameterCount() == 0);

Type guard

boolean isInstantiableWithNoArgs(Class<?> c) {
    return !c.isInterface() && !Modifier.isAbstract(c.getModifiers())
        && Arrays.stream(c.getConstructors())
            .anyMatch(k -> k.getParameterCount() == 0);
}

Try / catch

try {
    Map<String, Object> meta = MetaUtils.getMeta(providerClass);
} catch (IllegalArgumentException e) {
    log.error("MetaProvider unusable: {}", e.getMessage(), e.getCause());
    // fall back to empty/default metadata
}

Prevention

When it happens

Trigger: Registering a MetaProvider implementation that only defines parameterized constructors; using a non-static inner class whose constructor implicitly requires an outer instance; a default constructor removed after adding an explicit one.

Common situations: Custom MetaProvider written with constructor-injected dependencies; Kotlin/Java nested class missing 'static' or 'inner' semantics; Lombok or record types without a no-arg constructor.

Related errors


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