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
	 */
	@Override

View on GitHub (pinned to 98a7beda4f)

Solutions

  1. Change the parameter to one of the supported types: McpSyncServerExchange or McpTransportContext.
  2. Check the library version's documented supported parameter types for resource methods and align the signature.
  3. 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

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


AI-assisted analysis of spring-projects/spring-ai@98a7beda4f (2026-09-11). Data as JSON: /api/errors/da4b04026bcf9192. Report an issue: GitHub.