Skip to content

πŸš€ The fast, Pythonic way to build MCP servers and clients.

Map common audio extensions when Audio is given a path (#5919)

Asem ahmed60782efa0ad0
diff --git a/fastmcp_slim/fastmcp/utilities/types.py b/fastmcp_slim/fastmcp/utilities/types.py
index bc0445504..ad36c81f9 100644
--- a/fastmcp_slim/fastmcp/utilities/types.py
+++ b/fastmcp_slim/fastmcp/utilities/types.py
@@ -372,6 +372,11 @@ class Audio:
"ogg": "audio/ogg",
"m4a": "audio/mp4",
"flac": "audio/flac",
+ "opus": "audio/ogg",
+ "aac": "audio/aac",
+ "webm": "audio/webm",
+ "aiff": "audio/aiff",
+ "aif": "audio/aiff",
}
if self._format:
diff --git a/tests/utilities/test_types.py b/tests/utilities/test_types.py
index b3234921f..e28726d56 100644
--- a/tests/utilities/test_types.py
+++ b/tests/utilities/test_types.py
@@ -6,6 +6,7 @@ import pytest
from mcp_types import BlobResourceContents, TextResourceContents
from pydantic import Field
+from fastmcp import Client, FastMCP
from fastmcp.utilities.types import (
Audio,
File,
@@ -348,6 +349,46 @@ class TestAudio:
audio = Audio(path=path)
assert audio._mime_type == mime
+ @pytest.mark.parametrize(
+ "extension,mime_type",
+ [
+ ("opus", "audio/ogg"),
+ ("aac", "audio/aac"),
+ ("webm", "audio/webm"),
+ ("aiff", "audio/aiff"),
+ ("aif", "audio/aiff"),
+ ],
+ )
+ @pytest.mark.parametrize("uppercase", [False, True])
+ async def test_path_and_format_agree_on_mime_type(
+ self, tmp_path, extension, mime_type, uppercase
+ ):
+ """A file extension maps to the same MIME type as the matching format."""
+ if uppercase:
+ extension = extension.upper()
+ path = tmp_path / f"test.{extension}"
+ path.write_bytes(b"fake audio data")
+
+ from_path = Audio(path=path)
+ from_format = Audio(data=b"test", format=extension)
+
+ assert from_path._mime_type == mime_type
+ assert from_format._mime_type == mime_type
+ assert from_path.to_audio_content().mime_type == mime_type
+
+ server = FastMCP()
+
+ @server.tool
+ def audio() -> Audio:
+ return from_path
+
+ async with Client(server) as client:
+ result = await client.call_tool("audio")
+
+ assert result.content[0].type == "audio"
+ assert result.content[0].mime_type == mime_type
+ assert base64.b64decode(result.content[0].data) == path.read_bytes()
+
def test_to_audio_content(self, tmp_path, monkeypatch):
"""Test conversion to AudioContent."""
# Test with path