Repository navigation
Description
What happened?
Follow-up to #3829: its fix gave bare bytes returns a base64 fallback and suppressed their output schema, but both protections stop at the top level. On 4.1.0 and current main, a tool returning the same non-UTF-8 bytes nested inside a container fails the whole tools/call, while returning them bare succeeds as base64.
Two gaps on main:
default_serializer(fastmcp/tools/base.py:73) raisesPydanticSerializationError: Error serializing to JSON: invalid utf-8 sequence ...for non-UTF-8 bytes nested anywhere in the value, and its call site_convert_to_content(base.py:390) runs before thetry/exceptthat guards structured-content serialization (base.py:413), so the error escapesconvert_resultand the entire call fails._contains_bytes_type(fastmcp/tools/function_parsing.py:34) only recurses throughUnion/UnionType/Annotated, sodict[str, bytes]andlist[bytes]tools still advertise an output schema that runtime serialization cannot always fill — the schema bare-bytestools already suppress (function_parsing.py:400-402).
Expected: nested bytes should get the same base64 fallback a bare bytes result already has, and return annotations containing bytes should suppress the output schema like bare bytes does.
Output of the MRE below, on fastmcp 4.1.0:
blob_bare: ok -> //4gaW52YWxpZCB1dGYtOA==
blob_nested: FAILS -> ToolError: Error calling tool 'blob_nested': Error serializing to JSON: invalid utf-8 sequence of 1 bytes from index 0
blob_list: FAILS -> ToolError: Error calling tool 'blob_list': Error serializing to JSON: invalid utf-8 sequence of 1 bytes from index 0
blob_nested: advertises outputSchema -> True
blob_list: advertises outputSchema -> True
Example Code
import asyncio
import warnings
from fastmcp import FastMCP
from fastmcp.client import Client
mcp = FastMCP()
@mcp.tool
def blob_bare() -> bytes:
return b"\xff\xfe invalid utf-8"
@mcp.tool
def blob_nested() -> dict[str, bytes]:
return {"data": b"\xff\xfe invalid utf-8"}
@mcp.tool
def blob_list() -> list[bytes]:
return [b"\xff\xfe invalid utf-8"]
async def main():
async with Client(mcp) as c:
for name in ("blob_bare", "blob_nested", "blob_list"):
try:
r = await c.call_tool(name, {})
print(f"{name}: ok -> {r.content[0].text[:60]}")
except Exception as e:
print(f"{name}: FAILS -> {type(e).__name__}: {e}")
for t in await c.list_tools():
if t.name != "blob_bare":
print(f"{t.name}: advertises outputSchema -> {t.outputSchema is not None}")
warnings.filterwarnings("ignore")
asyncio.run(main())Version Information
fastmcp 4.1.0 (mcp 2.3.0 / starlette 1.7.0)
Python 3.13.15
macOS 15 (Darwin 25.6.0, arm64)
Fix with tests is ready in #5577 — it was originally linked to #3829 (closed), whose bare-bytes scope it extends.
Report prepared with LLM assistance; the MRE and the output above are verbatim from my runs against the versions cited above.
Activity