spring-projects/spring-ai · error · IllegalArgumentException

Unsupported exchange type: ${exchangeType} for method: ${met

Error message

Unsupported exchange type: ${exchangeType} for method: ${method} in ${declaringClass}

What it means

The final fall-through throw in AsyncMcpResourceMethodCallback.assignExchangeType: the method's exchange parameter is not McpTransportContext, McpSyncServerExchange, or McpAsyncServerExchange, so the callback cannot map the runtime exchange to it at all. Only these three exchange types are supported for async resource methods.

Source

Thrown at mcp/mcp-annotations/src/main/java/org/springframework/ai/mcp/annotation/method/resource/AsyncMcpResourceMethodCallback.java:97

						+ syncServerExchange.getClass().getName() + " for Async method: " + method.getName() + " in "
						+ method.getDeclaringClass().getName());

			}
			else if (exchange instanceof McpAsyncServerExchange asyncServerExchange) {
				return asyncServerExchange.transportContext();
			}
		}
		else if (McpAsyncServerExchange.class.isAssignableFrom(paramType)) {
			if (exchange instanceof McpAsyncServerExchange asyncServerExchange) {
				return asyncServerExchange;
			}

			throw new IllegalArgumentException(
					"Unsupported exchange type: " + (exchange != null ? exchange.getClass().getName() : "null")
							+ " for Async 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 A Mono that emits 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 exchange parameter to one of the supported types: McpTransportContext, McpAsyncServerExchange, or McpSyncServerExchange.
  2. Pass extra context via the request object or method-level fields, not via the exchange parameter slot.
  3. Review the MCP annotations documentation for the exact list of injectable parameter types.

Example fix

// before
@McpResource(uri = "x://{id}")
public ReadResourceResult read(MyCustomContext ctx, String id) { ... }

// after
@McpResource(uri = "x://{id}")
public ReadResourceResult read(McpTransportContext ctx, String id) { ... }
Defensive patterns

Strategy: validation

Validate before calling

Set<Class<?>> ALLOWED = Set.of(McpTransportContext.class, McpAsyncServerExchange.class, McpSyncServerExchange.class);
Arrays.stream(method.getParameterTypes())
    .filter(ALLOWED::contains)
    .skip(1) // more than one exchange-like param is suspicious
    .findAny()
    .ifPresent(p -> { throw new IllegalStateException("Multiple/unsupported exchange params: " + p); });

Type guard

static boolean isSupportedExchangeType(Class<?> p) {
    return McpTransportContext.class == p || McpAsyncServerExchange.class == p || McpSyncServerExchange.class == p;
}

Try / catch

try {
    asyncMcpServer.annotate(provider);
} catch (IllegalArgumentException e) {
    throw new IllegalStateException("Fix exchange parameter type in " + provider.getClass(), e);
}

Prevention

When it happens

Trigger: Declaring an @McpResource method with an unsupported first/exchange parameter type (e.g., a custom wrapper, String, HttpServletRequest) and then invoking the async callback.

Common situations: Developers assuming arbitrary context objects can be injected into resource methods; refactoring that replaced McpTransportContext with a custom context class.

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/afd53ae7eb463902. Report an issue: GitHub.