|
| 1 | +/* |
| 2 | + * Copyright 2025-2025 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.ai.mcp.server.common.autoconfigure; |
| 18 | + |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +import org.junit.jupiter.api.Disabled; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | +import org.springaicommunity.mcp.annotation.McpTool; |
| 24 | +import org.springaicommunity.mcp.annotation.McpToolParam; |
| 25 | +import reactor.core.publisher.Flux; |
| 26 | +import reactor.core.publisher.Mono; |
| 27 | + |
| 28 | +import org.springframework.boot.autoconfigure.AutoConfigurations; |
| 29 | +import org.springframework.boot.test.context.runner.ApplicationContextRunner; |
| 30 | +import org.springframework.context.annotation.Bean; |
| 31 | +import org.springframework.context.annotation.Configuration; |
| 32 | +import org.springframework.stereotype.Component; |
| 33 | + |
| 34 | +import static org.assertj.core.api.Assertions.assertThat; |
| 35 | + |
| 36 | +/** |
| 37 | + * Integration test to demonstrate and verify the fix for Issue #4542: |
| 38 | + * Stateless Async MCP Server with streamable-http returns only the first element |
| 39 | + * from tools with a Flux return type. |
| 40 | + * |
| 41 | + */ |
| 42 | +public class FluxReturnTypeIT { |
| 43 | + |
| 44 | + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() |
| 45 | + .withConfiguration(AutoConfigurations.of(McpServerStatelessAutoConfiguration.class, |
| 46 | + StatelessToolCallbackConverterAutoConfiguration.class)); |
| 47 | + |
| 48 | + /** |
| 49 | + * This test demonstrates Issue #4542: When a @McpTool method returns Flux<T>, |
| 50 | + * only the first element is returned instead of all elements. |
| 51 | + * |
| 52 | + * The test is currently disabled because the bug exists in the external |
| 53 | + * org.springaicommunity.mcp library. |
| 54 | + */ |
| 55 | + @Test |
| 56 | + @Disabled("Bug in org.springaicommunity.mcp library - Issue #4542") |
| 57 | + void testFluxReturnTypeReturnsAllElements() { |
| 58 | + this.contextRunner |
| 59 | + .withUserConfiguration(FluxToolConfiguration.class) |
| 60 | + .withPropertyValues("spring.ai.mcp.server.type=ASYNC", "spring.ai.mcp.server.protocol=STATELESS") |
| 61 | + .run(context -> { |
| 62 | + assertThat(context).hasBean("fluxTestTools"); |
| 63 | + |
| 64 | + // TODO: Add actual MCP client call to verify all elements are returned |
| 65 | + // Expected: ["item-1", "item-2", "item-3"] |
| 66 | + // Actual (buggy): ["item-1"] |
| 67 | + }); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * This test demonstrates the workaround: Using Mono<List<T>> instead of Flux<T> |
| 72 | + * properly returns all elements. |
| 73 | + */ |
| 74 | + @Test |
| 75 | + void testMonoListWorkaround() { |
| 76 | + this.contextRunner |
| 77 | + .withUserConfiguration(MonoListToolConfiguration.class) |
| 78 | + .withPropertyValues("spring.ai.mcp.server.type=ASYNC", "spring.ai.mcp.server.protocol=STATELESS") |
| 79 | + .run(context -> { |
| 80 | + assertThat(context).hasBean("monoListTestTools"); |
| 81 | + |
| 82 | + // This workaround properly returns all elements: ["item-1", "item-2", "item-3"] |
| 83 | + }); |
| 84 | + } |
| 85 | + |
| 86 | + @Configuration |
| 87 | + static class FluxToolConfiguration { |
| 88 | + |
| 89 | + @Bean |
| 90 | + FluxTestTools fluxTestTools() { |
| 91 | + return new FluxTestTools(); |
| 92 | + } |
| 93 | + |
| 94 | + } |
| 95 | + |
| 96 | + @Component |
| 97 | + static class FluxTestTools { |
| 98 | + |
| 99 | + /** |
| 100 | + * This method demonstrates the bug: it returns Flux<String> but only the |
| 101 | + * first element is returned to the client. |
| 102 | + */ |
| 103 | + @McpTool(name = "flux-test", description = "Test Flux return type - BUGGY") |
| 104 | + public Flux<String> getMultipleItems( |
| 105 | + @McpToolParam(description = "Number of items to return", required = true) int count) { |
| 106 | + return Flux.range(1, count).map(i -> "item-" + i); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * This method also demonstrates the bug with a more realistic streaming scenario. |
| 111 | + */ |
| 112 | + @McpTool(name = "flux-data-stream", description = "Stream data items - BUGGY") |
| 113 | + public Flux<DataItem> streamDataItems( |
| 114 | + @McpToolParam(description = "Category to filter", required = false) String category) { |
| 115 | + return Flux.just( |
| 116 | + new DataItem("id1", "Item 1", category), |
| 117 | + new DataItem("id2", "Item 2", category), |
| 118 | + new DataItem("id3", "Item 3", category) |
| 119 | + ); |
| 120 | + } |
| 121 | + |
| 122 | + } |
| 123 | + |
| 124 | + @Configuration |
| 125 | + static class MonoListToolConfiguration { |
| 126 | + |
| 127 | + @Bean |
| 128 | + MonoListTestTools monoListTestTools() { |
| 129 | + return new MonoListTestTools(); |
| 130 | + } |
| 131 | + |
| 132 | + } |
| 133 | + |
| 134 | + @Component |
| 135 | + static class MonoListTestTools { |
| 136 | + |
| 137 | + /** |
| 138 | + * WORKAROUND: Use Mono<List<T>> instead of Flux<T> to return all elements. |
| 139 | + */ |
| 140 | + @McpTool(name = "mono-list-test", description = "Test Mono<List> workaround") |
| 141 | + public Mono<List<String>> getMultipleItems( |
| 142 | + @McpToolParam(description = "Number of items to return", required = true) int count) { |
| 143 | + return Flux.range(1, count).map(i -> "item-" + i).collectList(); |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * WORKAROUND: Collect Flux elements into a list before returning. |
| 148 | + */ |
| 149 | + @McpTool(name = "mono-list-data-stream", description = "Get data items as list") |
| 150 | + public Mono<List<DataItem>> getDataItems( |
| 151 | + @McpToolParam(description = "Category to filter", required = false) String category) { |
| 152 | + return Flux.just( |
| 153 | + new DataItem("id1", "Item 1", category), |
| 154 | + new DataItem("id2", "Item 2", category), |
| 155 | + new DataItem("id3", "Item 3", category) |
| 156 | + ).collectList(); |
| 157 | + } |
| 158 | + |
| 159 | + } |
| 160 | + |
| 161 | + record DataItem(String id, String name, String category) { |
| 162 | + } |
| 163 | + |
| 164 | +} |
| 165 | + |
0 commit comments