Skip to content

Fix NPE and add support for generic return types in AsyncMcpToolProvider#5920

Open
luoshengming wants to merge 1 commit intospring-projects:mainfrom
luoshengming:fix/mcp-generic-type-support
Open

Fix NPE and add support for generic return types in AsyncMcpToolProvider#5920
luoshengming wants to merge 1 commit intospring-projects:mainfrom
luoshengming:fix/mcp-generic-type-support

Conversation

@luoshengming
Copy link
Copy Markdown

This PR fixes a bug in AsyncMcpToolProvider where using generic return types (e.g., Mono<ApiResponse<String>>) would cause an IllegalArgumentException due to passing null to ClassUtils.isPrimitiveOrWrapper().

Problem

When a tool method returns a parameterized type like Mono<ApiResponse<T>>, the typeArgument extracted from the return type is a ParameterizedType, not a Class<?>. The original code would set methodReturnType to null and then pass it to ClassUtils.isPrimitiveOrWrapper(), which throws IllegalArgumentException: Class must not be null.

Solution

  • Handle Class<?> and ParameterizedType separately using pattern matching
  • For ParameterizedType, use McpJsonSchemaGenerator.generateFromType() instead of generateFromClass()
  • This aligns with how SyncMcpToolProvider handles generic return types

Testing

Added test case testGetToolSpecificationsWithGenericReturnType() that verifies generic return types are properly handled and output schema is correctly generated.

Impact

  • ✅ Fixes a real bug affecting users with generic return types
  • ✅ Backward compatible - no breaking changes
  • ✅ Aligns async implementation with sync implementation

- Fix potential IllegalArgumentException when typeArgument is ParameterizedType

- Add support for ParameterizedType (e.g., Mono<ApiResponse<String>>)

- Use generateFromType() for parameterized types to preserve generic information

- Add test case to verify generic return type schema generation

This fix aligns AsyncMcpToolProvider with SyncMcpToolProvider's approach of using generateFromType() for handling generic return types.

Signed-off-by: luoshengming <luoshengming@hotmail.com>
@luoshengming luoshengming force-pushed the fix/mcp-generic-type-support branch from a2eb659 to 5737534 Compare May 1, 2026 12:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant