feat(iot): mcp code review

This commit is contained in:
haohao
2026-02-28 16:13:09 +08:00
parent 182be9adf6
commit 6a49fd40f8
4 changed files with 6 additions and 0 deletions

View File

@@ -5,6 +5,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* IoT MCP 模块配置(如控制类工具开关等)
* TODO @AI: 在应用启动时打印控制类工具开关状态enableControlTools=true 时输出 WARN 提醒)
*/
@ConfigurationProperties(prefix = "yudao.iot.mcp")
@Data

View File

@@ -65,6 +65,7 @@ public class McpApiKeyAuthenticationFilter extends OncePerRequestFilter {
filterChain.doFilter(wrappedRequest, response);
}
// TODO @AI: 增加详细审计日志IP、User-Agent、认证结果并评估使用 HashMap 缓存减少遍历时间差异
private McpSecurityProperties.ApiKeyItem findMatchingApiKey(String requestKey) {
byte[] requestKeyBytes = requestKey.getBytes(StandardCharsets.UTF_8);
for (McpSecurityProperties.ApiKeyItem item : properties.getApiKeys()) {

View File

@@ -77,6 +77,7 @@ public class IotDeviceMcpTool {
/**
* 设备元信息转 Map不含属性快照、不含 deviceSecret
* TODO @AI: 评估设备信息返回是否需要脱敏规则(如设备名含手机号/地址时),可配置脱敏策略
*/
private Map<String, Object> deviceToMap(IotDeviceDO d, String productName) {
Map<String, Object> m = new LinkedHashMap<>();
@@ -110,6 +111,7 @@ public class IotDeviceMcpTool {
PageResult<IotDeviceDO> page = getDeviceService().getDevicePage(req);
Set<Long> productIds = page.getList().stream().map(IotDeviceDO::getProductId).filter(java.util.Objects::nonNull).collect(Collectors.toSet());
Map<Long, IotProductDO> productMap = productIds.isEmpty() ? Map.of() : getProductService().getProductMap(productIds);
// TODO @AI: 当 productMap.get(productId) 为 null 时,记录告警日志(产品已删除但设备仍关联),便于数据一致性检查
List<Map<String, Object>> list = page.getList().stream()
.map(d -> deviceToMap(d, productMap.get(d.getProductId()) != null ? productMap.get(d.getProductId()).getName() : null))
.collect(Collectors.toList());
@@ -225,6 +227,7 @@ public class IotDeviceMcpTool {
if (device == null) {
return JsonUtils.toJsonString(Map.of("error", "device_not_found", "hint", "设备不存在"));
}
// TODO @AI: 增强时间解析逻辑,支持完整 ISO8601 格式(含时区),并限制最大查询时间范围(如 31 天)
// 1. 解析时间参数,失败时返回统一错误 JSON
LocalDateTime start;
LocalDateTime end;

View File

@@ -68,6 +68,7 @@ public class IotThingModelMcpTool {
/**
* 解析产品productId → productKey → productName。返回 null product 表示未传任何有效参数errorJson 非空时表示未找到或多条,调用方直接返回该 JSON。
* TODO @AI: 若后续新增产品查询类 MCP 工具,可将 resolveProduct 抽到 McpToolUtils 复用
*/
private ProductResolveResult resolveProduct(Long productId, String productKey, String productName) {
ProductResolveResult out = new ProductResolveResult();