spring-projects/spring-ai · error · McpLoggingConsumerMethodException
Error invoking logging consumer method:
Error message
Error invoking logging consumer method:
What it means
The annotated sync logging consumer method threw while being reflectively invoked. SyncMcpLoggingMethodCallback catches the reflection exception and rethrows it as McpLoggingConsumerMethodException with message 'Error invoking logging consumer method: <methodName>', preserving the original as the cause. This is a wrapper — the real fault is in the user's handler method or argument binding.
Source
Thrown at mcp/mcp-annotations/src/main/java/org/springframework/ai/mcp/annotation/method/logging/SyncMcpLoggingMethodCallback.java:67
* consumer method
* @throws IllegalArgumentException if the notification is null
*/
@Override
public void accept(LoggingMessageNotification notification) {
if (notification == null) {
throw new IllegalArgumentException("Notification must not be null");
}
try {
// Build arguments for the method call
Object[] args = this.buildArgs(this.method, null, notification);
// Invoke the method
this.method.setAccessible(true);
this.method.invoke(this.bean, args);
}
catch (Exception e) {
throw new McpLoggingConsumerMethodException(
"Error invoking logging consumer method: " + this.method.getName(), e);
}
}
/**
* Validates that the method return type is compatible with the logging consumer
* callback.
* @param method The method to validate
* @throws IllegalArgumentException if the return type is not compatible
*/
@Override
protected void validateReturnType(Method method) {
Class<?> returnType = method.getReturnType();
if (returnType != void.class) {
throw new IllegalArgumentException("Method must have void return type: " + method.getName() + " in "
+ method.getDeclaringClass().getName() + " returns " + returnType.getName());
}View on GitHub (pinned to 98a7beda4f)
Solutions
- Inspect the cause chain (getCause()) to find the real exception from the handler method.
- Fix the exception inside the annotated consumer method; wrap its risky operations in try-catch if logging must never fail.
- Verify the handler's parameter types match exactly what the callback builds (LoggingMessageNotification).
- Add logging around the handler body during development to localize the failing statement.
Example fix
// before
public void onLog(LoggingMessageNotification n) { store(n.data()); } // NPE when data() is null
// after
public void onLog(LoggingMessageNotification n) {
try {
if (n != null && n.data() != null) { store(n.data()); }
} catch (Exception e) {
logger.warn("logging handler failed", e);
}
} Defensive patterns
Strategy: try-catch
Validate before calling
// verify the handler signature matches before registration
Method m = bean.getClass().getMethod("onLog", LoggingMessageNotification.class); Try / catch
try {
callback.accept(notification);
} catch (McpLoggingConsumerMethodException e) {
logger.error("logging consumer " + e.getMessage() + " failed", e.getCause());
} Prevention
- Always log e.getCause(), not just the wrapper message
- Make handler bodies defensive against null notification fields
- Keep side effects in handlers failure-tolerant (try-catch inside the handler)
When it happens
Trigger: The user's @McpLogging method throws any exception (NPE, business error) at runtime; buildArgs supplied arguments incompatible with the method signature causing IllegalArgumentException from Method.invoke; the method is not accessible on the bean.
Common situations: Handler assumes non-null fields on the notification that are absent; handler signature expects a different type than LoggingMessageNotification so reflection binding fails; a database/logging backend used inside the handler is down.
Understand the failure class
Background: "API request failed": what wrapped HTTP errors from external APIs mean and how to find the real cause — this error's family across 29 libraries.
Related errors
- Error invoking resource list changed consumer method: " + th
- Error invoking complete method:
- Error invoking tool list changed consumer method:
- Method must not be null
- Error invoking complete method:
AI-assisted analysis of spring-projects/spring-ai@98a7beda4f (2026-09-11).
Data as JSON: /api/errors/85502a5ea22f1554.
Report an issue: GitHub.