Skip to content

Commit 7012f5d

Browse files
committed
Merge branch 'main' into ihrpr/zod-types
2 parents 33f69eb + c7887c0 commit 7012f5d

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

src/types.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -475,11 +475,31 @@ export const TextResourceContentsSchema = ResourceContentsSchema.extend({
475475
text: z.string(),
476476
});
477477

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+
478498
export const BlobResourceContentsSchema = ResourceContentsSchema.extend({
479499
/**
480500
* A base64-encoded string representing the binary data of the item.
481501
*/
482-
blob: z.string().base64(),
502+
blob: Base64Schema,
483503
});
484504

485505
/**
@@ -735,7 +755,7 @@ export const ImageContentSchema = z
735755
/**
736756
* The base64-encoded image data.
737757
*/
738-
data: z.string().base64(),
758+
data: Base64Schema,
739759
/**
740760
* The MIME type of the image. Different providers may support different image types.
741761
*/
@@ -758,7 +778,7 @@ export const AudioContentSchema = z
758778
/**
759779
* The base64-encoded audio data.
760780
*/
761-
data: z.string().base64(),
781+
data: Base64Schema,
762782
/**
763783
* The MIME type of the audio. Different providers may support different audio types.
764784
*/
@@ -911,7 +931,7 @@ export const ToolSchema = BaseMetadataSchema.extend({
911931
})
912932
.passthrough(),
913933
/**
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
915935
* the structuredContent field of a CallToolResult.
916936
*/
917937
outputSchema: z.optional(

0 commit comments

Comments
 (0)