File tree Expand file tree Collapse file tree 2 files changed +27
-1
lines changed Expand file tree Collapse file tree 2 files changed +27
-1
lines changed Original file line number Diff line number Diff line change
1
+ import sharp from "sharp" ;
2
+
1
3
export function sanitizeUrlParam ( param : string ) : string {
2
4
// Remove any characters that could be used for command injection
3
5
return param . replace ( / [ ; & | ` $ ( ) { } [ \] < > ] / g, "" ) ;
@@ -24,3 +26,24 @@ export interface HarEntry {
24
26
serverIPAddress ?: string ;
25
27
time ?: number ;
26
28
}
29
+
30
+ /**
31
+ * Compresses a base64 image intelligently to keep it under 1 MB if needed.
32
+ */
33
+ export async function maybeCompressBase64 ( base64 : string ) : Promise < string > {
34
+ const buffer = Buffer . from ( base64 , "base64" ) ;
35
+
36
+ if ( buffer . length <= 1048576 ) {
37
+ return base64 ;
38
+ }
39
+
40
+ const sizeRatio = 1048576 / buffer . length ;
41
+ const estimatedQuality = Math . floor ( sizeRatio * 100 ) ;
42
+ const quality = Math . min ( 95 , Math . max ( 30 , estimatedQuality ) ) ;
43
+
44
+ const compressedBuffer = await sharp ( buffer )
45
+ . png ( { quality } )
46
+ . toBuffer ( ) ;
47
+
48
+ return compressedBuffer . toString ( "base64" ) ;
49
+ }
Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ import { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
4
4
import logger from "../logger" ;
5
5
import config from "../config" ;
6
6
import { trackMCP } from "../lib/instrumentation" ;
7
+ import { maybeCompressBase64 } from "../lib/utils" ;
7
8
8
9
import {
9
10
getDevicesAndBrowsers ,
@@ -122,13 +123,15 @@ async function takeAppScreenshot(args: {
122
123
} ) ;
123
124
124
125
const screenshotBase64 = await driver . takeScreenshot ( ) ;
126
+ const compressed = await maybeCompressBase64 ( screenshotBase64 ) ;
125
127
126
128
return {
127
129
content : [
128
130
{
129
131
type : "image" ,
130
- data : screenshotBase64 ,
132
+ data : compressed ,
131
133
mimeType : "image/png" ,
134
+ name : `screenshot-${ selectedDevice . device } -${ Date . now ( ) } ` ,
132
135
} ,
133
136
] ,
134
137
} ;
You can’t perform that action at this time.
0 commit comments