fix: Complete JsonSchemaGenerator with required properties for MCP compliance
This commit is contained in:
@@ -120,10 +120,7 @@ public class ToolRegistryHeartbeatSender {
|
|||||||
if (method.getParameterCount() > 0) {
|
if (method.getParameterCount() > 0) {
|
||||||
try {
|
try {
|
||||||
Class<?> paramType = method.getParameterTypes()[0];
|
Class<?> paramType = method.getParameterTypes()[0];
|
||||||
Map<String, Object> schema = JsonSchemaGenerator.generatePropertiesSchema(paramType);
|
Map<String, Object> finalSchema = JsonSchemaGenerator.generateSchema(paramType);
|
||||||
Map<String, Object> finalSchema = new HashMap<>();
|
|
||||||
finalSchema.put("type", "object");
|
|
||||||
finalSchema.put("properties", schema);
|
|
||||||
meta.setParametersSchema(finalSchema);
|
meta.setParametersSchema(finalSchema);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("Failed to generate schema for {}", subToolName, e);
|
log.error("Failed to generate schema for {}", subToolName, e);
|
||||||
|
|||||||
@@ -1,62 +1,68 @@
|
|||||||
package io.shinhanlife.dap.mcc.util;
|
package io.shinhanlife.dap.mcc.util;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
/**
|
import com.fasterxml.jackson.annotation.JsonPropertyDescription;
|
||||||
* @package io.shinhanlife.dap.mcc.util
|
import io.shinhanlife.dap.mcc.annotation.McpParameter;
|
||||||
* @className JsonSchemaGenerator
|
import java.lang.reflect.Field;
|
||||||
* @description AX HUB 시스템 처리 클래스
|
import java.util.ArrayList;
|
||||||
* @author 김형식
|
import java.util.HashMap;
|
||||||
* @create 2026.09.01
|
import java.util.List;
|
||||||
* <pre>
|
import java.util.Map;
|
||||||
* ---------- 개정이력 ----------
|
|
||||||
* 수정일 수정자 수정내용
|
public class JsonSchemaGenerator {
|
||||||
* ---------- -------- ---------------------------
|
|
||||||
* 2026.09.01 김형식 최초생성
|
/**
|
||||||
*
|
* Java DTO 클래스를 분석하여 MCP 규격의 완전한 JSON Schema를 생성합니다.
|
||||||
* </pre>
|
*/
|
||||||
*/
|
public static Map<String, Object> generateSchema(Class<?> clazz) {
|
||||||
import io.shinhanlife.dap.mcc.annotation.McpParameter;
|
Map<String, Object> schema = new HashMap<>();
|
||||||
import java.lang.reflect.Field;
|
schema.put("type", "object");
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
Map<String, Object> properties = new HashMap<>();
|
||||||
import java.util.Map;
|
List<String> requiredList = new ArrayList<>();
|
||||||
|
|
||||||
public class JsonSchemaGenerator {
|
for (Field field : clazz.getDeclaredFields()) {
|
||||||
|
Map<String, Object> fieldSchema = new HashMap<>();
|
||||||
/**
|
|
||||||
* Java DTO 클래스를 분석하여 MCP 규격의 JSON Schema (Properties)를 생성합니다.
|
// 1. 타입 매핑
|
||||||
*/
|
fieldSchema.put("type", mapJavaTypeToJsonType(field.getType()));
|
||||||
public static Map<String, Object> generatePropertiesSchema(Class<?> clazz) {
|
|
||||||
Map<String, Object> properties = new HashMap<>();
|
// 2. 어노테이션 기반 설명 추출
|
||||||
|
McpParameter paramAnnotation = field.getAnnotation(McpParameter.class);
|
||||||
for (Field field : clazz.getDeclaredFields()) {
|
JsonPropertyDescription descAnnotation = field.getAnnotation(JsonPropertyDescription.class);
|
||||||
Map<String, Object> fieldSchema = new HashMap<>();
|
if (paramAnnotation != null && !paramAnnotation.description().isEmpty()) {
|
||||||
|
fieldSchema.put("description", paramAnnotation.description());
|
||||||
// 1. 타입 매핑
|
} else if (descAnnotation != null && !descAnnotation.value().isEmpty()) {
|
||||||
fieldSchema.put("type", mapJavaTypeToJsonType(field.getType()));
|
fieldSchema.put("description", descAnnotation.value());
|
||||||
|
} else {
|
||||||
// 2. 어노테이션 기반 설명 추출
|
fieldSchema.put("description", field.getName()); // 기본값
|
||||||
McpParameter paramAnnotation = field.getAnnotation(McpParameter.class);
|
}
|
||||||
if (paramAnnotation != null && !paramAnnotation.description().isEmpty()) {
|
|
||||||
fieldSchema.put("description", paramAnnotation.description());
|
// 3. 필수 여부 판단
|
||||||
} else {
|
JsonProperty jsonProp = field.getAnnotation(JsonProperty.class);
|
||||||
fieldSchema.put("description", field.getName()); // 기본값
|
if ((jsonProp != null && jsonProp.required()) || (paramAnnotation != null && paramAnnotation.required())) {
|
||||||
}
|
requiredList.add(field.getName());
|
||||||
|
}
|
||||||
properties.put(field.getName(), fieldSchema);
|
|
||||||
}
|
properties.put(field.getName(), fieldSchema);
|
||||||
|
}
|
||||||
return properties;
|
|
||||||
}
|
schema.put("properties", properties);
|
||||||
|
if (!requiredList.isEmpty()) {
|
||||||
private static String mapJavaTypeToJsonType(Class<?> clazz) {
|
schema.put("required", requiredList);
|
||||||
if (clazz == String.class) return "string";
|
}
|
||||||
if (clazz == Integer.class || clazz == int.class) return "integer";
|
|
||||||
if (clazz == Long.class || clazz == long.class) return "integer";
|
return schema;
|
||||||
if (clazz == Double.class || clazz == double.class) return "number";
|
}
|
||||||
if (clazz == Float.class || clazz == float.class) return "number";
|
|
||||||
if (clazz == Boolean.class || clazz == boolean.class) return "boolean";
|
private static String mapJavaTypeToJsonType(Class<?> clazz) {
|
||||||
if (List.class.isAssignableFrom(clazz)) return "array";
|
if (clazz == String.class) return "string";
|
||||||
return "object";
|
if (clazz == Integer.class || clazz == int.class) return "integer";
|
||||||
}
|
if (clazz == Long.class || clazz == long.class) return "integer";
|
||||||
}
|
if (clazz == Double.class || clazz == double.class) return "number";
|
||||||
|
if (clazz == Float.class || clazz == float.class) return "number";
|
||||||
|
if (clazz == Boolean.class || clazz == boolean.class) return "boolean";
|
||||||
|
if (List.class.isAssignableFrom(clazz)) return "array";
|
||||||
|
return "object";
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user