|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Oracle and/or its affiliates. |
| 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 | + * http://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 io.helidon.extensions.mcp.codegen; |
| 17 | + |
| 18 | +import java.util.List; |
| 19 | +import java.util.Optional; |
| 20 | +import java.util.regex.Pattern; |
| 21 | +import java.util.stream.Collectors; |
| 22 | + |
| 23 | +import io.helidon.codegen.classmodel.ClassModel; |
| 24 | +import io.helidon.codegen.classmodel.Method; |
| 25 | +import io.helidon.codegen.classmodel.Parameter; |
| 26 | +import io.helidon.common.types.AccessModifier; |
| 27 | +import io.helidon.common.types.Annotated; |
| 28 | +import io.helidon.common.types.Annotation; |
| 29 | +import io.helidon.common.types.ResolvedType; |
| 30 | +import io.helidon.common.types.TypeInfo; |
| 31 | +import io.helidon.common.types.TypeName; |
| 32 | +import io.helidon.common.types.TypeNames; |
| 33 | +import io.helidon.common.types.TypedElementInfo; |
| 34 | + |
| 35 | +import static io.helidon.common.types.TypeNames.LIST; |
| 36 | +import static io.helidon.extensions.mcp.codegen.McpTypes.MCP_CANCELLATION; |
| 37 | +import static io.helidon.extensions.mcp.codegen.McpTypes.MCP_DESCRIPTION; |
| 38 | +import static io.helidon.extensions.mcp.codegen.McpTypes.MCP_FEATURES; |
| 39 | +import static io.helidon.extensions.mcp.codegen.McpTypes.MCP_LOGGER; |
| 40 | +import static io.helidon.extensions.mcp.codegen.McpTypes.MCP_PARAMETERS; |
| 41 | +import static io.helidon.extensions.mcp.codegen.McpTypes.MCP_PROGRESS; |
| 42 | +import static io.helidon.extensions.mcp.codegen.McpTypes.MCP_REQUEST; |
| 43 | +import static io.helidon.extensions.mcp.codegen.McpTypes.MCP_ROOTS; |
| 44 | +import static io.helidon.extensions.mcp.codegen.McpTypes.MCP_SAMPLING; |
| 45 | + |
| 46 | +/** |
| 47 | + * Utility class for methods used by several MCP code generator. |
| 48 | + */ |
| 49 | +class McpCodegenUtil { |
| 50 | + private static final Pattern PATTERN = Pattern.compile("^."); |
| 51 | + |
| 52 | + static final List<String> MCP_TYPES = List.of(MCP_REQUEST.classNameWithEnclosingNames(), |
| 53 | + MCP_FEATURES.classNameWithEnclosingNames(), |
| 54 | + MCP_LOGGER.classNameWithEnclosingNames(), |
| 55 | + MCP_PROGRESS.classNameWithEnclosingNames(), |
| 56 | + MCP_CANCELLATION.classNameWithEnclosingNames(), |
| 57 | + MCP_SAMPLING.classNameWithEnclosingNames(), |
| 58 | + MCP_PARAMETERS.classNameWithEnclosingNames()); |
| 59 | + |
| 60 | + private McpCodegenUtil() { |
| 61 | + } |
| 62 | + |
| 63 | + static boolean isBoolean(TypeName type) { |
| 64 | + return TypeNames.PRIMITIVE_BOOLEAN.equals(type) |
| 65 | + || TypeNames.BOXED_BOOLEAN.equals(type); |
| 66 | + } |
| 67 | + |
| 68 | + static boolean isNumber(TypeName type) { |
| 69 | + return TypeNames.BOXED_INT.equals(type) |
| 70 | + || TypeNames.BOXED_BYTE.equals(type) |
| 71 | + || TypeNames.BOXED_LONG.equals(type) |
| 72 | + || TypeNames.BOXED_FLOAT.equals(type) |
| 73 | + || TypeNames.BOXED_SHORT.equals(type) |
| 74 | + || TypeNames.BOXED_DOUBLE.equals(type) |
| 75 | + || TypeNames.PRIMITIVE_INT.equals(type) |
| 76 | + || TypeNames.PRIMITIVE_BYTE.equals(type) |
| 77 | + || TypeNames.PRIMITIVE_LONG.equals(type) |
| 78 | + || TypeNames.PRIMITIVE_FLOAT.equals(type) |
| 79 | + || TypeNames.PRIMITIVE_SHORT.equals(type) |
| 80 | + || TypeNames.PRIMITIVE_DOUBLE.equals(type); |
| 81 | + } |
| 82 | + |
| 83 | + static boolean isList(TypeName type) { |
| 84 | + return type.equals(TypeNames.LIST) && type.typeArguments().size() == 1; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Create a class name from the provided element and suffix. |
| 89 | + * The first character is change to upper case. |
| 90 | + * |
| 91 | + * @param element name as prefix |
| 92 | + * @param suffix the suffix |
| 93 | + * @return class name as TypeName |
| 94 | + */ |
| 95 | + static TypeName createClassName(TypedElementInfo element, String suffix) { |
| 96 | + String uppercaseElement = PATTERN.matcher(element.elementName()).replaceFirst(m -> m.group().toUpperCase()); |
| 97 | + return TypeName.builder() |
| 98 | + .className(uppercaseElement + suffix) |
| 99 | + .build(); |
| 100 | + } |
| 101 | + |
| 102 | + static List<TypedElementInfo> getElementsWithAnnotation(TypeInfo type, TypeName target) { |
| 103 | + return type.elementInfo().stream() |
| 104 | + .filter(element -> element.hasAnnotation(target)) |
| 105 | + .collect(Collectors.toList()); |
| 106 | + } |
| 107 | + |
| 108 | + static TypeName generatedTypeName(TypeName factoryTypeName, String suffix) { |
| 109 | + return TypeName.builder() |
| 110 | + .packageName(factoryTypeName.packageName()) |
| 111 | + .className(factoryTypeName.classNameWithEnclosingNames().replace('.', '_') + "__" + suffix) |
| 112 | + .build(); |
| 113 | + } |
| 114 | + |
| 115 | + static boolean isIgnoredSchemaElement(TypeName typeName) { |
| 116 | + return MCP_REQUEST.equals(typeName) |
| 117 | + || MCP_ROOTS.equals(typeName) |
| 118 | + || MCP_LOGGER.equals(typeName) |
| 119 | + || MCP_FEATURES.equals(typeName) |
| 120 | + || MCP_PROGRESS.equals(typeName) |
| 121 | + || MCP_SAMPLING.equals(typeName) |
| 122 | + || MCP_CANCELLATION.equals(typeName); |
| 123 | + } |
| 124 | + |
| 125 | + static boolean isResourceTemplate(String uri) { |
| 126 | + return uri.contains("{") || uri.contains("}"); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Add a new method to the generated class that convert a {@code List<McpParameters>} to |
| 131 | + * a list of the provided type. |
| 132 | + * |
| 133 | + * @param classModel generated class |
| 134 | + * @param type generic type |
| 135 | + */ |
| 136 | + static void addToListMethod(ClassModel.Builder classModel, TypeName type) { |
| 137 | + TypeName typeList = ResolvedType.create(TypeName.builder(LIST) |
| 138 | + .addTypeArgument(type) |
| 139 | + .build()).type(); |
| 140 | + TypeName parameterList = ResolvedType.create(TypeName.builder(LIST) |
| 141 | + .addTypeArgument(MCP_PARAMETERS) |
| 142 | + .build()).type(); |
| 143 | + Method.Builder method = Method.builder() |
| 144 | + .name("toList") |
| 145 | + .isStatic(true) |
| 146 | + .accessModifier(AccessModifier.PRIVATE) |
| 147 | + .returnType(typeList) |
| 148 | + .addParameter(Parameter.builder().name("list").type(parameterList).build()) |
| 149 | + .addContentLine("return list == null ? List.of()") |
| 150 | + .increaseContentPadding() |
| 151 | + .addContentLine(": list.stream().map(p -> p.as(" + type + ".class))") |
| 152 | + .increaseContentPadding() |
| 153 | + .addContentLine(".map(p -> p.get()).toList();"); |
| 154 | + classModel.addMethod(method.build()); |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * Returns {@code true} if the provided type is an MCP type and create request getter for that type, |
| 159 | + * otherwise nothing is created and return {@code false}. |
| 160 | + * |
| 161 | + * @param parameters list of parameters |
| 162 | + * @param type the tested type |
| 163 | + * @return {@code true} if an MCP type, {@code false} otherwise. |
| 164 | + */ |
| 165 | + static boolean isMcpType(List<String> parameters, TypedElementInfo type) { |
| 166 | + if (MCP_REQUEST.equals(type.typeName())) { |
| 167 | + parameters.add("request"); |
| 168 | + return true; |
| 169 | + } |
| 170 | + if (MCP_FEATURES.equals(type.typeName())) { |
| 171 | + parameters.add("request.features()"); |
| 172 | + return true; |
| 173 | + } |
| 174 | + if (MCP_LOGGER.equals(type.typeName())) { |
| 175 | + parameters.add("request.features().logger()"); |
| 176 | + return true; |
| 177 | + } |
| 178 | + if (MCP_PROGRESS.equals(type.typeName())) { |
| 179 | + parameters.add("request.features().progress()"); |
| 180 | + return true; |
| 181 | + } |
| 182 | + if (MCP_CANCELLATION.equals(type.typeName())) { |
| 183 | + parameters.add("request.features().cancellation()"); |
| 184 | + return true; |
| 185 | + } |
| 186 | + if (MCP_SAMPLING.equals(type.typeName())) { |
| 187 | + parameters.add("request.features().sampling()"); |
| 188 | + return true; |
| 189 | + } |
| 190 | + if (MCP_ROOTS.equals(type.typeName())) { |
| 191 | + parameters.add("request.features().roots()"); |
| 192 | + return true; |
| 193 | + } |
| 194 | + if (MCP_PARAMETERS.equals(type.typeName())) { |
| 195 | + parameters.add("request.parameters()"); |
| 196 | + return true; |
| 197 | + } |
| 198 | + return false; |
| 199 | + } |
| 200 | + |
| 201 | + static Optional<String> getDescription(Annotated element) { |
| 202 | + if (element.hasAnnotation(MCP_DESCRIPTION)) { |
| 203 | + Annotation description = element.annotation(MCP_DESCRIPTION); |
| 204 | + return description.stringValue(); |
| 205 | + } |
| 206 | + return Optional.empty(); |
| 207 | + } |
| 208 | +} |
0 commit comments