Skip to content

Add more robust base64 check #786

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

cliffhall
Copy link
Member

@cliffhall cliffhall commented Jul 19, 2025

Description

Adds a more robust base64 check for AudioContent, ImageContent and BlobResourceContent. This fixes an issue discovered while creating this PR in the servers repo: modelcontextprotocol/servers#2382

Motivation and Context

  • Essentially any tool call result with a large base64 data field would fail with a "Maximum call stack size exceeded" error.
  • Please read the Additional Context section at the bottom of that PR for the process of elimination that isolated this as the problem.

The problem and the fix

  • Under the hood, z.string().base64() uses a regular expression to validate the string.
  • This is used in the schemas for AudioContent, ImageContent and BlobResourceContent.
  • While this regex is fine for typical inputs, running it against a string that is several megabytes long can cause the JavaScript engine's regex parser to hit its internal recursion limit, resulting in a "Maximum call stack size exceeded" error.
  • Replaced use of z.string().base64() with a new Base64Schema
const Base64Schema = z.string().refine(
  (val) => {
    try {
      // atob throws a DOMException if the string contains characters
      // that are not part of the Base64 character set.
      atob(val);
      return true;
    } catch (e) {
      return false;
    }
  },
  { message: "Invalid Base64 string" },
);

How Has This Been Tested?

In the Inspector CLI and UI, tested the large file that was causing the "Maximum call stack size exceeded" error. It is no longer present and the 4mb file can be processed.

Inspector CLI

Screenshot 2025-07-18 at 8 28 42 PM

Inspector UI

Screenshot 2025-07-18 at 8 27 55 PM

Breaking Changes

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

…obResourceContent.

* This fixes an issue found in the servers repo with this PR: modelcontextprotocol/servers#2382

* Under the hood, z.string().base64() uses a regular expression to validate the string.

* While this regex is fine for typical inputs, running it against a string that is several megabytes long can cause the JavaScript engine's regex parser to hit its internal recursion limit, resulting in a "Maximum call stack size exceeded" error.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant