@@ -495,11 +495,31 @@ export const TextResourceContentsSchema = ResourceContentsSchema.extend({
495
495
text : z . string ( ) ,
496
496
} ) ;
497
497
498
+
499
+ /**
500
+ * A Zod schema for validating Base64 strings that is more performant and
501
+ * robust for very large inputs than the default regex-based check. It avoids
502
+ * stack overflows by using the native `atob` function for validation.
503
+ */
504
+ const Base64Schema = z . string ( ) . refine (
505
+ ( val ) => {
506
+ try {
507
+ // atob throws a DOMException if the string contains characters
508
+ // that are not part of the Base64 character set.
509
+ atob ( val ) ;
510
+ return true ;
511
+ } catch {
512
+ return false ;
513
+ }
514
+ } ,
515
+ { message : "Invalid Base64 string" } ,
516
+ ) ;
517
+
498
518
export const BlobResourceContentsSchema = ResourceContentsSchema . extend ( {
499
519
/**
500
520
* A base64-encoded string representing the binary data of the item.
501
521
*/
502
- blob : z . string ( ) . base64 ( ) ,
522
+ blob : Base64Schema ,
503
523
} ) ;
504
524
505
525
/**
@@ -755,7 +775,7 @@ export const ImageContentSchema = z
755
775
/**
756
776
* The base64-encoded image data.
757
777
*/
758
- data : z . string ( ) . base64 ( ) ,
778
+ data : Base64Schema ,
759
779
/**
760
780
* The MIME type of the image. Different providers may support different image types.
761
781
*/
@@ -778,7 +798,7 @@ export const AudioContentSchema = z
778
798
/**
779
799
* The base64-encoded audio data.
780
800
*/
781
- data : z . string ( ) . base64 ( ) ,
801
+ data : Base64Schema ,
782
802
/**
783
803
* The MIME type of the audio. Different providers may support different audio types.
784
804
*/
@@ -931,7 +951,7 @@ export const ToolSchema = BaseMetadataSchema.extend({
931
951
} )
932
952
. strip ( ) ,
933
953
/**
934
- * An optional JSON Schema object defining the structure of the tool's output returned in
954
+ * An optional JSON Schema object defining the structure of the tool's output returned in
935
955
* the structuredContent field of a CallToolResult.
936
956
*/
937
957
outputSchema : z . optional (
0 commit comments