Junjie
昨天 411ff551ae7641dfc5c9331e99bf8b6e5770e2fa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package com.zy.ai.mcp.config;
 
import com.alibaba.fastjson.JSONObject;
import com.zy.ai.mcp.dto.McpToolHandler;
import com.zy.ai.mcp.dto.ToolDefinition;
import com.zy.ai.mcp.dto.ToolRegistry;
import com.zy.ai.mcp.service.WcsDataFacade;
 
import java.util.*;
 
public class McpToolsBootstrap {
 
    public static void registerAll(ToolRegistry registry, final WcsDataFacade facade) {
 
        registry.register(tool(
                "device.get_crn_status",
                "Query realtime status of a crn device by deviceNo.",
                schemaObj(
                        propInt("crnNos", true)
                ),
                schemaObj(
                        propObj("devices", true)
                ),
                new McpToolHandler() {
                    public Object handle(JSONObject args) {
                        return facade.getCrnDeviceStatus(args);
                    }
                }
        ));
 
        registry.register(tool(
                "device.get_station_status",
                "Query realtime status of a station device",
                schemaObj(
 
                ),
                schemaObj(
                        propObj("stations", true)
                ),
                new McpToolHandler() {
                    public Object handle(JSONObject args) {
                        return facade.getStationDeviceStatus(args);
                    }
                }
        ));
 
        registry.register(tool(
                "device.get_rgv_status",
                "Query realtime status of a rgv device by deviceNo.",
                schemaObj(
                        propInt("rgvNos", true)
                ),
                schemaObj(
                        propObj("devices", true)
                ),
                new McpToolHandler() {
                    public Object handle(JSONObject args) {
                        return facade.getRgvDeviceStatus(args);
                    }
                }
        ));
 
        registry.register(tool(
                "task.list",
                "List tasks by filters (status/CrnDevice/RgvDevice//time window).",
                schemaObj(
                        propInt("crnNo", false),
                        propInt("rgvNo", false),
                        propArr("taskNos", false, "integer"),
                        propInt("limit", false)
                ),
                schemaObj(propArr("tasks", true, "object")),
                new McpToolHandler() {
                    public Object handle(JSONObject args) {
                        return facade.getTasks(args);
                    }
                }
        ));
 
        registry.register(tool(
                "log.query",
                "Query logs by keyword/level/time window/device/task. Return clipped log lines.",
                schemaObj(
                        propInt("limit", false)
                ),
                schemaObj(propArr("logs", true, "object")),
                new McpToolHandler() {
                    public Object handle(JSONObject args) {
                        return facade.getLogs(args);
                    }
                }
        ));
 
        registry.register(tool(
                "config.get_device_config",
                "Get device config by deviceCode.",
                schemaObj(
                        propArr("crnNos", false, "integer"),
                        propArr("rgvNos", false, "integer"),
                        propArr("devpNos", false, "integer")
                ),
                schemaObj(propObj("deviceConfigs", true)),
                new McpToolHandler() {
                    public Object handle(JSONObject args) {
                        return facade.getDeviceConfig(args);
                    }
                }
        ));
 
        registry.register(tool(
                "config.get_system_config",
                "Get key system configs for diagnosis.",
                schemaObj(
 
                ),
                schemaObj(
                        propObj("systemConfigs", true)
                ),
                new McpToolHandler() {
                    public Object handle(JSONObject args) {
                        return facade.getSystemConfig(args);
                    }
                }
        ));
 
//        // ★ 诊断聚合快照:一次拿全
//        registry.register(tool(
//                "build_diagnosis_snapshot",
//                "Aggregate diagnosis snapshot: tasks + device realtime + configs + clipped logs for diagnosis.",
//                schemaObj(
//                        propStr("warehouseCode", true),
//                        propArr("deviceCodes", false, "string"),     // 不传则按任务涉及设备推导
//                        propStr("taskNo", false),
//                        propStr("fromTime", true),
//                        propStr("toTime", true),
//                        propInt("taskLimit", false),
//                        propInt("logLimit", false),
//                        propArr("logKeywords", false, "string"),
//                        propBool("includeSystemConfig", false),
//                        propBool("includeDeviceConfig", false)
//                ),
//                schemaObj(
//                        propObj("snapshot", true),
//                        propArr("hints", false, "string")
//                ),
//                new McpToolHandler() {
//                    public Object handle(JSONObject args) {
//                        return facade.buildDiagnosisSnapshot(args);
//                    }
//                }
//        ));
    }
 
    // ---------- schema helpers ----------
    private static ToolDefinition tool(String name, String desc,
                                       Map<String, Object> in, Map<String, Object> out,
                                       McpToolHandler handler) {
        ToolDefinition d = new ToolDefinition();
        d.setName(name);
        d.setDescription(desc);
        d.setInputSchema(in);
        d.setOutputSchema(out);
        d.setHandler(handler);
        return d;
    }
 
    private static Map<String, Object> schemaObj(Object... props) {
        Map<String, Object> m = new LinkedHashMap<String, Object>();
        m.put("type", "object");
 
        Map<String, Object> properties = new LinkedHashMap<String, Object>();
        List<String> required = new ArrayList<String>();
 
        for (Object p : props) {
            @SuppressWarnings("unchecked")
            Map<String, Object> pm = (Map<String, Object>) p;
            String name = String.valueOf(pm.get("name"));
            boolean req = Boolean.TRUE.equals(pm.get("required"));
            pm.remove("name");
            pm.remove("required");
            properties.put(name, pm);
            if (req) required.add(name);
        }
 
        m.put("properties", properties);
        if (!required.isEmpty()) m.put("required", required);
        return m;
    }
 
    private static Map<String, Object> propStr(String name, boolean required) {
        Map<String, Object> m = new LinkedHashMap<String, Object>();
        m.put("name", name);
        m.put("required", required);
        m.put("type", "string");
        return m;
    }
 
    private static Map<String, Object> propInt(String name, boolean required) {
        Map<String, Object> m = new LinkedHashMap<String, Object>();
        m.put("name", name);
        m.put("required", required);
        m.put("type", "integer");
        return m;
    }
 
    private static Map<String, Object> propBool(String name, boolean required) {
        Map<String, Object> m = new LinkedHashMap<String, Object>();
        m.put("name", name);
        m.put("required", required);
        m.put("type", "boolean");
        return m;
    }
 
    private static Map<String, Object> propObj(String name, boolean required) {
        Map<String, Object> m = new LinkedHashMap<String, Object>();
        m.put("name", name);
        m.put("required", required);
        m.put("type", "object");
        return m;
    }
 
    private static Map<String, Object> propArr(String name, boolean required, String itemType) {
        Map<String, Object> m = new LinkedHashMap<String, Object>();
        m.put("name", name);
        m.put("required", required);
        m.put("type", "array");
        Map<String, Object> items = new LinkedHashMap<String, Object>();
        items.put("type", itemType);
        m.put("items", items);
        return m;
    }
}