@@ -12,7 +12,8 @@ import path from 'path'
1212import crypto from 'crypto'
1313import { spawn } from 'child_process'
1414import { createRequire } from 'module'
15- import { existsSync , readdirSync } from 'fs'
15+ import { existsSync , readdirSync , writeFileSync } from 'fs'
16+ import { mkdirp } from 'mkdirp'
1617
1718const require = createRequire ( import . meta. url )
1819
@@ -439,12 +440,38 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
439440 const helper = Object . values ( helpers ) [ 0 ]
440441 if ( helper ) {
441442 try {
442- if ( helper . grabAriaSnapshot ) result . artifacts . aria = await helper . grabAriaSnapshot ( )
443- if ( helper . grabCurrentUrl ) result . artifacts . url = await helper . grabCurrentUrl ( )
444- if ( helper . grabBrowserLogs ) result . artifacts . consoleLogs = ( await helper . grabBrowserLogs ( ) ) || [ ]
443+ const traceDir = getTraceDir ( 'mcp' , 'run_code' )
444+ mkdirp . sync ( traceDir )
445+
446+ if ( helper . grabAriaSnapshot ) {
447+ const aria = await helper . grabAriaSnapshot ( )
448+ const ariaFile = path . join ( traceDir , 'aria.txt' )
449+ writeFileSync ( ariaFile , aria )
450+ result . artifacts . aria = `file://${ ariaFile } `
451+ }
452+
453+ if ( helper . grabCurrentUrl ) {
454+ result . artifacts . url = await helper . grabCurrentUrl ( )
455+ }
456+
457+ if ( helper . grabBrowserLogs ) {
458+ const logs = ( await helper . grabBrowserLogs ( ) ) || [ ]
459+ const logsFile = path . join ( traceDir , 'console.json' )
460+ writeFileSync ( logsFile , JSON . stringify ( logs , null , 2 ) )
461+ result . artifacts . consoleLogs = `file://${ logsFile } `
462+ }
463+
445464 if ( helper . grabSource ) {
446465 const html = await helper . grabSource ( )
447- result . artifacts . html = html . substring ( 0 , 10000 ) + '...'
466+ const htmlFile = path . join ( traceDir , 'page.html' )
467+ writeFileSync ( htmlFile , html )
468+ result . artifacts . html = `file://${ htmlFile } `
469+ }
470+
471+ if ( helper . saveScreenshot ) {
472+ const screenshotFile = path . join ( traceDir , 'screenshot.png' )
473+ await helper . saveScreenshot ( screenshotFile )
474+ result . artifacts . screenshot = `file://${ screenshotFile } `
448475 }
449476 } catch ( e ) {
450477 result . output += ` (Warning: ${ e . message } )`
0 commit comments