{"record":{"id":"c1d179c954fbce44","repo":"spring-projects/spring-ai","slug":"request-must-not-be-null-c1d179","errorCode":null,"errorMessage":"Request must not be null","messagePattern":"Request must not be null","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"mcp/mcp-annotations/src/main/java/org/springframework/ai/mcp/annotation/method/elicitation/SyncMcpElicitationMethodCallback.java","lineNumber":61,"sourceCode":"\tprivate SyncMcpElicitationMethodCallback(Builder builder) {\n\t\tsuper(builder.method, builder.bean);\n\t}\n\n\t/**\n\t * Apply the callback to the given request.\n\t * <p>\n\t * This method builds the arguments for the method call, invokes the method, and\n\t * returns the result.\n\t * @param request The elicitation request, must not be null\n\t * @return The result of the method invocation\n\t * @throws McpElicitationMethodException if there is an error invoking the elicitation\n\t * method\n\t * @throws IllegalArgumentException if the request is null\n\t */\n\t@Override\n\tpublic ElicitResult apply(ElicitRequest request) {\n\t\tif (request == null) {\n\t\t\tthrow new IllegalArgumentException(\"Request must not be null\");\n\t\t}\n\n\t\ttry {\n\t\t\t// Build arguments for the method call\n\t\t\tObject[] args = this.buildArgs(this.method, null, request);\n\n\t\t\t// Invoke the method\n\t\t\tthis.method.setAccessible(true);\n\t\t\tObject result = this.method.invoke(this.bean, args);\n\n\t\t\tif (this.method.getReturnType().isAssignableFrom(StructuredElicitResult.class)) {\n\t\t\t\tStructuredElicitResult<?> structuredElicitResult = (StructuredElicitResult<?>) result;\n\t\t\t\tvar content = structuredElicitResult.structuredContent() != null\n\t\t\t\t\t\t? jsonHelper.convertToMap(structuredElicitResult.structuredContent()) : null;\n\n\t\t\t\treturn ElicitResult.builder(structuredElicitResult.action())\n\t\t\t\t\t.content(content)\n\t\t\t\t\t.meta(structuredElicitResult.meta())","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/spring-projects/spring-ai/blob/98a7beda4f29d80a71c5837eb4053b03a93a46f7/mcp/mcp-annotations/src/main/java/org/springframework/ai/mcp/annotation/method/elicitation/SyncMcpElicitationMethodCallback.java#L43-L79","documentation":"Thrown by SyncMcpElicitationMethodCallback.apply(ElicitRequest) when the caller passes a null request. The callback reflects the request into the handler method's single ElicitRequest parameter, so a null argument would cause an NPE downstream; it fails fast with this message.","triggerScenarios":"Invoking the elicitation function manually with null: elicitationHandler.apply(null) — e.g. a test harness or a custom client wiring that forwards a missing elicitation payload.","commonSituations":"Unit tests exercising the callback (as seen by the testNullRequest caller), or a client adapter that doesn't check whether the incoming elicitation message carries a request before invoking the handler.","solutions":["Never call apply(null); construct a valid McpSchema.ElicitRequest with its params/meta","Guard the call site: only invoke the callback when the incoming MCP elicitation request is present","Catch IllegalArgumentException around apply() if null inputs are possible from untrusted wiring and convert to a proper error response"],"exampleFix":"// before\nElicitResult result = callback.apply(null);\n\n// after\nif (incoming != null) {\n    ElicitResult result = callback.apply(incoming);\n}\n","handlingStrategy":"validation","validationCode":"if (request == null) {\n    throw new IllegalStateException(\"No elicitation request received; refusing to invoke handler\");\n}\nElicitResult result = callback.apply(request);","typeGuard":"boolean hasRequest(ElicitRequest req) { return req != null; }","tryCatchPattern":"try {\n    return callback.apply(request);\n} catch (IllegalArgumentException e) {\n    return new ElicitResult(ElicitResult.Action.DECLINE, Map.of());\n}","preventionTips":["Only invoke callbacks from wiring that guarantees a parsed ElicitRequest","Never call apply(null) in tests expecting a result — expect this exception","Normalize missing elicitation payloads into an explicit DECLINE result instead"],"tags":["java","mcp","elicitation","null-argument","fail-fast"],"backgroundTag":"null-argument","analyzedSha":"98a7beda4f29d80a71c5837eb4053b03a93a46f7","analyzedAt":"2026-09-11T14:15:49.441Z","contentChangedAt":"2026-09-11T14:15:49.441Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}