webapp.xml
8.53 KB
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
<?xml version="1.0" encoding="UTF-8"?>
<!-- This software is in the public domain under CC0 1.0 Universal plus a
Grant of Patent License.
To the extent possible under law, the author(s) have dedicated all
copyright and related and neighboring rights to this software to the
public domain worldwide. This software is distributed without any warranty.
You should have received a copy of the CC0 Public Domain Dedication
along with this software (see the LICENSE.md file). If not, see
<https://creativecommons.org/publicdomain/zero/1.0/>. -->
<screen xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://moqui.org/xsd/xml-screen-3.xsd"
require-authentication="true" track-artifact-hit="false" default-menu-include="false">
<parameter name="jsonrpc"/>
<parameter name="id"/>
<parameter name="method"/>
<parameter name="params"/>
<actions>
<!-- Handle MCP JSON-RPC requests -->
<script><![CDATA[
import groovy.json.JsonBuilder
import groovy.json.JsonSlurper
// Check MCP protocol version header
def protocolVersion = ec.web.request.getHeader("MCP-Protocol-Version")
if (!protocolVersion) {
protocolVersion = "2025-03-26" // Default for backwards compatibility
}
// Only handle POST requests for JSON-RPC, GET for SSE streams
if (ec.web.request.method != "POST" && ec.web.request.method != "GET") {
ec.web.sendError(405, "Method Not Allowed: MCP supports POST and GET")
return
}
// Handle GET requests for SSE streams
if (ec.web.request.method == "GET") {
handleSseStream(ec, protocolVersion)
return
}
// Parse JSON-RPC request body if not already in parameters
if (!jsonrpc && !method) {
def requestBody = ec.web.request.getInputStream()?.getText()
if (requestBody) {
def jsonSlurper = new JsonSlurper()
def jsonRequest = jsonSlurper.parseText(requestBody)
jsonrpc = jsonRequest.jsonrpc
id = jsonRequest.id
method = jsonRequest.method
params = jsonRequest.params
}
}
// Validate JSON-RPC version
if (jsonrpc && jsonrpc != "2.0") {
def errorResponse = new JsonBuilder([
jsonrpc: "2.0",
error: [
code: -32600,
message: "Invalid Request: Only JSON-RPC 2.0 supported"
],
id: id
]).toString()
ec.web.sendJsonResponse(errorResponse)
return
}
def result = null
def error = null
try {
// Route to appropriate MCP service
def serviceName = null
switch (method) {
case "initialize":
serviceName = "mo-mcp.McpJsonRpcServices.handle#Initialize"
break
case "tools/list":
serviceName = "mo-mcp.McpJsonRpcServices.handle#ToolsList"
break
case "tools/call":
serviceName = "mo-mcp.McpJsonRpcServices.handle#ToolsCall"
break
case "resources/list":
serviceName = "mo-mcp.McpJsonRpcServices.handle#ResourcesList"
break
case "resources/read":
serviceName = "mo-mcp.McpJsonRpcServices.handle#ResourcesRead"
break
case "ping":
serviceName = "mo-mcp.McpJsonRpcServices.handle#Ping"
break
default:
error = [
code: -32601,
message: "Method not found: ${method}"
]
}
if (serviceName && !error) {
// Call the MCP service
result = ec.service.sync(serviceName, params ?: [:])
}
} catch (Exception e) {
ec.logger.error("MCP JSON-RPC error for method ${method}", e)
error = [
code: -32603,
message: "Internal error: ${e.message}"
]
}
// Build JSON-RPC response
def responseObj = [
jsonrpc: "2.0",
id: id
]
if (error) {
responseObj.error = error
} else {
responseObj.result = result
}
def response = new JsonBuilder(responseObj).toString()
// Check Accept header for response format negotiation
def acceptHeader = ec.web.request.getHeader("Accept") ?: ""
def wantsSse = acceptHeader.contains("text/event-stream")
if (wantsSse && method) {
// Send SSE response for streaming
sendSseResponse(ec, responseObj, protocolVersion)
} else {
// Send regular JSON response
ec.web.sendJsonResponse(response)
}
]]></script>
</actions>
<actions>
<!-- SSE Helper Functions -->
<script><![CDATA[
def handleSseStream(ec, protocolVersion) {
// Set SSE headers
ec.web.response.setContentType("text/event-stream")
ec.web.response.setCharacterEncoding("UTF-8")
ec.web.response.setHeader("Cache-Control", "no-cache")
ec.web.response.setHeader("Connection", "keep-alive")
ec.web.response.setHeader("MCP-Protocol-Version", protocolVersion)
def writer = ec.web.response.writer
try {
// Send initial connection event
writer.write("event: connected\n")
writer.write("data: {\"type\":\"connected\",\"timestamp\":\"${ec.user.now}\"}\n")
writer.write("\n")
writer.flush()
// Keep connection alive with periodic pings
def count = 0
while (count < 30) { // Keep alive for ~30 seconds
Thread.sleep(1000)
writer.write("event: ping\n")
writer.write("data: {\"timestamp\":\"${ec.user.now}\"}\n")
writer.write("\n")
writer.flush()
count++
}
} catch (Exception e) {
ec.logger.warn("SSE stream interrupted: ${e.message}")
} finally {
writer.close()
}
}
def sendSseResponse(ec, responseObj, protocolVersion) {
// Set SSE headers
ec.web.response.setContentType("text/event-stream")
ec.web.response.setCharacterEncoding("UTF-8")
ec.web.response.setHeader("Cache-Control", "no-cache")
ec.web.response.setHeader("Connection", "keep-alive")
ec.web.response.setHeader("MCP-Protocol-Version", protocolVersion)
def writer = ec.web.response.writer
def jsonBuilder = new JsonBuilder(responseObj)
try {
// Send the response as SSE event
writer.write("event: response\n")
writer.write("data: ${jsonBuilder.toString()}\n")
writer.write("\n")
writer.flush()
} catch (Exception e) {
ec.logger.error("Error sending SSE response: ${e.message}")
} finally {
writer.close()
}
}
]]></script>
</actions>
<widgets>
<!-- This screen should never render widgets - it handles JSON-RPC requests directly -->
</widgets>
</screen>