@@ -475,11 +475,31 @@ export const TextResourceContentsSchema = ResourceContentsSchema.extend({
475
475
text : z . string ( ) ,
476
476
} ) ;
477
477
478
+
479
+ /**
480
+ * A Zod schema for validating Base64 strings that is more performant and
481
+ * robust for very large inputs than the default regex-based check. It avoids
482
+ * stack overflows by using the native `atob` function for validation.
483
+ */
484
+ const Base64Schema = z . string ( ) . refine (
485
+ ( val ) => {
486
+ try {
487
+ // atob throws a DOMException if the string contains characters
488
+ // that are not part of the Base64 character set.
489
+ atob ( val ) ;
490
+ return true ;
491
+ } catch {
492
+ return false ;
493
+ }
494
+ } ,
495
+ { message : "Invalid Base64 string" } ,
496
+ ) ;
497
+
478
498
export const BlobResourceContentsSchema = ResourceContentsSchema . extend ( {
479
499
/**
480
500
* A base64-encoded string representing the binary data of the item.
481
501
*/
482
- blob : z . string ( ) . base64 ( ) ,
502
+ blob : Base64Schema ,
483
503
} ) ;
484
504
485
505
/**
@@ -735,7 +755,7 @@ export const ImageContentSchema = z
735
755
/**
736
756
* The base64-encoded image data.
737
757
*/
738
- data : z . string ( ) . base64 ( ) ,
758
+ data : Base64Schema ,
739
759
/**
740
760
* The MIME type of the image. Different providers may support different image types.
741
761
*/
@@ -758,7 +778,7 @@ export const AudioContentSchema = z
758
778
/**
759
779
* The base64-encoded audio data.
760
780
*/
761
- data : z . string ( ) . base64 ( ) ,
781
+ data : Base64Schema ,
762
782
/**
763
783
* The MIME type of the audio. Different providers may support different audio types.
764
784
*/
@@ -911,7 +931,7 @@ export const ToolSchema = BaseMetadataSchema.extend({
911
931
} )
912
932
. passthrough ( ) ,
913
933
/**
914
- * An optional JSON Schema object defining the structure of the tool's output returned in
934
+ * An optional JSON Schema object defining the structure of the tool's output returned in
915
935
* the structuredContent field of a CallToolResult.
916
936
*/
917
937
outputSchema : z . optional (
0 commit comments