Skip to content

Conversation

@arvinxx
Copy link
Member

@arvinxx arvinxx commented Jan 5, 2026

Fixes the "Invalid image url" error when MCP returns audio content by bypassing image-only file type validation for audio files, following the same pattern as TTS uploads.

Changes

  • Add inferContentTypeFromUrl utility function that supports both image and audio files
  • Modify FileService.uploadBase64 to accept skipCheckFileType parameter
  • Update MCP content processor to use skipCheckFileType for audio files
  • Add comprehensive tests for new utility function

Closes #10778

Generated with Claude Code

Summary by Sourcery

Support MCP audio file uploads by relaxing image-only content type validation and improving MIME type inference from URLs.

New Features:

  • Introduce a generic inferContentTypeFromUrl utility that infers MIME types for both image and audio file URLs.

Bug Fixes:

  • Fix MCP audio uploads failing with image-only file type validation by allowing audio MIME types during file upload.

Enhancements:

  • Extend FileService.uploadBase64 to accept options for skipping strict image file type checks when appropriate.
  • Update MCP content processing to use the new audio-safe upload path for audio content blocks.

Tests:

  • Add comprehensive unit tests for inferContentTypeFromUrl covering images, audio, case insensitivity, query parameters, fragments, unknown extensions, and relative paths.

- Add inferContentTypeFromUrl utility function that supports both image and audio files
- Modify FileService.uploadBase64 to accept skipCheckFileType parameter
- Update MCP content processor to use skipCheckFileType for audio files
- Add comprehensive tests for new utility function

Fixes the "Invalid image url" error when MCP returns audio content by
bypassing image-only file type validation for audio files, following
the same pattern as TTS uploads.

Fixes #10778

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-authored-by: Arvin Xu <arvinxx@users.noreply.github.com>
@vercel
Copy link

vercel bot commented Jan 5, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Review Updated (UTC)
lobehub Ready Ready Preview, Comment Jan 5, 2026 3:48am

@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Jan 5, 2026

Reviewer's Guide

Adds a generic URL-based content-type inference utility, updates file upload logic to optionally bypass image-only validation for audio (used by MCP audio uploads), and introduces tests to cover the new MIME inference behavior for images and audio.

Sequence diagram for MCP audio upload content processing

sequenceDiagram
  participant MCPContentProcessor
  participant FileService
  participant UrlUtils

  MCPContentProcessor->>FileService: uploadBase64(audioData, audioPathname, options_skipCheckFileType_true)
  activate FileService

  alt skipCheckFileType_true
    FileService->>UrlUtils: inferContentTypeFromUrl(audioPathname)
    UrlUtils-->>FileService: audio_mime_type
  else skipCheckFileType_false
    FileService->>UrlUtils: inferContentTypeFromImageUrl(pathname)
    UrlUtils-->>FileService: image_mime_type
  end

  FileService-->>MCPContentProcessor: url_with_uploaded_audio
  deactivate FileService
Loading

Updated class diagram for FileService uploadBase64 and URL MIME utilities

classDiagram
  class FileService {
    +uploadBase64(base64Data: string, pathname: string, options: UploadBase64Options): Promise_FileUploadResult
  }

  class UploadBase64Options {
    +skipCheckFileType: boolean
  }

  class Promise_FileUploadResult {
    +fileId: string
    +key: string
    +url: string
  }

  class UrlUtils {
    +inferContentTypeFromImageUrl(url: string): string
    +inferContentTypeFromUrl(url: string): string
  }

  FileService --> UploadBase64Options : uses
  FileService --> Promise_FileUploadResult : returns
  FileService --> UrlUtils : calls
Loading

File-Level Changes

Change Details Files
Introduce a generic utility to infer MIME type from URLs supporting both images and audio, with comprehensive test coverage.
  • Add inferContentTypeFromUrl that parses a (possibly relative) URL, extracts the extension, lowercases it, and resolves a MIME type via the mime library, falling back to application/octet-stream when unknown or missing.
  • Use URL parsing with a fixed base to robustly handle absolute URLs, relative paths, and URLs with query strings or fragments when deriving the pathname and extension.
  • Add tests that verify correct MIME inference for common image and audio extensions, case-insensitive extensions, URLs containing query parameters and fragments, unknown/no extension fallbacks, and relative path handling.
packages/utils/src/url.ts
packages/utils/src/url.test.ts
Extend FileService base64 upload to optionally skip image-only file-type validation and instead use the new generic content-type inference.
  • Update FileService.uploadBase64 signature to accept an optional options object with skipCheckFileType, defaulting to false.
  • When skipCheckFileType is true, compute fileType with inferContentTypeFromUrl(pathname); otherwise, retain existing inferContentTypeFromImageUrl behavior with application/octet-stream fallback.
  • Preserve existing metadata calculations (size, hash, etc.) while allowing more flexible MIME typing for non-image uploads.
src/server/services/file/index.ts
Use the relaxed file-type validation path for MCP audio uploads to prevent "Invalid image url" errors.
  • Update MCP content processor to pass { skipCheckFileType: true } when uploading base64 audio blocks via FileService.uploadBase64.
  • Keep the existing audio path construction (S3 path under mcp/audio with a nanoid and provided extension) while relying on the new generic MIME inference for the stored audio files.
src/server/services/mcp/contentProcessor.ts

Assessment against linked issues

Issue Objective Addressed Explanation
#10778 Fix the error that occurs when MCP returns content with type "audio" by allowing such audio files to be uploaded without failing image-only URL validation.
#10778 Ensure audio files returned from MCP are assigned an appropriate MIME type based on their URL or path when uploaded (e.g., .mp3 → audio/mp3).

Possibly linked issues

  • #unknown: PR changes MCP file handling to infer audio MIME types and skip image-only validation, fixing the reported error

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@codecov
Copy link

codecov bot commented Jan 5, 2026

Codecov Report

❌ Patch coverage is 16.66667% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 74.63%. Comparing base (82980a7) to head (9663d4a).
⚠️ Report is 7 commits behind head on next.

Additional details and impacted files
@@            Coverage Diff             @@
##             next   #11223      +/-   ##
==========================================
- Coverage   77.57%   74.63%   -2.94%     
==========================================
  Files        1118      887     -231     
  Lines       84636    67969   -16667     
  Branches    11224     7265    -3959     
==========================================
- Hits        65656    50730   -14926     
+ Misses      18980    17239    -1741     
Flag Coverage Δ
app 70.26% <16.66%> (+0.01%) ⬆️
database 94.36% <ø> (ø)
packages/agent-runtime 90.26% <ø> (ø)
packages/context-engine ?
packages/conversation-flow 95.79% <ø> (ø)
packages/electron-server-ipc 93.76% <ø> (ø)
packages/file-loaders 92.20% <ø> (ø)
packages/model-bank 100.00% <ø> (ø)
packages/model-runtime ?
packages/prompts 77.55% <ø> (ø)
packages/python-interpreter 96.50% <ø> (ø)
packages/utils ?
packages/web-crawler 96.81% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
Store 69.60% <ø> (ø)
Services 52.49% <ø> (ø)
Server 73.05% <16.66%> (-0.02%) ⬇️
Libs 42.22% <ø> (+0.67%) ⬆️
Utils 92.89% <ø> (-0.17%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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.

mcp 返回 audio 类型时报错 Invalid image url

1 participant