fix(events): never drop events on non-JSON-serializable payloads#40
Merged
Conversation
Events whose payload held a value that is not JSON-serializable were being
silently lost. Two failure modes with a single root cause:
- Truncation aborted with "Unable to serialize unknown type" because
truncate_event serializes the event with pydantic's model_dump_json(), which
raises on values such as a raw callable.
- The generated API client then failed to send the event ("'set' object has no
attribute '__dict__'") and, after three identical retries, dropped it.
Both originated from the FastMCP v3 middleware capturing a tools/list response
via Tool.model_dump(), which includes SDK-internal fields that are not
JSON-serializable: a callable "fn" (FunctionTool) and a "tags" set.
Fixes, in two layers:
- Source: _tool_to_dict now emits the canonical MCP shape via to_mcp_tool()
(name/description/inputSchema/...), so the tools/list payload is clean and
free of internal callables and sets.
- Systemic guarantee: truncate_event now coerces event payload fields to
JSON-safe primitives up front (callables -> repr, sets -> lists, datetimes ->
ISO strings, unknown -> str) via a new make_json_safe helper, and passes
fallback=str to its size-check serialization. Because the API client
re-serializes the same event object on send, this prevents any
non-serializable value in a payload from dropping an event.
Adds unit coverage for truncation JSON-safety and an end-to-end community
FastMCP v3 test asserting a tools/list event is serializable and survives the
API client's serialization. Verified on FastMCP 3.0.0b1 and 3.4.4.
7811615 to
a986388
Compare
kashishhora
approved these changes
Jul 10, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Unable to serialize unknown type: <class 'function'>—truncate_eventserializes the event with pydantic'smodel_dump_json(), which raises on a raw callable.'set' object has no attribute '__dict__') and, after three identical retries, dropped it.tools/listresponse viaTool.model_dump(), which includes SDK-internal fields that are not JSON-serializable: a callablefn(FunctionTool) and atagsset._tool_to_dictnow emits the canonical MCP shape viato_mcp_tool()(name/description/inputSchema/…), so thetools/listpayload is clean.truncate_eventcoerces event payload fields to JSON-safe primitives up front (callables → repr, sets → lists, datetimes → ISO strings, unknown → str) via a newmake_json_safehelper, and passesfallback=strto its size-check serialization. Because the API client re-serializes the same event object on send, this prevents any non-serializable value in a payload from dropping an event.1.0.1→1.0.2.Impact
Customers on 1.0.1 running FastMCP v3 servers were losing events at scale —
tools/listtelemetry in particular was serialized, failed to send, and dropped after retries. This restores delivery for those events and hardens the pipeline against the whole class of non-serializable payloads (tool results containing datetimes, sets, or custom objects; custom event properties).Test plan
uv run pytest tests/test_truncation_json_safe.py tests/community/test_community_v3_event_serialization.py -v— new unit + end-to-end tests pass:make_json_safeneutralizes callables, sets, and datetimestools/listevent is JSON-serializable, is the clean MCP shape (nofn), and survives the generated API client's serializer (the exact'set'failure is gone)'set' object has no attribute '__dict__'; disabling either fix layer independently re-breaks a testuv run pytest tests/ -q— full suite: 508 passed, 4 skipped, 3 xfailed