{"record":{"id":"d2275d6366168059","repo":"spring-projects/spring-ai","slug":"method-must-return-createmessageresult-methodnam","errorCode":null,"errorMessage":"Method must return CreateMessageResult: {methodName} in {className} returns {returnTypeName}","messagePattern":"Method must return CreateMessageResult: (.+?) in (.+?) returns (.+?)","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"mcp/mcp-annotations/src/main/java/org/springframework/ai/mcp/annotation/method/sampling/SyncMcpSamplingMethodCallback.java","lineNumber":86,"sourceCode":"\t\t\t// Return the result\n\t\t\treturn (CreateMessageResult) result;\n\t\t}\n\t\tcatch (Exception e) {\n\t\t\tthrow new McpSamplingMethodException(\"Error invoking sampling method: \" + this.method.getName(), e);\n\t\t}\n\t}\n\n\t/**\n\t * Validates that the method return type is compatible with the sampling callback.\n\t * @param method The method to validate\n\t * @throws IllegalArgumentException if the return type is not compatible\n\t */\n\t@Override\n\tprotected void validateReturnType(Method method) {\n\t\tClass<?> returnType = method.getReturnType();\n\n\t\tif (!CreateMessageResult.class.isAssignableFrom(returnType)) {\n\t\t\tthrow new IllegalArgumentException(\"Method must return CreateMessageResult: \" + method.getName() + \" in \"\n\t\t\t\t\t+ method.getDeclaringClass().getName() + \" returns \" + returnType.getName());\n\t\t}\n\t}\n\n\t/**\n\t * Checks if a parameter type is compatible with the exchange type.\n\t * @param paramType The parameter type to check\n\t * @return true if the parameter type is compatible with the exchange type, false\n\t * otherwise\n\t */\n\t@Override\n\tprotected boolean isExchangeType(Class<?> paramType) {\n\t\t// No exchange type for sampling methods\n\t\treturn false;\n\t}\n\n\t/**\n\t * Create a new builder.","sourceCodeStart":68,"sourceCodeEnd":104,"githubUrl":"https://github.com/spring-projects/spring-ai/blob/98a7beda4f29d80a71c5837eb4053b03a93a46f7/mcp/mcp-annotations/src/main/java/org/springframework/ai/mcp/annotation/method/sampling/SyncMcpSamplingMethodCallback.java#L68-L104","documentation":"SyncMcpSamplingMethodCallback.validateReturnType throws IllegalArgumentException when a method registered as a sync sampling callback does not return a CreateMessageResult (or subtype). Unlike the async variant, Mono is not accepted here; the sync path invokes the method and casts the raw result to CreateMessageResult.","triggerScenarios":"Registering a sampling method returning String, void, Mono<CreateMessageResult>, or any other type on the sync callback path, then constructing the callback.","commonSituations":"Using a reactive-style handler method in a SyncSamplingSpecification; forgetting to adapt the return type when migrating from async to sync specs; returning a DTO instead of CreateMessageResult.","solutions":["Change the method return type to CreateMessageResult","Wrap the produced value: return new CreateMessageResult(...) inside the handler","If the handler is truly reactive, register it under AsyncSamplingSpecification instead"],"exampleFix":"// before\nMono<CreateMessageResult> sample(CreateMessageRequest req) { ... }\n// after\nCreateMessageResult sample(CreateMessageRequest req) {\n    return new CreateMessageResult(buildMessage(req));\n}","handlingStrategy":"validation","validationCode":"if (!CreateMessageResult.class.isAssignableFrom(method.getReturnType())) {\n    throw new IllegalArgumentException(method + \" must return CreateMessageResult for sync sampling\");\n}","typeGuard":null,"tryCatchPattern":"try { builder.build(); } catch (IllegalArgumentException e) { log.error(\"Sync sampling return type invalid: {}\", e.getMessage()); }","preventionTips":["Give sync sampling methods plain CreateMessageResult signatures","Route Mono-returning methods to AsyncSamplingSpecification instead","Add reflection-based signature checks in application tests"],"tags":["return-type","sampling","sync"],"backgroundTag":"type-mismatch","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"}