fix(mcpserver): normalize Image and Audio format to registered MIME types (#3585)

Closed
MU
1mo ago
+114-19

Summary

Closes #3585.

Currently, Image and Audio compute MIME types inconsistently depending on whether path= or format= is passed:

  • With path=, a suffix table is used (.jpg -> image/jpeg, .mp3 -> audio/mpeg, .m4a -> audio/mp4).
  • With format=, the code naively interpolated f"image/{format}" / f"audio/{format}". Passing format="jpg", format="mp3", or format="m4a" resulted in unregistered MIME types (image/jpg, audio/mp3, audio/m4a), which fails validation on downstream consumers.

Solution

  • Defined IMAGE_MIME_TYPES and AUDIO_MIME_TYPES mapping tables.
  • Route format= through the same table as path=, falling back to f"image/{fmt}" / f"audio/{fmt}" for formats outside the table (e.g. bmp, opus).
  • Normalized formats with .lower().lstrip(".") for consistent matching.
  • Added comprehensive unit tests in tests/server/mcpserver/test_media_types.py covering format and path MIME type resolution, defaults, and fallbacks with 100% statement and branch coverage.

Disclosure: Implemented with AI assistance under human review.