fix(llm-tools): 修复MCP工具inputSchema不规范导致客户端报错
- 增加 inputSchema 兜底校正,自动包装裸参数 Map 为完整 JSON Schema - 修复财务余额、采购到货、采购列表工具返回 schema 缺少 type/properties/required 的问题 - 降低 MCP tools/list 阶段客户端连接与序列化故障风险cp_1.0.0
parent
0eea95f20d
commit
7835efa964
|
|
@ -92,7 +92,8 @@ public class FinanceBalanceAggregateToolProvider extends AbstractMcpQueryToolPro
|
|||
properties.put("top_n", intProperty("SUMMARY 返回前 N 组(默认 " + DEFAULT_TOP_N + ",上限 " + MAX_TOP_N + ");仅 SUMMARY 可用;"
|
||||
+ "group_by=TIME_MONTH 时忽略(返回区间内全部月份)"));
|
||||
properties.put("include_total", boolProperty("是否统计总组数(默认 false);LIST 时受 count_cap=" + COUNT_CAP + " 限制"));
|
||||
return withPagingProperties(properties, McpPages.MAX_AGG_PAGE_SIZE, McpPages.DEFAULT_PAGE_SIZE);
|
||||
withPagingProperties(properties, McpPages.MAX_AGG_PAGE_SIZE, McpPages.DEFAULT_PAGE_SIZE);
|
||||
return objectSchema(properties);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -70,7 +70,8 @@ public class PurchaseArrivalAggregateToolProvider extends AbstractMcpQueryToolPr
|
|||
properties.put("time_range", stringProperty("时间区间,格式 start~end(yyyy-MM-dd)。基于 purchase_date;默认近 12 个月,跨度上限 36 个月"));
|
||||
properties.put("top_n", intProperty("SUMMARY 返回前 N 组(默认 10,上限 100);仅 SUMMARY 可用"));
|
||||
properties.put("include_total", boolProperty("是否统计总条数(默认 false);LIST 时受 count_cap=50000 限制"));
|
||||
return withPagingProperties(properties, McpPages.MAX_AGG_PAGE_SIZE, McpPages.DEFAULT_PAGE_SIZE);
|
||||
withPagingProperties(properties, McpPages.MAX_AGG_PAGE_SIZE, McpPages.DEFAULT_PAGE_SIZE);
|
||||
return objectSchema(properties);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -91,7 +91,8 @@ public class PurchaseListToolProvider extends AbstractMcpQueryToolProvider {
|
|||
properties.put("time_range", stringProperty("时间区间,格式 start~end(yyyy-MM-dd)。基于 purchase_date(HISTORY 基于 create_time);默认近 12 个月,跨度上限 36 个月"));
|
||||
properties.put("include_detail", boolProperty("是否返回明细行(默认 false)。ORDER 附采购明细,HISTORY 附历史明细"));
|
||||
properties.put("include_total", boolProperty("是否统计总条数(默认 false);true 时受 count_cap=50000 限制,超出置 total_count_capped=true"));
|
||||
return withPagingProperties(properties, McpPages.MAX_LIST_PAGE_SIZE, McpPages.DEFAULT_PAGE_SIZE);
|
||||
withPagingProperties(properties, McpPages.MAX_LIST_PAGE_SIZE, McpPages.DEFAULT_PAGE_SIZE);
|
||||
return objectSchema(properties, "entity");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -3,6 +3,10 @@ package com.ruoyi.sip.llm.tools.support;
|
|||
import com.ruoyi.sip.llm.McpTool;
|
||||
import com.ruoyi.sip.llm.tools.McpToolProvider;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
|
|
@ -10,16 +14,37 @@ import java.util.Map;
|
|||
|
||||
public abstract class AbstractMcpToolProvider implements McpToolProvider {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(AbstractMcpToolProvider.class);
|
||||
|
||||
@Override
|
||||
public McpTool buildTool() {
|
||||
McpTool tool = new McpTool();
|
||||
tool.setName(getToolName());
|
||||
tool.setDescription(getToolDescription());
|
||||
tool.setInputSchema(buildInputSchema());
|
||||
tool.setInputSchema(normalizeInputSchema(buildInputSchema()));
|
||||
tool.setHandler(this::handle);
|
||||
return tool;
|
||||
}
|
||||
|
||||
/**
|
||||
* 兜底校正 inputSchema:MCP 客户端要求 inputSchema 是完整的 JSON Schema 对象
|
||||
* ({@code {type:"object", properties:{...}, required:[...]}})。
|
||||
* <p>历史上个别工具误把 properties 片段直接返回,导致客户端拿到参数列表本身、且丢失 required,
|
||||
* 进而在 tools/list 阶段报错。这里检测到缺少 type/properties 的裸参数 Map 时自动包装并告警,
|
||||
* 避免同类遗漏再次表现为客户端连接/序列化故障。</p>
|
||||
*/
|
||||
private Map<String, Object> normalizeInputSchema(Map<String, Object> schema) {
|
||||
if (schema == null || schema.isEmpty()) {
|
||||
return objectSchema(new LinkedHashMap<String, Object>());
|
||||
}
|
||||
if (schema.containsKey("type") && schema.containsKey("properties")) {
|
||||
return schema;
|
||||
}
|
||||
log.warn("MCP 工具 {} 的 inputSchema 缺少 type/properties,已按 {type:object, properties:...} 兜底包装,"
|
||||
+ "请检查 buildInputSchema 是否遗漏 objectSchema() 调用", getToolName());
|
||||
return objectSchema(schema);
|
||||
}
|
||||
|
||||
protected abstract String getToolName();
|
||||
|
||||
protected abstract String getToolDescription();
|
||||
|
|
@ -34,7 +59,7 @@ public abstract class AbstractMcpToolProvider implements McpToolProvider {
|
|||
schema.put("properties", properties);
|
||||
schema.put("required", requiredFields == null || requiredFields.length == 0
|
||||
? Collections.emptyList()
|
||||
: java.util.Arrays.asList(requiredFields));
|
||||
: Arrays.asList(requiredFields));
|
||||
schema.put("additionalProperties", false);
|
||||
return schema;
|
||||
}
|
||||
|
|
@ -76,4 +101,4 @@ public abstract class AbstractMcpToolProvider implements McpToolProvider {
|
|||
protected boolean isBlank(String value) {
|
||||
return value == null || value.trim().isEmpty();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue