|
| 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; |
| 18 | + |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +import org.springaicommunity.mcp.method.elicitation.AsyncElicitationSpecification; |
| 22 | +import org.springaicommunity.mcp.method.logging.AsyncLoggingSpecification; |
| 23 | +import org.springaicommunity.mcp.method.progress.AsyncProgressSpecification; |
| 24 | +import org.springaicommunity.mcp.method.sampling.AsyncSamplingSpecification; |
| 25 | +import org.springframework.ai.mcp.customizer.McpAsyncClientCustomizer; |
| 26 | +import org.springframework.util.CollectionUtils; |
| 27 | +import org.springframework.util.StringUtils; |
| 28 | + |
| 29 | +import io.modelcontextprotocol.client.McpClient.AsyncSpec; |
| 30 | + |
| 31 | +/** |
| 32 | + * @author Christian Tzolov |
| 33 | + */ |
| 34 | +public class McpAsyncAnnotationCustomizer implements McpAsyncClientCustomizer { |
| 35 | + |
| 36 | + private final List<AsyncSamplingSpecification> asyncSamplingSpecifications; |
| 37 | + |
| 38 | + private final List<AsyncLoggingSpecification> asyncLoggingSpecifications; |
| 39 | + |
| 40 | + private final List<AsyncElicitationSpecification> asyncElicitationSpecifications; |
| 41 | + |
| 42 | + private final List<AsyncProgressSpecification> asyncProgressSpecifications; |
| 43 | + |
| 44 | + public McpAsyncAnnotationCustomizer(List<AsyncSamplingSpecification> asyncSamplingSpecifications, |
| 45 | + List<AsyncLoggingSpecification> asyncLoggingSpecifications, |
| 46 | + List<AsyncElicitationSpecification> asyncElicitationSpecifications, |
| 47 | + List<AsyncProgressSpecification> asyncProgressSpecifications) { |
| 48 | + |
| 49 | + this.asyncSamplingSpecifications = asyncSamplingSpecifications; |
| 50 | + this.asyncLoggingSpecifications = asyncLoggingSpecifications; |
| 51 | + this.asyncElicitationSpecifications = asyncElicitationSpecifications; |
| 52 | + this.asyncProgressSpecifications = asyncProgressSpecifications; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public void customize(String name, AsyncSpec clientSpec) { |
| 57 | + |
| 58 | + if (!CollectionUtils.isEmpty(asyncElicitationSpecifications)) { |
| 59 | + this.asyncElicitationSpecifications.forEach(elicitationSpec -> { |
| 60 | + if (!StringUtils.hasText(elicitationSpec.clientId()) || elicitationSpec.clientId().equals(name)) { |
| 61 | + clientSpec.elicitation(elicitationSpec.elicitationHandler()); |
| 62 | + } |
| 63 | + }); |
| 64 | + } |
| 65 | + |
| 66 | + if (!CollectionUtils.isEmpty(asyncSamplingSpecifications)) { |
| 67 | + this.asyncSamplingSpecifications.forEach(samplingSpec -> { |
| 68 | + if (!StringUtils.hasText(samplingSpec.clientId()) || samplingSpec.clientId().equals(name)) { |
| 69 | + clientSpec.sampling(samplingSpec.samplingHandler()); |
| 70 | + } |
| 71 | + }); |
| 72 | + } |
| 73 | + |
| 74 | + if (!CollectionUtils.isEmpty(asyncLoggingSpecifications)) { |
| 75 | + this.asyncLoggingSpecifications.forEach(loggingSpec -> { |
| 76 | + if (!StringUtils.hasText(loggingSpec.clientId()) || loggingSpec.clientId().equals(name)) { |
| 77 | + clientSpec.loggingConsumer(loggingSpec.loggingHandler()); |
| 78 | + } |
| 79 | + }); |
| 80 | + } |
| 81 | + |
| 82 | + if (!CollectionUtils.isEmpty(asyncProgressSpecifications)) { |
| 83 | + this.asyncProgressSpecifications.forEach(progressSpec -> { |
| 84 | + if (!StringUtils.hasText(progressSpec.clientId()) || progressSpec.clientId().equals(name)) { |
| 85 | + clientSpec.progressConsumer(progressSpec.progressHandler()); |
| 86 | + } |
| 87 | + }); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | +} |
0 commit comments