|
| 1 | +/* |
| 2 | +======================================================================== |
| 3 | +SchemaCrawler |
| 4 | +http://www.schemacrawler.com |
| 5 | +Copyright (c) 2000-2025, Sualeh Fatehi <sualeh@hotmail.com>. |
| 6 | +All rights reserved. |
| 7 | +------------------------------------------------------------------------ |
| 8 | +
|
| 9 | +SchemaCrawler is distributed in the hope that it will be useful, but |
| 10 | +WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 | +
|
| 13 | +SchemaCrawler and the accompanying materials are made available under |
| 14 | +the terms of the Eclipse Public License v1.0, GNU General Public License |
| 15 | +v3 or GNU Lesser General Public License v3. |
| 16 | +
|
| 17 | +You may elect to redistribute this code under any of these licenses. |
| 18 | +
|
| 19 | +The Eclipse Public License is available at: |
| 20 | +http://www.eclipse.org/legal/epl-v10.html |
| 21 | +
|
| 22 | +The GNU General Public License v3 and the GNU Lesser General Public |
| 23 | +License v3 are available at: |
| 24 | +http://www.gnu.org/licenses/ |
| 25 | +
|
| 26 | +======================================================================== |
| 27 | +*/ |
| 28 | + |
| 29 | +package schemacrawler.tools.command.aichat.mcp; |
| 30 | + |
| 31 | +import com.fasterxml.jackson.databind.JsonNode; |
| 32 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 33 | +import com.fasterxml.jackson.databind.node.ArrayNode; |
| 34 | +import com.fasterxml.jackson.databind.node.ObjectNode; |
| 35 | +import com.fasterxml.jackson.module.jsonSchema.JsonSchema; |
| 36 | +import com.fasterxml.jackson.module.jsonSchema.JsonSchemaGenerator; |
| 37 | +import com.github.victools.jsonschema.generator.SchemaVersion; |
| 38 | +import org.springframework.ai.chat.model.ToolContext; |
| 39 | +import org.springframework.ai.tool.ToolCallback; |
| 40 | +import org.springframework.ai.tool.definition.ToolDefinition; |
| 41 | +import org.springframework.ai.util.json.JsonParser; |
| 42 | +import org.springframework.lang.Nullable; |
| 43 | +import schemacrawler.tools.command.aichat.FunctionDefinition; |
| 44 | +import schemacrawler.tools.command.aichat.FunctionDefinition.FunctionType; |
| 45 | +import schemacrawler.tools.command.aichat.functions.FunctionDefinitionRegistry; |
| 46 | +import us.fatehi.utility.UtilityMarker; |
| 47 | + |
| 48 | +import java.util.*; |
| 49 | +import java.util.Map.Entry; |
| 50 | +import java.util.logging.Level; |
| 51 | +import java.util.logging.Logger; |
| 52 | + |
| 53 | +@UtilityMarker |
| 54 | +public final class SpringAIUtility { |
| 55 | + |
| 56 | + private static final Logger LOGGER = Logger.getLogger(SpringAIUtility.class.getCanonicalName()); |
| 57 | + |
| 58 | + private SpringAIUtility() { |
| 59 | + // Prevent instantiation |
| 60 | + } |
| 61 | + |
| 62 | + public static List<ToolCallback> toolCallbacks(final List<ToolDefinition> tools) { |
| 63 | + Objects.requireNonNull(tools, "Tools must not be null"); |
| 64 | + final List<ToolCallback> toolCallbacks = new ArrayList<>(); |
| 65 | + for (final ToolDefinition toolDefinition : tools) { |
| 66 | + toolCallbacks.add(new SpringAIToolCallback(toolDefinition)); |
| 67 | + } |
| 68 | + return toolCallbacks; |
| 69 | + } |
| 70 | + |
| 71 | + public static List<ToolDefinition> tools() { |
| 72 | + |
| 73 | + final List<ToolDefinition> toolDefinitions = new ArrayList<>(); |
| 74 | + for (final FunctionDefinition<?> functionDefinition : |
| 75 | + FunctionDefinitionRegistry.getFunctionDefinitionRegistry().getFunctionDefinitions()) { |
| 76 | + if (functionDefinition.getFunctionType() != FunctionType.USER) { |
| 77 | + continue; |
| 78 | + } |
| 79 | + |
| 80 | + try { |
| 81 | + final ToolDefinition toolDefinition = |
| 82 | + ToolDefinition.builder() |
| 83 | + .name(functionDefinition.getName()) |
| 84 | + .description(functionDefinition.getDescription()) |
| 85 | + .inputSchema(generateToolInput(functionDefinition.getParametersClass())) |
| 86 | + .build(); |
| 87 | + toolDefinitions.add(toolDefinition); |
| 88 | + } catch (final Exception e) { |
| 89 | + LOGGER.log( |
| 90 | + Level.WARNING, String.format("Could not load <%s>", functionDefinition.getName()), e); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + return toolDefinitions; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * @see org.springframework.ai.util.json.schema.JsonSchemaGenerator |
| 99 | + */ |
| 100 | + private static String generateToolInput(final Class<?> parametersClass) throws Exception { |
| 101 | + Objects.requireNonNull(parametersClass, "Parameters must not be null"); |
| 102 | + |
| 103 | + final Map<String, JsonNode> parametersJsonSchema = jsonSchema(parametersClass); |
| 104 | + final ObjectNode schema = JsonParser.getObjectMapper().createObjectNode(); |
| 105 | + schema.put("$schema", SchemaVersion.DRAFT_2020_12.getIdentifier()); |
| 106 | + schema.put("type", "object"); |
| 107 | + |
| 108 | + final List<String> required = new ArrayList<>(); |
| 109 | + final ObjectNode properties = schema.putObject("properties"); |
| 110 | + for (final Entry<String, JsonNode> parameter : parametersJsonSchema.entrySet()) { |
| 111 | + final String parameterName = parameter.getKey(); |
| 112 | + final JsonNode parameterSchema = parameter.getValue(); |
| 113 | + if (parameterSchema.has("required") && parameterSchema.get("required").asBoolean()) { |
| 114 | + ((ObjectNode) parameterSchema).remove("required"); |
| 115 | + required.add(parameterName); |
| 116 | + } |
| 117 | + properties.set(parameterName, parameterSchema); |
| 118 | + } |
| 119 | + final ArrayNode requiredArray = schema.putArray("required"); |
| 120 | + required.forEach(requiredArray::add); |
| 121 | + |
| 122 | + schema.put("additionalProperties", false); |
| 123 | + |
| 124 | + return schema.toPrettyString(); |
| 125 | + } |
| 126 | + |
| 127 | + private static Map<String, JsonNode> jsonSchema(final Class<?> parametersClass) throws Exception { |
| 128 | + final ObjectMapper mapper = new ObjectMapper(); |
| 129 | + final JsonSchemaGenerator schemaGen = new JsonSchemaGenerator(mapper); |
| 130 | + final JsonSchema schema = schemaGen.generateSchema(parametersClass); |
| 131 | + final JsonNode schemaNode = mapper.valueToTree(schema); |
| 132 | + final JsonNode properties = schemaNode.get("properties"); |
| 133 | + final Set<Entry<String, JsonNode>> namedProperties; |
| 134 | + if (properties == null) { |
| 135 | + namedProperties = new HashSet<>(); |
| 136 | + } else { |
| 137 | + namedProperties = properties.properties(); |
| 138 | + } |
| 139 | + final Map<String, JsonNode> propertiesMap = new HashMap<>(); |
| 140 | + for (final Entry<String, JsonNode> entry : namedProperties) { |
| 141 | + propertiesMap.put(entry.getKey(), entry.getValue()); |
| 142 | + } |
| 143 | + return propertiesMap; |
| 144 | + } |
| 145 | + |
| 146 | + public record SpringAIToolCallback( |
| 147 | + ToolDefinition toolDefinition) implements ToolCallback { |
| 148 | + |
| 149 | + public SpringAIToolCallback { |
| 150 | + Objects.requireNonNull(toolDefinition, "Tool definition must not be null"); |
| 151 | + } |
| 152 | + |
| 153 | + @Override |
| 154 | + public ToolDefinition getToolDefinition() { |
| 155 | + return toolDefinition; |
| 156 | + } |
| 157 | + |
| 158 | + @Override |
| 159 | + public String call(final String toolInput) { |
| 160 | + final String callMessage = |
| 161 | + String.format( |
| 162 | + "Call to <%s>%n%s%nTool was successfully executed with no return value.", |
| 163 | + toolDefinition.name(), toolInput); |
| 164 | + System.out.println(callMessage); |
| 165 | + return callMessage; |
| 166 | + } |
| 167 | + |
| 168 | + @Override |
| 169 | + public String call(final String toolInput, @Nullable final ToolContext tooContext) { |
| 170 | + return call(toolInput); |
| 171 | + } |
| 172 | + } |
| 173 | +} |
0 commit comments