fix: Complete JsonSchemaGenerator with required properties for MCP compliance

This commit is contained in:
jade
2026-07-20 17:07:39 +09:00
parent 80ff392b5a
commit b55f863de9
2 changed files with 69 additions and 66 deletions

View File

@@ -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);

View File

@@ -1,22 +1,10 @@
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
* @className JsonSchemaGenerator
* @description AX HUB 시스템 처리 클래스
* @author 김형식
* @create 2026.09.01
* <pre>
* ---------- 개정이력 ----------
* 수정일 수정자 수정내용
* ---------- -------- ---------------------------
* 2026.09.01 김형식 최초생성
*
* </pre>
*/
import io.shinhanlife.dap.mcc.annotation.McpParameter; import io.shinhanlife.dap.mcc.annotation.McpParameter;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@@ -24,10 +12,14 @@ import java.util.Map;
public class JsonSchemaGenerator { public class JsonSchemaGenerator {
/** /**
* Java DTO 클래스를 분석하여 MCP 규격의 JSON Schema (Properties)를 생성합니다. * Java DTO 클래스를 분석하여 MCP 규격의 완전한 JSON Schema를 생성합니다.
*/ */
public static Map<String, Object> generatePropertiesSchema(Class<?> clazz) { public static Map<String, Object> generateSchema(Class<?> clazz) {
Map<String, Object> schema = new HashMap<>();
schema.put("type", "object");
Map<String, Object> properties = new HashMap<>(); Map<String, Object> properties = new HashMap<>();
List<String> requiredList = new ArrayList<>();
for (Field field : clazz.getDeclaredFields()) { for (Field field : clazz.getDeclaredFields()) {
Map<String, Object> fieldSchema = new HashMap<>(); Map<String, Object> fieldSchema = new HashMap<>();
@@ -37,16 +29,30 @@ public class JsonSchemaGenerator {
// 2. 어노테이션 기반 설명 추출 // 2. 어노테이션 기반 설명 추출
McpParameter paramAnnotation = field.getAnnotation(McpParameter.class); McpParameter paramAnnotation = field.getAnnotation(McpParameter.class);
JsonPropertyDescription descAnnotation = field.getAnnotation(JsonPropertyDescription.class);
if (paramAnnotation != null && !paramAnnotation.description().isEmpty()) { if (paramAnnotation != null && !paramAnnotation.description().isEmpty()) {
fieldSchema.put("description", paramAnnotation.description()); fieldSchema.put("description", paramAnnotation.description());
} else if (descAnnotation != null && !descAnnotation.value().isEmpty()) {
fieldSchema.put("description", descAnnotation.value());
} else { } else {
fieldSchema.put("description", field.getName()); // 기본값 fieldSchema.put("description", field.getName()); // 기본값
} }
// 3. 필수 여부 판단
JsonProperty jsonProp = field.getAnnotation(JsonProperty.class);
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()) {
schema.put("required", requiredList);
}
return schema;
} }
private static String mapJavaTypeToJsonType(Class<?> clazz) { private static String mapJavaTypeToJsonType(Class<?> clazz) {