package com.iot.gateway.mcp; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; @Slf4j @Component public class McpToolRegistry { public List> getToolDefinitions() { List> tools = new ArrayList<>(); // 1. iot_device_list Map t1 = new HashMap<>(); t1.put("name", "iot_device_list"); t1.put("description", "查询当前租户名下的所有工业物联网设备台账与实时运行状态(在线/报警/停机)。"); Map s1 = new HashMap<>(); s1.put("type", "object"); Map p1 = new HashMap<>(); p1.put("status", Map.of("type", "string", "description", "设备状态过滤: online/offline/alarm,默认全部")); p1.put("pageNum", Map.of("type", "integer", "description", "分页页码,默认 1")); p1.put("pageSize", Map.of("type", "integer", "description", "每页条数,默认 10")); s1.put("properties", p1); t1.put("inputSchema", s1); tools.add(t1); // 2. iot_telemetry_latest Map t2 = new HashMap<>(); t2.put("name", "iot_telemetry_latest"); t2.put("description", "召测指定工业设备的最新实时点位遥测数据(注塑机温度、注塑压力、螺杆转速、能耗电流等)。"); Map s2 = new HashMap<>(); s2.put("type", "object"); Map p2 = new HashMap<>(); p2.put("deviceId", Map.of("type", "string", "description", "设备唯一标识编码或编号")); s2.put("properties", p2); s2.put("required", List.of("deviceId")); t2.put("inputSchema", s2); tools.add(t2); // 3. iot_device_control Map t3 = new HashMap<>(); t3.put("name", "iot_device_control"); t3.put("description", "向边缘物联设备安全下发反写控制指令(如设定注塑温度、设备启停控制、工艺参数微调)。"); Map s3 = new HashMap<>(); s3.put("type", "object"); Map p3 = new HashMap<>(); p3.put("deviceId", Map.of("type", "string", "description", "目标设备编码")); p3.put("command", Map.of("type", "string", "description", "指令动作: start/stop/set_param")); p3.put("params", Map.of("type", "object", "description", "指令参数键值对")); s3.put("properties", p3); s3.put("required", List.of("deviceId", "command")); t3.put("inputSchema", s3); tools.add(t3); // 4. iot_alarm_query Map t4 = new HashMap<>(); t4.put("name", "iot_alarm_query"); t4.put("description", "按设备与时间范围检索未处理的设备故障与历史严重告警记录。"); Map s4 = new HashMap<>(); s4.put("type", "object"); Map p4 = new HashMap<>(); p4.put("deviceId", Map.of("type", "string", "description", "设备编码(可选)")); p4.put("severity", Map.of("type", "integer", "description", "严重级别: 1提示, 2次要, 3严重, 4紧急")); s4.put("properties", p4); t4.put("inputSchema", s4); tools.add(t4); return tools; } public Map executeTool(String toolName, Map args, Long tenantId) { log.info("执行 MCP 工具调用: tool={}, tenantId={}, args={}", toolName, tenantId, args); Map result = new HashMap<>(); if ("iot_device_list".equals(toolName)) { result.put("tenantId", tenantId); result.put("total", 3); result.put("devices", List.of( Map.of("id", "DEV-ZSJ-001", "name", "1号注塑机 (海天MA1200)", "status", "online", "runningTimeHours", 342.5), Map.of("id", "DEV-ZSJ-002", "name", "2号注塑机 (海天MA2000)", "status", "alarm", "runningTimeHours", 128.0), Map.of("id", "DEV-CNC-003", "name", "3号数控精雕机", "status", "offline", "runningTimeHours", 89.2) )); } else if ("iot_telemetry_latest".equals(toolName)) { String deviceId = String.valueOf(args.getOrDefault("deviceId", "DEV-ZSJ-001")); result.put("deviceId", deviceId); result.put("timestamp", System.currentTimeMillis()); result.put("telemetry", Map.of( "barrel_temp_zone1", 215.4, "barrel_temp_zone2", 220.1, "injection_pressure_mpa", 13.8, "screw_speed_rpm", 85.0, "cycle_time_seconds", 24.3, "electric_current_a", 42.6 )); } else if ("iot_device_control".equals(toolName)) { String deviceId = String.valueOf(args.get("deviceId")); String command = String.valueOf(args.get("command")); result.put("success", true); result.put("deviceId", deviceId); result.put("command", command); result.put("status", "EXECUTED"); result.put("message", "控制指令已成功通过 MQTT 发送给边缘网关下发执行"); } else if ("iot_alarm_query".equals(toolName)) { result.put("alarms", List.of( Map.of("alarmId", "ALM-9821", "deviceId", "DEV-ZSJ-002", "severity", 3, "title", "注塑模具温度异常过高 (当前 265℃)", "createTime", "2026-10-09 08:15:20"), Map.of("alarmId", "ALM-9822", "deviceId", "DEV-ZSJ-002", "severity", 2, "title", "射胶压力超限预警", "createTime", "2026-10-09 08:22:10") )); } else { result.put("error", "未知的工具名称: " + toolName); } return result; } }