{"record":{"id":"62778ea61676b159","repo":"mastra-ai/mastra","slug":"invalid-mediatypes-pattern-json-stringify-p","errorCode":null,"errorMessage":"Invalid \\`mediaTypes\\` pattern: ${JSON.stringify(pattern)}. Expected \\`*\\`, \\`*/*\\`, \\`type/*\\`, or a full mime type like \\`application/pdf\\`.","messagePattern":"Invalid \\\\`mediaTypes\\\\` pattern: (.+?)\\. Expected \\\\`\\*\\\\`, \\\\`\\*/\\*\\\\`, \\\\`type/\\*\\\\`, or a full mime type like \\\\`application/pdf\\\\`\\.","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/core/src/workspace/tools/read-file.ts","lineNumber":64,"sourceCode":"  if (mimeType.endsWith('+json') || mimeType.endsWith('+xml')) return true;\n  return false;\n}\n\n/**\n * Validates a single `mediaTypes` pattern. Accepts:\n * - `*` or `*​/*` — match anything\n * - `type/*` — match all subtypes of a top-level type (e.g. `image/*`)\n * - `type/subtype` — exact mime type (e.g. `application/pdf`, `application/vnd.api+json`)\n *\n * Throws a descriptive error for anything else so misconfigurations surface\n * immediately instead of silently failing to match.\n */\nconst MEDIA_TYPE_PATTERN = /^(?:\\*|\\*\\/\\*|[a-z0-9!#$&^_.+-]+\\/(?:\\*|[a-z0-9!#$&^_.+-]+))$/i;\n\nfunction validateMediaTypePatterns(patterns: string[]): void {\n  for (const pattern of patterns) {\n    if (typeof pattern !== 'string' || !MEDIA_TYPE_PATTERN.test(pattern)) {\n      throw new Error(\n        `Invalid \\`mediaTypes\\` pattern: ${JSON.stringify(pattern)}. Expected \\`*\\`, \\`*/*\\`, \\`type/*\\`, or a full mime type like \\`application/pdf\\`.`,\n      );\n    }\n  }\n}\n\n/**\n * Build a predicate from the `mediaTypes` config option.\n * Supports glob patterns (e.g. `'image/*'`), custom functions, and `false`\n * to disable media parts entirely.\n */\nfunction buildMediaTypeCheck(\n  config: string[] | ((mimeType: string) => boolean) | false | undefined,\n): (mimeType: string | undefined) => boolean {\n  if (config === false) return () => false;\n  if (typeof config === 'function') {\n    return (mimeType: string | undefined) => (mimeType ? config(mimeType) : false);\n  }","sourceCodeStart":46,"sourceCodeEnd":82,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/core/src/workspace/tools/read-file.ts#L46-L82","documentation":"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.","triggerScenarios":"Passing `mediaTypes` entries that fail the regex: empty strings, `text`, `*pdf`, `application/`, mime types with invalid characters, or non-string values inside the array.","commonSituations":"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.","solutions":["Correct the pattern to one of the accepted forms: `*`, `*/*`, `type/*`, or `type/subtype`","Replace extension-based filters (`*.pdf`) with mime-based ones (`application/pdf`)","Validate the array contents (all strings, non-empty) before passing to the tool"],"exampleFix":"// before\nreadFile(path, { mediaTypes: ['*.pdf', 'pdf'] })\n\n// after\nreadFile(path, { mediaTypes: ['application/pdf'] })","handlingStrategy":"validation","validationCode":"const MEDIA_TYPE_RE = /^(?:\\*|\\*\\/\\*|[a-z0-9!#$&^_.+-]+\\/(?:\\*|[a-z0-9!#$&^_.+-]+))$/i;\nfor (const p of mediaTypes ?? []) {\n  if (typeof p !== 'string' || !MEDIA_TYPE_RE.test(p)) throw new Error(`Invalid mediaTypes pattern: ${p}`);\n}","typeGuard":"function isValidMediaTypePattern(p) {\n  return typeof p === 'string' && /^(?:\\*|\\*\\/\\*|[a-z0-9!#$&^_.+-]+\\/(?:\\*|[a-z0-9!#$&^_.+-]+))$/i.test(p);\n}","tryCatchPattern":"try {\n  return await readFileTool.execute({ path, mediaTypes }, ctx);\n} catch (err) {\n  if (err instanceof Error && err.message.startsWith('Invalid `mediaTypes` pattern')) {\n    return { error: 'Fix mediaTypes pattern: use *, */*, type/*, or type/subtype.' };\n  }\n  throw err;\n}","preventionTips":["Keep accepted-form examples (*, */*, image/*, application/pdf) next to the config","Never build media filters from file extensions; map extensions to mime types first","Validate config objects with a schema (zod .regex()) before passing to tools"],"tags":["validation","mime-type","media-types","input-validation"],"backgroundTag":"invalid-pattern-format","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}