python-sdk icon indicating copy to clipboard operation
python-sdk copied to clipboard

Audio(data=b"") raises "Either path or data can be provided", while Image(data=b"") works

Open xThreeh opened this issue 1 month ago • 2 comments

Initial Checks

  • [x] I confirm that I'm using the newest release of my line (the latest 2.x, or the latest 1.x if I'm still on v1)
  • [x] I confirm that I searched for my issue in https://github.com/modelcontextprotocol/python-sdk/issues before opening this issue

Release line

2.x

Description

mcp.server.mcpserver.utilities.types.Audio rejects an empty byte payload that Image accepts. Audio.__init__ checks if not bool(path) ^ bool(data), so data=b"" is treated as missing and raises ValueError("Either path or data can be provided"). Image.__init__ checks path is None and data is None / path is not None and data is not None, so Image(data=b"") builds content with data="".

Empty bytes are a valid payload here: the schema's AudioContent.data is a base64 string with no minimum length, and base64.b64encode(b"") is "". Audio.to_audio_content() itself already uses self.data is not None. The truthiness check also produces the same misleading message when both path and data are passed.

Using the same two is None checks as Image would fix both. I'm happy to send that change with a test if you'd like an outside PR for it.

Example Code

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

print(Image(data=b"").to_image_content().data)  # ''
Audio(data=b"")  # ValueError: Either path or data can be provided

Python & MCP Python SDK

Python 3.13, Windows 11; main at 91941ed (src/mcp/server/mcpserver/utilities/types.py:66)

xThreeh avatar Oct 08 '26 04:10 xThreeh

Hi, I'd like to fix this. Prepared change on KaiyiQuan:fix/3656-audio-empty-data (PR https://github.com/modelcontextprotocol/python-sdk/pull/3657): Audio's validation aligned with Image's (None-based checks), so Audio(data=b"") is accepted while the two error paths are preserved. 6 new unit tests pass. Could you assign this issue to me so the PR can be reviewed? Thanks!

KaiyiQuan avatar Oct 08 '26 15:10 KaiyiQuan

Hi! I reproduced this: Audio.init validates with bool(path) ^ bool(data), and because b"" is falsy, Audio(data=b"") is rejected even though to_audio_content() already handles empty data correctly.

I have a small fix ready that mirrors Image.init (is None checks, with separate errors for "neither" and "both"). It also removes the stale "# pragma: no cover" and adds tests. The full suite passes at 100% coverage, and ruff and pyright are clean. Could I be assigned to open the PR? Happy to defer to the reporter if they'd like to resubmit.

(I worked on this with help from Cursor and Grok Bot.)

ajay206 avatar Oct 12 '26 10:10 ajay206