SimpleMcpClient.groovy
10.3 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*
* This software is in the public domain under CC0 1.0 Universal plus a
* Grant of Patent License.
*
* To the extent possible under law, 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
* <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
package org.moqui.mcp.test
import groovy.json.JsonBuilder
import groovy.json.JsonSlurper
import java.net.http.HttpClient
import java.net.http.HttpRequest
import java.net.http.HttpResponse
import java.net.URI
import java.time.Duration
import java.util.concurrent.ConcurrentHashMap
/**
* Simple MCP client for testing MCP server functionality
* Makes JSON-RPC requests to the MCP server endpoint
*/
class SimpleMcpClient {
private String baseUrl
private String sessionId
private HttpClient httpClient
private JsonSlurper jsonSlurper
private Map<String, Object> sessionData = new ConcurrentHashMap<>()
SimpleMcpClient(String baseUrl = "http://localhost:8080/mcp") {
this.baseUrl = baseUrl
this.httpClient = HttpClient.newBuilder()
.connectTimeout(Duration.ofSeconds(30))
.build()
this.jsonSlurper = new JsonSlurper()
}
/**
* Initialize MCP session with Basic authentication
*/
boolean initializeSession(String username = "john.sales", String password = "moqui") {
try {
// Store credentials for Basic auth
sessionData.put("username", username)
sessionData.put("password", password)
// Initialize MCP session
def params = [
protocolVersion: "2025-06-18",
capabilities: [tools: [:], resources: [:]],
clientInfo: [name: "SimpleMcpClient", version: "1.0.0"]
]
def result = makeJsonRpcRequest("initialize", params)
if (result && result.result && result.result.sessionId) {
this.sessionId = result.result.sessionId
sessionData.put("initialized", true)
sessionData.put("sessionId", sessionId)
println "Session initialized: ${sessionId}"
return true
}
return false
} catch (Exception e) {
println "Error initializing session: ${e.message}"
return false
}
}
/**
* Make JSON-RPC request to MCP server
*/
private Map makeJsonRpcRequest(String method, Map params = null) {
try {
def requestBody = [
jsonrpc: "2.0",
id: System.currentTimeMillis(),
method: method
]
if (params != null) {
requestBody.params = params
}
def requestBuilder = HttpRequest.newBuilder()
.uri(URI.create(baseUrl))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(new JsonBuilder(requestBody).toString()))
// Add Basic authentication
if (sessionData.containsKey("username") && sessionData.containsKey("password")) {
def auth = "${sessionData.username}:${sessionData.password}"
def encodedAuth = java.util.Base64.getEncoder().encodeToString(auth.bytes)
requestBuilder.header("Authorization", "Basic ${encodedAuth}")
}
// Add session header for non-initialize requests
if (method != "initialize" && sessionId) {
requestBuilder.header("Mcp-Session-Id", sessionId)
}
def request = requestBuilder.build()
def response = httpClient.send(request, HttpResponse.BodyHandlers.ofString())
if (response.statusCode() == 200) {
return jsonSlurper.parseText(response.body())
} else {
return [error: [message: "HTTP ${response.statusCode()}: ${response.body()}"]]
}
} catch (Exception e) {
println "Error making JSON-RPC request: ${e.message}"
return [error: [message: e.message]]
}
}
/**
* Ping MCP server
*/
boolean ping() {
try {
def result = makeJsonRpcRequest("tools/call", [
name: "McpServices.mcp#Ping",
arguments: [:]
])
return result && !result.error
} catch (Exception e) {
println "Error pinging server: ${e.message}"
return false
}
}
/**
* List available tools
*/
List<Map> listTools() {
try {
def result = makeJsonRpcRequest("tools/list", [sessionId: sessionId])
if (result && result.result && result.result.tools) {
return result.result.tools
}
return []
} catch (Exception e) {
println "Error listing tools: ${e.message}"
return []
}
}
/**
* Call any tool
*/
Map callTool(String toolName, Map arguments = [:]) {
try {
def result = makeJsonRpcRequest("tools/call", [
name: toolName,
arguments: arguments
])
return result ?: [error: [message: "No response from server"]]
} catch (Exception e) {
println "Error calling tool ${toolName}: ${e.message}"
return [error: [message: e.message]]
}
}
/**
* Call a screen tool
*/
Map callScreen(String screenPath, Map parameters = [:]) {
try {
// Determine the correct tool name based on the screen path
String toolName = getScreenToolName(screenPath)
// Don't override render mode - let the MCP service handle it
def args = parameters
def result = makeJsonRpcRequest("tools/call", [
name: toolName,
arguments: args
])
return result ?: [error: [message: "No response from server"]]
} catch (Exception e) {
println "Error calling screen ${screenPath}: ${e.message}"
return [error: [message: e.message]]
}
}
/**
* Get the correct tool name for a given screen path
*/
private String getScreenToolName(String screenPath) {
// If it already looks like a tool name, return it
if (screenPath.startsWith("screen_")) {
return screenPath
}
// Clean Encoding: strip component:// and .xml, replace / with _
def cleanPath = screenPath
if (cleanPath.startsWith("component://")) cleanPath = cleanPath.substring(12)
if (cleanPath.endsWith(".xml")) cleanPath = cleanPath.substring(0, cleanPath.length() - 4)
return "screen_" + cleanPath.replace('/', '_')
}
/**
* Search for products in PopCommerce catalog
*/
List<Map> searchProducts(String color = "blue", String category = "PopCommerce") {
def result = callScreen("PopCommerce/Catalog/Product", [
color: color,
category: category
])
if (result.error) {
println "Error searching products: ${result.error.message}"
return []
}
// Extract products from the screen response
def content = result.result?.content
if (content && content instanceof List && content.size() > 0) {
// Look for products in the content
for (item in content) {
if (item.type == "resource" && item.resource && item.resource.products) {
return item.resource.products
}
}
}
return []
}
/**
* Find customer by name
*/
Map findCustomer(String firstName = "John", String lastName = "Doe") {
def result = callScreen("PopCommerce/Customer/FindCustomer", [
firstName: firstName,
lastName: lastName
])
if (result.error) {
println "Error finding customer: ${result.error.message}"
return [:]
}
// Extract customer from the screen response
def content = result.result?.content
if (content && content instanceof List && content.size() > 0) {
// Look for customer in the content
for (item in content) {
if (item.type == "resource" && item.resource && item.resource.customer) {
return item.resource.customer
}
}
}
return [:]
}
/**
* Create an order
*/
Map createOrder(String customerId, String productId, Map orderDetails = [:]) {
def parameters = [
customerId: customerId,
productId: productId
] + orderDetails
def result = callScreen("PopCommerce/Order/CreateOrder", parameters)
if (result.error) {
println "Error creating order: ${result.error.message}"
return [:]
}
// Extract order from the screen response
def content = result.result?.content
if (content && content instanceof List && content.size() > 0) {
// Look for order in the content
for (item in content) {
if (item.type == "resource" && item.resource && item.resource.order) {
return item.resource.order
}
}
}
return [:]
}
/**
* Get session data
*/
Map getSessionData() {
return new HashMap(sessionData)
}
/**
* Close the session
*/
void closeSession() {
try {
if (sessionId) {
makeJsonRpcRequest("close", [:])
}
} catch (Exception e) {
println "Error closing session: ${e.message}"
} finally {
sessionData.clear()
sessionId = null
}
}
}