spring-projects/spring-ai · error · IllegalArgumentException
Unsupported exchange type: {exchangeClassName|null} for Sync
Error message
Unsupported exchange type: {exchangeClassName|null} for Sync method: {methodName} in {className} What it means
When the sync resource method declares a McpSyncServerExchange parameter, assignExchangeType requires the runtime exchange argument to be exactly an McpSyncServerExchange. If it is absent (null) or any other type (async exchange, transport context, arbitrary object), the library cannot supply the sync exchange and throws IllegalArgumentException.
Source
Thrown at mcp/mcp-annotations/src/main/java/org/springframework/ai/mcp/annotation/method/resource/SyncMcpResourceMethodCallback.java:88
if (McpTransportContext.class.isAssignableFrom(paramType)) {
if (exchange instanceof McpTransportContext transportContext) {
return transportContext;
}
else if (exchange instanceof McpSyncServerExchange syncServerExchange) {
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 resultView on GitHub (pinned to 98a7beda4f)
Solutions
- Pass a non-null McpSyncServerExchange when applying the callback (e.g., in tests, build one from the sync server session).
- Register the handler with the correct (sync) server so the framework supplies the matching exchange.
- If the method does not need an exchange, drop the McpSyncServerExchange parameter from the signature.
Example fix
// before (test)
ReadResourceResult r = callback.apply(null, new ReadResourceRequest(new McpResourceUri("res://x")));
// after
McpSyncServerExchange exchange = ...; // from sync server session
ReadResourceResult r = callback.apply(exchange, new ReadResourceRequest(new McpResourceUri("res://x"))); Defensive patterns
Strategy: validation
Validate before calling
void requireSyncExchange(Object exchange) {
if (!(exchange instanceof McpSyncServerExchange))
throw new IllegalArgumentException("Expected McpSyncServerExchange, got: " + exchange);
} Type guard
static boolean hasSyncExchange(Object exchange) {
return exchange instanceof McpSyncServerExchange;
} Try / catch
try {
return callback.apply(exchange, request);
} catch (IllegalArgumentException e) {
if (e.getMessage().startsWith("Unsupported exchange type")) {
log.error("Resource method requires McpSyncServerExchange; supplied exchange was wrong or null", e);
} throw e;
} Prevention
- In tests, obtain a real McpSyncServerExchange from the sync server session instead of passing null.
- Do not remove exchange parameters when a method does not need them — prefer omitting them from the signature.
- Cover every resource method with a through-the-server integration test.
When it happens
Trigger: Invoking the sync resource method callback with exchange=null while the method signature demands McpSyncServerExchange, or with an McpAsyncServerExchange/McpTransportContext instead of a sync exchange.
Common situations: Calling the callback directly from tests with the wrong exchange object, or a framework wiring bug where the async dispatcher routes a request to a sync-registered handler.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- Unsupported exchange type: {exchange != null ? exchange.getC
- Unsupported Async exchange type: {exchangeClassName} for Syn
- Unsupported exchange type: {exchangeClassName|null} for meth
- Request must not be null
- Unsupported Async exchange type: {exchangeClassName} for Syn
AI-assisted analysis of spring-projects/spring-ai@98a7beda4f (2026-09-11).
Data as JSON: /api/errors/eb68bbd34cb18d13.
Report an issue: GitHub.