spring-ai icon indicating copy to clipboard operation
spring-ai copied to clipboard

GH-4542: Collect all Flux elements in async MCP tool callbacks

Open suryateja-g13 opened this issue 6 months ago • 2 comments

Summary

  • Fixes AbstractAsyncMcpToolMethodCallback.convertToCallToolResult() which was calling .next() on Flux return types, silently discarding all elements except the first
  • When an async MCP tool method returns Flux<T>, all emitted elements are now collected and returned as separate CallToolResult content entries
  • This affects both AsyncMcpToolMethodCallback (stateful) and AsyncStatelessMcpToolMethodCallback (stateless) because both extend AbstractAsyncMcpToolMethodCallback
  • Updated existing tests that asserted the broken one-element behavior to verify all elements are returned

Before: Flux.just("a", "b", "c") → CallToolResult with 1 content item ("a") After: Flux.just("a", "b", "c") → CallToolResult with 3 content items ("a", "b", "c")

Fixes: #4542

Test plan

  • [ ] ./mvnw -pl mcp/mcp-annotations test — all 1349 tests pass
  • [ ] testMultipleFluxTool now asserts 3 content items instead of 1
  • [ ] testGetToolSpecificationsWithFluxHandling now asserts 3 content items in both AsyncMcpToolProviderTests and AsyncStatelessMcpToolProviderTests
  • [ ] testToolWithFluxReturnType in AsyncMcpToolProviderTests now asserts all 3 items

suryateja-g13 avatar May 26 '26 02:05 suryateja-g13

The failing "MCP transport integration tests" check is unrelated to this PR's changes. The failures are in WebClientStreamableHttpTransportIT.testCloseInitialized and testCloseUninitialized within the mcp/mcp-transport-webflux module — a module this PR does not modify.

This PR only changes files in mcp/mcp-annotations. The same "MCP transport integration tests" workflow passed successfully on main a few hours ago (run 26394342907), so this appears to be a pre-existing intermittent failure in the transport layer.

suryateja-g13 avatar May 26 '26 02:05 suryateja-g13

Thank you for taking the time to migrate and build on this — really appreciate it!

suryateja-g13 avatar May 27 '26 01:05 suryateja-g13