Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/mcp/server/mcpserver/utilities/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,10 @@ def __init__(
data: bytes | None = None,
format: str | None = None,
):
if not bool(path) ^ bool(data): # pragma: no cover
raise ValueError("Either path or data can be provided")
if path is None and data is None: # pragma: no cover
raise ValueError("Either path or data must be provided")
if path is not None and data is not None: # pragma: no cover
raise ValueError("Only one of path or data can be provided")

self.path = Path(path) if path else None
self.data = data
Expand Down
36 changes: 36 additions & 0 deletions tests/server/mcpserver/utilities/test_media_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""Audio/Image media helper validation (#3656)."""

import pytest

from mcp.server.mcpserver.utilities.types import Audio, Image


class TestAudioDataValidation:
def test_audio_accepts_empty_bytes_payload(self):
audio = Audio(data=b"")
assert audio.data == b""
assert audio.path is None

def test_audio_accepts_nonempty_bytes_payload(self):
audio = Audio(data=b"\x00\x01")
assert audio.data == b"\x00\x01"

def test_audio_accepts_path_only(self):
audio = Audio(path="/tmp/x.wav")
assert audio.path is not None
assert audio.data is None

def test_audio_raises_when_nothing_provided(self):
with pytest.raises(ValueError):
Audio()

def test_audio_raises_when_both_provided(self):
with pytest.raises(ValueError):
Audio(path="/tmp/x.wav", data=b"")


class TestImageDataValidation:
def test_image_accepts_empty_bytes_payload(self):
image = Image(data=b"")
assert image.data == b""
assert image.path is None
Loading