Skip to content

fix(events): never drop events on non-JSON-serializable payloads#40

Merged
naji247 merged 2 commits into
mainfrom
fix/event-serialization-json-safe
Jul 10, 2026
Merged

fix(events): never drop events on non-JSON-serializable payloads#40
naji247 merged 2 commits into
mainfrom
fix/event-serialization-json-safe

Conversation

@naji247

@naji247 naji247 commented Jul 10, 2026

Copy link
Copy Markdown
Member

Summary

  • Bug (1.0.1): events whose payload contained a value that is not JSON-serializable were being silently dropped. Two failure modes, one root cause:
    • Truncation aborted with Unable to serialize unknown type: <class 'function'>truncate_event serializes the event with pydantic's model_dump_json(), which raises on 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.
  • Fix (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.
    • Systemic guaranteetruncate_event 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.
  • Release: patch version bump 1.0.11.0.2.

Impact

Customers on 1.0.1 running FastMCP v3 servers were losing events at scale — tools/list telemetry 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:
    • truncation does not abort on a callable/set payload and yields a JSON-serializable event
    • make_json_safe neutralizes callables, sets, and datetimes
    • a real tools/list event is JSON-serializable, is the clean MCP shape (no fn), and survives the generated API client's serializer (the exact 'set' failure is gone)
  • Regression check: on unfixed code all these tests fail — the end-to-end one fails with the exact production error 'set' object has no attribute '__dict__'; disabling either fix layer independently re-breaks a test
  • uv run pytest tests/ -q — full suite: 508 passed, 4 skipped, 3 xfailed
  • Verified on FastMCP 3.0.0b1 (pinned) and 3.4.4 (latest published)

naji247 added 2 commits July 10, 2026 14:42
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.
@naji247 naji247 force-pushed the fix/event-serialization-json-safe branch from 7811615 to a986388 Compare July 10, 2026 18:50
@naji247 naji247 merged commit bb1e9bc into main Jul 10, 2026
43 checks passed
@naji247 naji247 deleted the fix/event-serialization-json-safe branch July 10, 2026 19:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants