spring-projects/spring-ai · error · IllegalArgumentException
Unsupported exchange type: {exchangeClassName|null} for meth
Error message
Unsupported exchange type: {exchangeClassName|null} for method: {methodName} in {className} What it means
The final fall-through in assignExchangeType: the method's declared exchange parameter type matches none of the supported types' dispatch paths, and the runtime exchange object is not an instance the library recognizes. It throws IllegalArgumentException naming the actual exchange class (or null) plus the method and class.
Source
Thrown at mcp/mcp-annotations/src/main/java/org/springframework/ai/mcp/annotation/method/resource/SyncMcpResourceMethodCallback.java:93
return syncServerExchange.transportContext();
}
else if (exchange instanceof McpAsyncServerExchange asyncServerExchange) {
throw new IllegalArgumentException("Unsupported Async exchange type: "
+ asyncServerExchange.getClass().getName() + " for Sync method: " + method.getName() + " in "
+ method.getDeclaringClass().getName());
}
}
else if (McpSyncServerExchange.class.isAssignableFrom(paramType)) {
if (exchange instanceof McpSyncServerExchange syncServerExchange) {
return syncServerExchange;
}
throw new IllegalArgumentException(
"Unsupported exchange type: " + (exchange != null ? exchange.getClass().getName() : "null")
+ " for Sync method: " + method.getName() + " in " + method.getDeclaringClass().getName());
}
throw new IllegalArgumentException(
"Unsupported exchange type: " + (exchange != null ? exchange.getClass().getName() : "null")
+ " for method: " + method.getName() + " in " + method.getDeclaringClass().getName());
}
/**
* Apply the callback to the given exchange and request.
* <p>
* This method extracts URI variable values from the request URI, builds the arguments
* for the method call, invokes the method, and converts the result to a
* ReadResourceResult.
* @param exchange The server exchange, may be null if the method doesn't require it
* @param request The resource request, must not be null
* @return The resource result
* @throws McpError if there is an error invoking the resource method
* @throws IllegalArgumentException if the request is null or if URI variable
* extraction fails
*/
@OverrideView on GitHub (pinned to 98a7beda4f)
Solutions
- Change the parameter to one of the supported types: McpSyncServerExchange or McpTransportContext.
- Check the library version's documented supported parameter types for resource methods and align the signature.
- Remove the exchange parameter if the handler does not need transport/exchange metadata.
Example fix
// before
@McpResource(uri = "cfg://{key}")
public String get(MyCustomExchange ex, String key) { ... }
// after
@McpResource(uri = "cfg://{key}")
public String get(McpSyncServerExchange ex, String key) { ... } Defensive patterns
Strategy: type-guard
Validate before calling
void checkExchangeParamType(Class<?> paramType) {
boolean ok = McpSyncServerExchange.class.isAssignableFrom(paramType)
|| McpTransportContext.class.isAssignableFrom(paramType)
|| paramType == Object.class /* exchange as plain arg */;
if (!ok) throw new IllegalArgumentException("Unsupported exchange parameter: " + paramType);
} Type guard
static boolean isSupportedExchangeParam(Class<?> p) {
return McpSyncServerExchange.class.isAssignableFrom(p) || McpTransportContext.class.isAssignableFrom(p);
} Try / catch
try {
registerResource(method, callback);
} catch (IllegalArgumentException e) {
if (e.getMessage().startsWith("Unsupported exchange type")) {
log.error("Use McpSyncServerExchange or McpTransportContext for the exchange parameter", e);
} throw e;
} Prevention
- Stick to documented parameter types (McpSyncServerExchange, McpTransportContext) for resource methods.
- Re-check signatures after upgrading spring-ai MCP versions, as supported parameter sets evolve.
- Avoid custom exchange wrapper classes in handler signatures.
When it happens
Trigger: A sync resource method's parameter type is neither McpTransportContext-assignable nor McpSyncServerExchange-assignable (e.g., a custom exchange type or unrelated class), so validation passed by inheritance accident but assignExchangeType has no branch to handle the runtime value.
Common situations: Custom wrapper types around the exchange, or a signature edited to accept an unsupported type after an upgrade changed the supported parameter set.
Understand the failure class
Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.
Related errors
- Unsupported exchange type: {exchange != null ? exchange.getC
- Unsupported Async exchange type: {exchangeClassName} for Syn
- Unsupported exchange type: {exchangeClassName|null} for Sync
- Unsupported Async exchange type: {exchangeClassName} for Syn
- Single parameter must be of type ElicitRequest:
AI-assisted analysis of spring-projects/spring-ai@98a7beda4f (2026-09-11).
Data as JSON: /api/errors/da4b04026bcf9192.
Report an issue: GitHub.