mastra-ai/mastra · error

Invalid \`mediaTypes\` pattern: ${JSON.stringify(pattern)}.

Error message

Invalid \`mediaTypes\` pattern: ${JSON.stringify(pattern)}. Expected \`*\`, \`*/*\`, \`type/*\`, or a full mime type like \`application/pdf\`.

What it means

The `readFile` tool accepts a `mediaTypes` allowlist to filter binary file reads. Each pattern must be `*`, `*/*`, `type/*` (e.g. `image/*`), or a full mime type like `application/pdf`. `validateMediaTypePatterns` runs each entry against `MEDIA_TYPE_PATTERN` and throws a descriptive Error for the first invalid one.

Source

Thrown at packages/core/src/workspace/tools/read-file.ts:64

  if (mimeType.endsWith('+json') || mimeType.endsWith('+xml')) return true;
  return false;
}

/**
 * Validates a single `mediaTypes` pattern. Accepts:
 * - `*` or `*​/*` — match anything
 * - `type/*` — match all subtypes of a top-level type (e.g. `image/*`)
 * - `type/subtype` — exact mime type (e.g. `application/pdf`, `application/vnd.api+json`)
 *
 * Throws a descriptive error for anything else so misconfigurations surface
 * immediately instead of silently failing to match.
 */
const MEDIA_TYPE_PATTERN = /^(?:\*|\*\/\*|[a-z0-9!#$&^_.+-]+\/(?:\*|[a-z0-9!#$&^_.+-]+))$/i;

function validateMediaTypePatterns(patterns: string[]): void {
  for (const pattern of patterns) {
    if (typeof pattern !== 'string' || !MEDIA_TYPE_PATTERN.test(pattern)) {
      throw new Error(
        `Invalid \`mediaTypes\` pattern: ${JSON.stringify(pattern)}. Expected \`*\`, \`*/*\`, \`type/*\`, or a full mime type like \`application/pdf\`.`,
      );
    }
  }
}

/**
 * Build a predicate from the `mediaTypes` config option.
 * Supports glob patterns (e.g. `'image/*'`), custom functions, and `false`
 * to disable media parts entirely.
 */
function buildMediaTypeCheck(
  config: string[] | ((mimeType: string) => boolean) | false | undefined,
): (mimeType: string | undefined) => boolean {
  if (config === false) return () => false;
  if (typeof config === 'function') {
    return (mimeType: string | undefined) => (mimeType ? config(mimeType) : false);
  }

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Correct the pattern to one of the accepted forms: `*`, `*/*`, `type/*`, or `type/subtype`
  2. Replace extension-based filters (`*.pdf`) with mime-based ones (`application/pdf`)
  3. Validate the array contents (all strings, non-empty) before passing to the tool

Example fix

// before
readFile(path, { mediaTypes: ['*.pdf', 'pdf'] })

// after
readFile(path, { mediaTypes: ['application/pdf'] })
Defensive patterns

Strategy: validation

Validate before calling

const MEDIA_TYPE_RE = /^(?:\*|\*\/\*|[a-z0-9!#$&^_.+-]+\/(?:\*|[a-z0-9!#$&^_.+-]+))$/i;
for (const p of mediaTypes ?? []) {
  if (typeof p !== 'string' || !MEDIA_TYPE_RE.test(p)) throw new Error(`Invalid mediaTypes pattern: ${p}`);
}

Type guard

function isValidMediaTypePattern(p) {
  return typeof p === 'string' && /^(?:\*|\*\/\*|[a-z0-9!#$&^_.+-]+\/(?:\*|[a-z0-9!#$&^_.+-]+))$/i.test(p);
}

Try / catch

try {
  return await readFileTool.execute({ path, mediaTypes }, ctx);
} catch (err) {
  if (err instanceof Error && err.message.startsWith('Invalid `mediaTypes` pattern')) {
    return { error: 'Fix mediaTypes pattern: use *, */*, type/*, or type/subtype.' };
  }
  throw err;
}

Prevention

When it happens

Trigger: Passing `mediaTypes` entries that fail the regex: empty strings, `text`, `*pdf`, `application/`, mime types with invalid characters, or non-string values inside the array.

Common situations: Typos like `image/jpg` misspellings are fine but `pdf` or `application-pdf` are not; copying extension-style filters (`*.pdf`) instead of mime patterns; programmatically building patterns and including null/undefined.

Related errors


AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30). Data as JSON: /api/errors/62778ea61676b159. Report an issue: GitHub.