Skip to content

Commit df4e3e6

Browse files
committed
fix(server): accept empty bytes data in Audio like Image
Audio.__init__ validated with `not bool(path) ^ bool(data)`, treating `data=b""` as missing and raising ValueError, while Image accepts an empty byte payload (`Image(data=b"")` works). Both helpers should treat only None as absent. Align Audio's validation with Image's (None-based checks, same error messages). Fixes #3656
1 parent 91941ed commit df4e3e6

2 files changed

Lines changed: 40 additions & 2 deletions

File tree

‎src/mcp/server/mcpserver/utilities/types.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,10 @@ def __init__(
6363
data: bytes | None = None,
6464
format: str | None = None,
6565
):
66-
if not bool(path) ^ bool(data): # pragma: no cover
67-
raise ValueError("Either path or data can be provided")
66+
if path is None and data is None: # pragma: no cover
67+
raise ValueError("Either path or data must be provided")
68+
if path is not None and data is not None: # pragma: no cover
69+
raise ValueError("Only one of path or data can be provided")
6870

6971
self.path = Path(path) if path else None
7072
self.data = data
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""Audio/Image media helper validation (#3656)."""
2+
3+
import pytest
4+
5+
from mcp.server.mcpserver.utilities.types import Audio, Image
6+
7+
8+
class TestAudioDataValidation:
9+
def test_audio_accepts_empty_bytes_payload(self):
10+
audio = Audio(data=b"")
11+
assert audio.data == b""
12+
assert audio.path is None
13+
14+
def test_audio_accepts_nonempty_bytes_payload(self):
15+
audio = Audio(data=b"\x00\x01")
16+
assert audio.data == b"\x00\x01"
17+
18+
def test_audio_accepts_path_only(self):
19+
audio = Audio(path="/tmp/x.wav")
20+
assert audio.path is not None
21+
assert audio.data is None
22+
23+
def test_audio_raises_when_nothing_provided(self):
24+
with pytest.raises(ValueError):
25+
Audio()
26+
27+
def test_audio_raises_when_both_provided(self):
28+
with pytest.raises(ValueError):
29+
Audio(path="/tmp/x.wav", data=b"")
30+
31+
32+
class TestImageDataValidation:
33+
def test_image_accepts_empty_bytes_payload(self):
34+
image = Image(data=b"")
35+
assert image.data == b""
36+
assert image.path is None

0 commit comments

Comments
 (0)