|
| 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.client.common.autoconfigure.annotations; |
| 18 | + |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +import io.modelcontextprotocol.spec.McpSchema.ProgressNotification; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | +import org.springaicommunity.mcp.annotation.McpProgress; |
| 24 | +import org.springaicommunity.mcp.method.progress.SyncProgressSpecification; |
| 25 | +import org.springframework.ai.mcp.client.common.autoconfigure.annotations.McpClientAnnotationScannerAutoConfiguration.ClientMcpAnnotatedBeans; |
| 26 | +import org.springframework.boot.autoconfigure.AutoConfigurations; |
| 27 | +import org.springframework.boot.test.context.runner.ApplicationContextRunner; |
| 28 | +import org.springframework.context.annotation.ComponentScan; |
| 29 | +import org.springframework.context.annotation.Configuration; |
| 30 | +import org.springframework.context.annotation.Lazy; |
| 31 | +import org.springframework.stereotype.Component; |
| 32 | + |
| 33 | +import static org.assertj.core.api.Assertions.assertThat; |
| 34 | + |
| 35 | +/** |
| 36 | + * Reproduction test for ordering bug where McpClientSpecificationFactoryAutoConfiguration |
| 37 | + * is created before any @Component beans with @McpProgress (or other MCP annotations) are |
| 38 | + * instantiated, resulting in empty specification lists. |
| 39 | + */ |
| 40 | +class McpClientSpecOrderingReproTests { |
| 41 | + |
| 42 | + private final ApplicationContextRunner runner = new ApplicationContextRunner() |
| 43 | + .withConfiguration(AutoConfigurations.of(McpClientAnnotationScannerAutoConfiguration.class, |
| 44 | + McpClientSpecificationFactoryAutoConfiguration.class)) |
| 45 | + .withUserConfiguration(ScanConfig.class); |
| 46 | + |
| 47 | + @Configuration |
| 48 | + @ComponentScan(basePackageClasses = ScannedClientHandlers.class) |
| 49 | + static class ScanConfig { |
| 50 | + |
| 51 | + } |
| 52 | + |
| 53 | + @Component |
| 54 | + @Lazy |
| 55 | + static class ScannedClientHandlers { |
| 56 | + |
| 57 | + @McpProgress(clients = "server1") |
| 58 | + public void onProgress(ProgressNotification pn) { |
| 59 | + } |
| 60 | + |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + void progressSpecsIncludeScannedComponent_evenWhenCreatedAfterSpecsBean() { |
| 65 | + runner.run(ctx -> { |
| 66 | + // 1) Trigger spec list bean creation early |
| 67 | + @SuppressWarnings("unchecked") |
| 68 | + List<SyncProgressSpecification> specs = (List<SyncProgressSpecification>) ctx.getBean("progressSpecs"); |
| 69 | + |
| 70 | + // 2) Now force creation of the scanned @Component (post-processor runs here) |
| 71 | + ctx.getBean(ScannedClientHandlers.class); |
| 72 | + |
| 73 | + // 3) Registry sees the component… |
| 74 | + ClientMcpAnnotatedBeans registry = ctx.getBean(ClientMcpAnnotatedBeans.class); |
| 75 | + assertThat(registry.getBeansByAnnotation(McpProgress.class)).hasSize(1); |
| 76 | + |
| 77 | + // 4) Expected behavior: specs reflect newly-registered handler |
| 78 | + // Under the bug, this assertion fails (list stays empty) |
| 79 | + assertThat(specs).hasSize(1); |
| 80 | + }); |
| 81 | + } |
| 82 | + |
| 83 | +} |
0 commit comments