{"record":{"id":"8fe4e58a217dd3f4","repo":"spring-projects/spring-ai","slug":"required-no-arg-constructor-not-found-in","errorCode":null,"errorMessage":"Required no-arg constructor not found in ","messagePattern":"Required no-arg constructor not found in ","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"mcp/mcp-annotations/src/main/java/org/springframework/ai/mcp/annotation/common/MetaUtils.java","lineNumber":79,"sourceCode":"\t * @throws IllegalArgumentException if a no-arg constructor is missing or the instance\n\t * cannot be created\n\t */\n\tpublic static Map<String, Object> getMeta(Class<? extends MetaProvider> metaProviderClass) {\n\n\t\tif (metaProviderClass == null) {\n\t\t\treturn null;\n\t\t}\n\n\t\tString className = metaProviderClass.getName();\n\t\tMetaProvider metaProvider;\n\t\ttry {\n\t\t\t// Prefer a public no-arg constructor; fall back to a declared no-arg if\n\t\t\t// accessible\n\t\t\tConstructor<? extends MetaProvider> constructor = getConstructor(metaProviderClass);\n\t\t\tmetaProvider = constructor.newInstance();\n\t\t}\n\t\tcatch (NoSuchMethodException e) {\n\t\t\tthrow new IllegalArgumentException(\"Required no-arg constructor not found in \" + className, e);\n\t\t}\n\t\tcatch (InvocationTargetException | InstantiationException | IllegalAccessException e) {\n\t\t\tthrow new IllegalArgumentException(className + \" instantiation failed\", e);\n\t\t}\n\n\t\tMap<String, Object> meta = metaProvider.getMeta();\n\t\treturn meta == null ? null : Collections.unmodifiableMap(meta);\n\t}\n\n\t/**\n\t * Locate a no-argument constructor on the given class: prefer public, otherwise fall\n\t * back to a declared no-arg constructor.\n\t * @param metaProviderClass the class to inspect\n\t * @return the resolved no-arg constructor\n\t * @throws NoSuchMethodException if the class does not declare any no-arg constructor\n\t */\n\tprivate static Constructor<? extends MetaProvider> getConstructor(Class<? extends MetaProvider> metaProviderClass)\n\t\t\tthrows NoSuchMethodException {","sourceCodeStart":61,"sourceCodeEnd":97,"githubUrl":"https://github.com/spring-projects/spring-ai/blob/98a7beda4f29d80a71c5837eb4053b03a93a46f7/mcp/mcp-annotations/src/main/java/org/springframework/ai/mcp/annotation/common/MetaUtils.java#L61-L97","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Add a public no-arg constructor to the MetaProvider implementation","Make an inner class static (Java) so it has an implicit no-arg constructor","Move dependency injection out of the constructor and into getMeta() or setters","Verify the class name passed to MetaUtils is the intended concrete class, not an abstract one"],"exampleFix":"// before\nclass MyMetaProvider implements MetaProvider {\n    private final String env;\n    MyMetaProvider(String env) { this.env = env; }\n    public Map<String, Object> getMeta() { return Map.of(\"env\", env); }\n}\n\n// after\nclass MyMetaProvider implements MetaProvider {\n    public Map<String, Object> getMeta() {\n        return Map.of(\"env\", System.getenv(\"ENV\"));\n    }\n}","handlingStrategy":"try-catch","validationCode":"Class<?> c = metaProviderClass;\nwhile (c != null && !c.isAnonymousClass()) { c = c.getSuperclass(); }\nboolean hasNoArgCtor = Arrays.stream(metaProviderClass.getConstructors())\n        .anyMatch(ctor -> ctor.getParameterCount() == 0);","typeGuard":"boolean isInstantiableWithNoArgs(Class<?> c) {\n    return !c.isInterface() && !Modifier.isAbstract(c.getModifiers())\n        && Arrays.stream(c.getConstructors())\n            .anyMatch(k -> k.getParameterCount() == 0);\n}","tryCatchPattern":"try {\n    Map<String, Object> meta = MetaUtils.getMeta(providerClass);\n} catch (IllegalArgumentException e) {\n    log.error(\"MetaProvider unusable: {}\", e.getMessage(), e.getCause());\n    // fall back to empty/default metadata\n}","preventionTips":["Give every MetaProvider a public no-arg constructor","Make nested provider classes static","Avoid constructor parameters; use getMeta() to gather data"],"tags":["reflection","mcp","constructor"],"backgroundTag":"missing-required-constructor","analyzedSha":"98a7beda4f29d80a71c5837eb4053b03a93a46f7","analyzedAt":"2026-09-11T14:15:49.441Z","contentChangedAt":"2026-09-11T14:15:49.441Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}