|
| 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 | +package org.springframework.ai.mcp; |
| 17 | + |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.HashSet; |
| 21 | +import java.util.Set; |
| 22 | +import java.util.stream.Collectors; |
| 23 | + |
| 24 | +import io.modelcontextprotocol.spec.McpSchema; |
| 25 | + |
| 26 | +import org.springframework.aot.hint.MemberCategory; |
| 27 | +import org.springframework.aot.hint.RuntimeHints; |
| 28 | +import org.springframework.aot.hint.RuntimeHintsRegistrar; |
| 29 | +import org.springframework.aot.hint.TypeReference; |
| 30 | + |
| 31 | +/** |
| 32 | + * @author Josh Long |
| 33 | + * @since 1.0.0 |
| 34 | + */ |
| 35 | +@SuppressWarnings("unused") |
| 36 | +public class McpHints implements RuntimeHintsRegistrar { |
| 37 | + |
| 38 | + @Override |
| 39 | + public void registerHints(RuntimeHints hints, ClassLoader classLoader) { |
| 40 | + var mcs = MemberCategory.values(); |
| 41 | + |
| 42 | + for (var tr : innerClasses(McpSchema.class)) { |
| 43 | + hints.reflection().registerType(tr, mcs); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + private Set<TypeReference> innerClasses(Class<?> clzz) { |
| 48 | + var indent = new HashSet<String>(); |
| 49 | + this.findNestedClasses(clzz, indent); |
| 50 | + return indent.stream().map(TypeReference::of).collect(Collectors.toSet()); |
| 51 | + } |
| 52 | + |
| 53 | + private void findNestedClasses(Class<?> clazz, Set<String> indent) { |
| 54 | + var classes = new ArrayList<Class<?>>(); |
| 55 | + classes.addAll(Arrays.asList(clazz.getDeclaredClasses())); |
| 56 | + classes.addAll(Arrays.asList(clazz.getClasses())); |
| 57 | + for (var nestedClass : classes) { |
| 58 | + this.findNestedClasses(nestedClass, indent); |
| 59 | + } |
| 60 | + indent.addAll(classes.stream().map(Class::getName).toList()); |
| 61 | + } |
| 62 | + |
| 63 | +} |
0 commit comments