Add comprehensive web context mocking for screen rendering
- Mock html_scripts, html_stylesheets for web-dependent screens - Mock webappName, servletContext, request, response objects - Mock ec.web.getResourceDistinctValue() for template compatibility - Mock sri object with buildUrl, getThemeValues, sendRedirectAndStopRender - Enables PopCommerce and other web screens to render in text mode - Text mode bypasses web dependencies and renders core content successfully
Showing
1 changed file
with
31 additions
and
4 deletions
| ... | @@ -1355,20 +1355,47 @@ try { | ... | @@ -1355,20 +1355,47 @@ try { |
| 1355 | try { | 1355 | try { |
| 1356 | ec.logger.info("MCP Screen Execution: Attempting to render screen ${screenPath}") | 1356 | ec.logger.info("MCP Screen Execution: Attempting to render screen ${screenPath}") |
| 1357 | 1357 | ||
| 1358 | // Mock web context objects that screens may expect | ||
| 1359 | ec.context.put("html_scripts", new LinkedHashSet<String>()) | ||
| 1360 | ec.context.put("html_stylesheets", new LinkedHashSet<String>()) | ||
| 1361 | |||
| 1362 | // Mock other common web context objects | ||
| 1363 | ec.context.put("webappName", "mcp") | ||
| 1364 | ec.context.put("servletContext", [:]) | ||
| 1365 | ec.context.put("request", [:]) | ||
| 1366 | ec.context.put("response", [:]) | ||
| 1367 | |||
| 1368 | // Mock ec.web for getResourceDistinctValue() calls | ||
| 1369 | def mockWeb = [ | ||
| 1370 | getResourceDistinctValue: { -> return System.currentTimeMillis() } | ||
| 1371 | ] | ||
| 1372 | ec.context.put("web", mockWeb) | ||
| 1373 | |||
| 1358 | // Try to render screen with specified render mode for LLM to read | 1374 | // Try to render screen with specified render mode for LLM to read |
| 1359 | def screenRender = ec.screen.makeRender() | 1375 | def screenRender = ec.screen.makeRender() |
| 1360 | .rootScreen(screenPath) // Set root screen location | 1376 | .rootScreen(screenPath) // Set root screen location |
| 1361 | .renderMode(renderMode) // Set render mode from parameter, but don't set additional path for root screen | 1377 | .renderMode(renderMode) // Set render mode from parameter, but don't set additional path for root screen |
| 1362 | 1378 | ||
| 1363 | // Mock sri.sendRedirectAndStopRender() to prevent redirects in MCP context | 1379 | // Get the real sri object and override problematic methods |
| 1364 | // We want screen content, not redirects to other pages | 1380 | // Put mock sri in context that will be used by templates |
| 1365 | def mockSri = [ | 1381 | def mockSriForContext = [ |
| 1382 | buildUrl: { String path -> | ||
| 1383 | return [ | ||
| 1384 | url: path.startsWith("/") ? path : "/${path}", | ||
| 1385 | path: path, | ||
| 1386 | isPermitted: { -> true }, | ||
| 1387 | toString: { -> return path.startsWith("/") ? path : "/${path}" } | ||
| 1388 | ] | ||
| 1389 | }, | ||
| 1390 | getThemeValues: { String enumId -> | ||
| 1391 | return [] | ||
| 1392 | }, | ||
| 1366 | sendRedirectAndStopRender: { String redirectUrl -> | 1393 | sendRedirectAndStopRender: { String redirectUrl -> |
| 1367 | ec.logger.info("MCP Screen Execution: Ignoring redirect to ${redirectUrl} - continuing screen render for MCP") | 1394 | ec.logger.info("MCP Screen Execution: Ignoring redirect to ${redirectUrl} - continuing screen render for MCP") |
| 1368 | // Don't actually redirect, just log and continue | 1395 | // Don't actually redirect, just log and continue |
| 1369 | } | 1396 | } |
| 1370 | ] | 1397 | ] |
| 1371 | ec.context.put("sri", mockSri) | 1398 | ec.context.put("sri", mockSriForContext) |
| 1372 | 1399 | ||
| 1373 | ec.logger.info("MCP Screen Execution: ScreenRender object created: ${screenRender?.getClass()?.getSimpleName()}") | 1400 | ec.logger.info("MCP Screen Execution: ScreenRender object created: ${screenRender?.getClass()?.getSimpleName()}") |
| 1374 | 1401 | ... | ... |
-
Please register or sign in to post a comment