@@ -495,11 +495,31 @@ export const TextResourceContentsSchema = ResourceContentsSchema.extend({
495495 text : z . string ( ) ,
496496} ) ;
497497
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+
498518export const BlobResourceContentsSchema = ResourceContentsSchema . extend ( {
499519 /**
500520 * A base64-encoded string representing the binary data of the item.
501521 */
502- blob : z . string ( ) . base64 ( ) ,
522+ blob : Base64Schema ,
503523} ) ;
504524
505525/**
@@ -755,7 +775,7 @@ export const ImageContentSchema = z
755775 /**
756776 * The base64-encoded image data.
757777 */
758- data : z . string ( ) . base64 ( ) ,
778+ data : Base64Schema ,
759779 /**
760780 * The MIME type of the image. Different providers may support different image types.
761781 */
@@ -778,7 +798,7 @@ export const AudioContentSchema = z
778798 /**
779799 * The base64-encoded audio data.
780800 */
781- data : z . string ( ) . base64 ( ) ,
801+ data : Base64Schema ,
782802 /**
783803 * The MIME type of the audio. Different providers may support different audio types.
784804 */
@@ -931,7 +951,7 @@ export const ToolSchema = BaseMetadataSchema.extend({
931951 } )
932952 . strip ( ) ,
933953 /**
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
935955 * the structuredContent field of a CallToolResult.
936956 */
937957 outputSchema : z . optional (
0 commit comments