35 public static class Tool {
36 @JsonProperty(
"function")
37 private ToolSpec toolSpec;
39 @Builder.Default
private String type =
"function";
42 @JsonIgnore @Builder.Default
private boolean isMCPTool =
false;
43 @JsonIgnore
private String mcpServerName;
44 @JsonIgnore
private ServerParameters mcpServerParameters;
51 public static class ToolSpec {
53 private String description;
54 private Parameters parameters;
60 public static class Parameters {
61 private Map<String, Property> properties;
62 private List<String> required =
new ArrayList<>();
64 public static Parameters of(Map<String, Property> properties) {
65 Parameters params =
new Parameters();
66 params.setProperties(properties);
68 if (properties !=
null) {
69 for (Map.Entry<String, Property> entry : properties.entrySet()) {
70 if (entry.getValue() !=
null && entry.getValue().isRequired()) {
71 params.getRequired().add(entry.getKey());
79 public String toString() {
81 com.fasterxml.jackson.databind.json.JsonMapper.builder()
84 node.put(
"type",
"object");
85 if (properties !=
null) {
86 ObjectNode propsNode = node.putObject(
"properties");
87 for (Map.Entry<String, Property> entry : properties.entrySet()) {
88 ObjectNode propNode = propsNode.putObject(entry.getKey());
89 Property prop = entry.getValue();
90 propNode.put(
"type", prop.getType());
91 propNode.put(
"description", prop.getDescription());
92 if (prop.getEnumValues() !=
null) {
93 propNode.putArray(
"enum")
95 prop.getEnumValues().stream()
97 com.fasterxml.jackson.databind.node.TextNode
99 .collect(java.util.stream.Collectors.toList()));
103 if (required !=
null && !required.isEmpty()) {
104 node.putArray(
"required")
107 .map(com.fasterxml.jackson.databind.node.TextNode::new)
108 .collect(java.util.stream.Collectors.toList()));
110 return node.toPrettyString();
118 public static class Property {
120 private String description;
122 @JsonProperty(
"enum")
123 @JsonInclude(JsonInclude.Include.NON_NULL)
124 private List<String> enumValues;
126 @JsonIgnore
private boolean required;
129 public static List<Tool>
fromJSONFile(String filePath, Map<String, ToolFunction> functionMap) {
131 ObjectMapper mapper =
new ObjectMapper();
132 List<Map<String, Object>> rawTools =
135 new com.fasterxml.jackson.core.type.TypeReference<>() {});
137 List<Tool> tools =
new ArrayList<>();
139 for (Map<String, Object> rawTool : rawTools) {
140 String json = mapper.writeValueAsString(rawTool);
141 Tool tool = mapper.readValue(json, Tool.class);
142 String toolName = tool.getToolSpec().getName();
143 for (Map.Entry<String,
ToolFunction> toolFunctionEntry : functionMap.entrySet()) {
144 if (toolFunctionEntry.getKey().equals(toolName)) {
145 tool.setToolFunction(toolFunctionEntry.getValue());
151 }
catch (Exception e) {
152 throw new RuntimeException(
"Failed to load tools from file: " + filePath, e);
156 public static List<Tool>
fromYAMLFile(String filePath, Map<String, ToolFunction> functionMap) {
158 ObjectMapper mapper =
new ObjectMapper(
new YAMLFactory());
159 List<Map<String, Object>> rawTools =
160 mapper.readValue(
new File(filePath),
new TypeReference<>() {});
161 List<Tool> tools =
new ArrayList<>();
162 for (Map<String, Object> rawTool : rawTools) {
163 String yaml = mapper.writeValueAsString(rawTool);
164 Tool tool = mapper.readValue(yaml, Tool.class);
165 String toolName = tool.getToolSpec().getName();
167 if (
function !=
null) {
168 tool.setToolFunction(
function);
173 }
catch (Exception e) {
174 throw new RuntimeException(
"Failed to load tools from YAML file: " + filePath, e);