5745c06b by Ean Schuessler

Implement ergonomic MCP tool improvements: tiered discovery (Browse, Search, Det…

…ails), semantic naming, and McpUtils refactoring.
1 parent 61da89b9
1 package org.moqui.mcp
2
3 import org.moqui.util.MNode
4
5 class McpUtils {
6 /**
7 * Convert a Moqui screen path to an MCP tool name.
8 * Preserves case to ensure reversibility.
9 * Format: moqui_<Component>_<Path_Parts>
10 * Example: component://PopCommerce/screen/PopCommerceAdmin/Catalog.xml -> moqui_PopCommerce_PopCommerceAdmin_Catalog
11 */
12 static String getToolName(String screenPath) {
13 if (!screenPath) return null
14
15 // Strip component:// prefix and .xml suffix
16 String cleanPath = screenPath
17 if (cleanPath.startsWith("component://")) cleanPath = cleanPath.substring(12)
18 if (cleanPath.endsWith(".xml")) cleanPath = cleanPath.substring(0, cleanPath.length() - 4)
19
20 List<String> parts = cleanPath.split('/').toList()
21
22 // Remove 'screen' if it's the second part (standard structure: component/screen/...)
23 if (parts.size() > 1 && parts[1] == "screen") {
24 parts.remove(1)
25 }
26
27 // Join with underscores and prefix
28 return "moqui_" + parts.join('_')
29 }
30
31 /**
32 * Convert an MCP tool name back to a Moqui screen path.
33 * Assumes standard component://<Component>/screen/<Path>.xml structure.
34 */
35 static String getScreenPath(String toolName) {
36 if (!toolName || !toolName.startsWith("moqui_")) return null
37
38 String cleanName = toolName.substring(6) // Remove moqui_
39 List<String> parts = cleanName.split('_').toList()
40
41 if (parts.size() < 1) return null
42
43 String component = parts[0]
44
45 // If there's only one part (e.g. moqui_MyComponent), it might be a root or invalid
46 // But usually we expect at least component and screen
47 if (parts.size() == 1) {
48 // Fallback for component roots? unlikely to be a valid screen path without 'screen' dir
49 return "component://${component}/screen/${component}.xml"
50 }
51
52 // Re-insert 'screen' directory which is standard
53 String path = parts.subList(1, parts.size()).join('/')
54 return "component://${component}/screen/${path}.xml"
55 }
56 }