|
| 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 | +import java.util.Map; |
| 21 | +import java.util.concurrent.ConcurrentHashMap; |
| 22 | +import java.util.stream.Stream; |
| 23 | + |
| 24 | +import io.modelcontextprotocol.client.McpClient.AsyncSpec; |
| 25 | +import org.slf4j.Logger; |
| 26 | +import org.slf4j.LoggerFactory; |
| 27 | +import org.springaicommunity.mcp.method.changed.prompt.AsyncPromptListChangedSpecification; |
| 28 | +import org.springaicommunity.mcp.method.changed.resource.AsyncResourceListChangedSpecification; |
| 29 | +import org.springaicommunity.mcp.method.changed.tool.AsyncToolListChangedSpecification; |
| 30 | +import org.springaicommunity.mcp.method.elicitation.AsyncElicitationSpecification; |
| 31 | +import org.springaicommunity.mcp.method.logging.AsyncLoggingSpecification; |
| 32 | +import org.springaicommunity.mcp.method.progress.AsyncProgressSpecification; |
| 33 | +import org.springaicommunity.mcp.method.sampling.AsyncSamplingSpecification; |
| 34 | + |
| 35 | +import org.springframework.ai.mcp.customizer.McpAsyncClientCustomizer; |
| 36 | +import org.springframework.util.CollectionUtils; |
| 37 | + |
| 38 | +/** |
| 39 | + * @author Christian Tzolov |
| 40 | + */ |
| 41 | +public class McpAsyncAnnotationCustomizer implements McpAsyncClientCustomizer { |
| 42 | + |
| 43 | + private static final Logger logger = LoggerFactory.getLogger(McpAsyncAnnotationCustomizer.class); |
| 44 | + |
| 45 | + private final List<AsyncSamplingSpecification> asyncSamplingSpecifications; |
| 46 | + |
| 47 | + private final List<AsyncLoggingSpecification> asyncLoggingSpecifications; |
| 48 | + |
| 49 | + private final List<AsyncElicitationSpecification> asyncElicitationSpecifications; |
| 50 | + |
| 51 | + private final List<AsyncProgressSpecification> asyncProgressSpecifications; |
| 52 | + |
| 53 | + private final List<AsyncToolListChangedSpecification> asyncToolListChangedSpecifications; |
| 54 | + |
| 55 | + private final List<AsyncResourceListChangedSpecification> asyncResourceListChangedSpecifications; |
| 56 | + |
| 57 | + private final List<AsyncPromptListChangedSpecification> asyncPromptListChangedSpecifications; |
| 58 | + |
| 59 | + // Tracking registered specifications per client |
| 60 | + private final Map<String, Boolean> clientElicitationSpecs = new ConcurrentHashMap<>(); |
| 61 | + |
| 62 | + private final Map<String, Boolean> clientSamplingSpecs = new ConcurrentHashMap<>(); |
| 63 | + |
| 64 | + public McpAsyncAnnotationCustomizer(List<AsyncSamplingSpecification> asyncSamplingSpecifications, |
| 65 | + List<AsyncLoggingSpecification> asyncLoggingSpecifications, |
| 66 | + List<AsyncElicitationSpecification> asyncElicitationSpecifications, |
| 67 | + List<AsyncProgressSpecification> asyncProgressSpecifications, |
| 68 | + List<AsyncToolListChangedSpecification> asyncToolListChangedSpecifications, |
| 69 | + List<AsyncResourceListChangedSpecification> asyncResourceListChangedSpecifications, |
| 70 | + List<AsyncPromptListChangedSpecification> asyncPromptListChangedSpecifications) { |
| 71 | + |
| 72 | + this.asyncSamplingSpecifications = asyncSamplingSpecifications; |
| 73 | + this.asyncLoggingSpecifications = asyncLoggingSpecifications; |
| 74 | + this.asyncElicitationSpecifications = asyncElicitationSpecifications; |
| 75 | + this.asyncProgressSpecifications = asyncProgressSpecifications; |
| 76 | + this.asyncToolListChangedSpecifications = asyncToolListChangedSpecifications; |
| 77 | + this.asyncResourceListChangedSpecifications = asyncResourceListChangedSpecifications; |
| 78 | + this.asyncPromptListChangedSpecifications = asyncPromptListChangedSpecifications; |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public void customize(String name, AsyncSpec clientSpec) { |
| 83 | + |
| 84 | + if (!CollectionUtils.isEmpty(this.asyncElicitationSpecifications)) { |
| 85 | + this.asyncElicitationSpecifications.forEach(elicitationSpec -> { |
| 86 | + Stream.of(elicitationSpec.clients()).forEach(clientId -> { |
| 87 | + if (clientId.equalsIgnoreCase(name)) { |
| 88 | + |
| 89 | + // Check if client already has an elicitation spec |
| 90 | + if (this.clientElicitationSpecs.containsKey(name)) { |
| 91 | + throw new IllegalArgumentException("Client '" + name |
| 92 | + + "' already has an elicitationSpec registered. Only one elicitationSpec is allowed per client."); |
| 93 | + } |
| 94 | + |
| 95 | + this.clientElicitationSpecs.put(name, Boolean.TRUE); |
| 96 | + clientSpec.elicitation(elicitationSpec.elicitationHandler()); |
| 97 | + |
| 98 | + logger.info("Registered elicitationSpec for client '{}'.", name); |
| 99 | + |
| 100 | + } |
| 101 | + }); |
| 102 | + }); |
| 103 | + } |
| 104 | + |
| 105 | + if (!CollectionUtils.isEmpty(this.asyncSamplingSpecifications)) { |
| 106 | + this.asyncSamplingSpecifications.forEach(samplingSpec -> { |
| 107 | + Stream.of(samplingSpec.clients()).forEach(clientId -> { |
| 108 | + if (clientId.equalsIgnoreCase(name)) { |
| 109 | + |
| 110 | + // Check if client already has a sampling spec |
| 111 | + if (this.clientSamplingSpecs.containsKey(name)) { |
| 112 | + throw new IllegalArgumentException("Client '" + name |
| 113 | + + "' already has a samplingSpec registered. Only one samplingSpec is allowed per client."); |
| 114 | + } |
| 115 | + this.clientSamplingSpecs.put(name, Boolean.TRUE); |
| 116 | + |
| 117 | + clientSpec.sampling(samplingSpec.samplingHandler()); |
| 118 | + |
| 119 | + logger.info("Registered samplingSpec for client '{}'.", name); |
| 120 | + } |
| 121 | + }); |
| 122 | + }); |
| 123 | + } |
| 124 | + |
| 125 | + if (!CollectionUtils.isEmpty(this.asyncLoggingSpecifications)) { |
| 126 | + this.asyncLoggingSpecifications.forEach(loggingSpec -> { |
| 127 | + Stream.of(loggingSpec.clients()).forEach(clientId -> { |
| 128 | + if (clientId.equalsIgnoreCase(name)) { |
| 129 | + clientSpec.loggingConsumer(loggingSpec.loggingHandler()); |
| 130 | + logger.info("Registered loggingSpec for client '{}'.", name); |
| 131 | + } |
| 132 | + }); |
| 133 | + }); |
| 134 | + } |
| 135 | + |
| 136 | + if (!CollectionUtils.isEmpty(this.asyncProgressSpecifications)) { |
| 137 | + this.asyncProgressSpecifications.forEach(progressSpec -> { |
| 138 | + Stream.of(progressSpec.clients()).forEach(clientId -> { |
| 139 | + if (clientId.equalsIgnoreCase(name)) { |
| 140 | + clientSpec.progressConsumer(progressSpec.progressHandler()); |
| 141 | + logger.info("Registered progressSpec for client '{}'.", name); |
| 142 | + } |
| 143 | + }); |
| 144 | + }); |
| 145 | + } |
| 146 | + |
| 147 | + if (!CollectionUtils.isEmpty(this.asyncToolListChangedSpecifications)) { |
| 148 | + this.asyncToolListChangedSpecifications.forEach(toolListChangedSpec -> { |
| 149 | + Stream.of(toolListChangedSpec.clients()).forEach(clientId -> { |
| 150 | + if (clientId.equalsIgnoreCase(name)) { |
| 151 | + clientSpec.toolsChangeConsumer(toolListChangedSpec.toolListChangeHandler()); |
| 152 | + logger.info("Registered toolListChangedSpec for client '{}'.", name); |
| 153 | + } |
| 154 | + }); |
| 155 | + }); |
| 156 | + } |
| 157 | + |
| 158 | + if (!CollectionUtils.isEmpty(this.asyncResourceListChangedSpecifications)) { |
| 159 | + this.asyncResourceListChangedSpecifications.forEach(resourceListChangedSpec -> { |
| 160 | + Stream.of(resourceListChangedSpec.clients()).forEach(clientId -> { |
| 161 | + if (clientId.equalsIgnoreCase(name)) { |
| 162 | + clientSpec.resourcesChangeConsumer(resourceListChangedSpec.resourceListChangeHandler()); |
| 163 | + logger.info("Registered resourceListChangedSpec for client '{}'.", name); |
| 164 | + } |
| 165 | + }); |
| 166 | + }); |
| 167 | + } |
| 168 | + |
| 169 | + if (!CollectionUtils.isEmpty(this.asyncPromptListChangedSpecifications)) { |
| 170 | + this.asyncPromptListChangedSpecifications.forEach(promptListChangedSpec -> { |
| 171 | + Stream.of(promptListChangedSpec.clients()).forEach(clientId -> { |
| 172 | + if (clientId.equalsIgnoreCase(name)) { |
| 173 | + clientSpec.promptsChangeConsumer(promptListChangedSpec.promptListChangeHandler()); |
| 174 | + logger.info("Registered promptListChangedSpec for client '{}'.", name); |
| 175 | + } |
| 176 | + }); |
| 177 | + }); |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | +} |
0 commit comments