@@ -458,11 +458,31 @@ export const TextResourceContentsSchema = ResourceContentsSchema.extend({
458
458
text : z . string ( ) ,
459
459
} ) ;
460
460
461
+
462
+ /**
463
+ * A Zod schema for validating Base64 strings that is more performant and
464
+ * robust for very large inputs than the default regex-based check. It avoids
465
+ * stack overflows by using the native `atob` function for validation.
466
+ */
467
+ const Base64Schema = z . string ( ) . refine (
468
+ ( val ) => {
469
+ try {
470
+ // atob throws a DOMException if the string contains characters
471
+ // that are not part of the Base64 character set.
472
+ atob ( val ) ;
473
+ return true ;
474
+ } catch {
475
+ return false ;
476
+ }
477
+ } ,
478
+ { message : "Invalid Base64 string" } ,
479
+ ) ;
480
+
461
481
export const BlobResourceContentsSchema = ResourceContentsSchema . extend ( {
462
482
/**
463
483
* A base64-encoded string representing the binary data of the item.
464
484
*/
465
- blob : z . string ( ) . base64 ( ) ,
485
+ blob : Base64Schema ,
466
486
} ) ;
467
487
468
488
/**
@@ -718,7 +738,7 @@ export const ImageContentSchema = z
718
738
/**
719
739
* The base64-encoded image data.
720
740
*/
721
- data : z . string ( ) . base64 ( ) ,
741
+ data : Base64Schema ,
722
742
/**
723
743
* The MIME type of the image. Different providers may support different image types.
724
744
*/
@@ -741,7 +761,7 @@ export const AudioContentSchema = z
741
761
/**
742
762
* The base64-encoded audio data.
743
763
*/
744
- data : z . string ( ) . base64 ( ) ,
764
+ data : Base64Schema ,
745
765
/**
746
766
* The MIME type of the audio. Different providers may support different audio types.
747
767
*/
@@ -894,7 +914,7 @@ export const ToolSchema = BaseMetadataSchema.extend({
894
914
} )
895
915
. passthrough ( ) ,
896
916
/**
897
- * An optional JSON Schema object defining the structure of the tool's output returned in
917
+ * An optional JSON Schema object defining the structure of the tool's output returned in
898
918
* the structuredContent field of a CallToolResult.
899
919
*/
900
920
outputSchema : z . optional (
0 commit comments