Skip to content

fix: exclude None optional fields from task result serialization#2544

Open
abhinavmathur-atlan wants to merge 1 commit intomodelcontextprotocol:mainfrom
abhinavmathur-atlan:fix/2539-exclude-none-in-task-result-handler
Open

fix: exclude None optional fields from task result serialization#2544
abhinavmathur-atlan wants to merge 1 commit intomodelcontextprotocol:mainfrom
abhinavmathur-atlan:fix/2539-exclude-none-in-task-result-handler

Conversation

@abhinavmathur-atlan
Copy link
Copy Markdown

Summary

Fixes #2539

task_result_handler.py called model_dump(by_alias=True) without exclude_none=True, causing optional fields like TextContent.annotations to be emitted as JSON null. The Node SDK Zod schema marks these fields as optional (absent), not nullable, so receiving null fails validation.

Root cause: The result_data dict built at line 119 carried null values into GetTaskPayloadResult.model_validate(). Since GetTaskPayloadResult uses extra="allow", the nested content is stored as raw dict values — the transport-level exclude_none=True does not recursively clean them.

Fix: Add exclude_none=True, mode="json" to both model_dump() calls in the terminal-state path, consistent with every other serialization site in shared/session.py and server/session.py.

Changes

  • src/mcp/server/experimental/task_result_handler.py: add exclude_none=True, mode="json" to result.model_dump(), and exclude_none=True to related_task.model_dump()
  • tests/experimental/tasks/server/test_task_result_handler.py: regression test asserting annotations and _meta are absent (not null) in the serialized wire output

model_dump() without exclude_none=True emits null for optional fields
(e.g. TextContent.annotations), breaking Node SDK Zod validation which
marks those fields as optional/absent rather than nullable.

Matches the exclude_none=True, mode="json" convention used throughout
the rest of the session layer's serialization paths.

Fixes modelcontextprotocol#2539
@abhinavmathur-atlan
Copy link
Copy Markdown
Author

@maxisbey @Kludex @felixweinberger , when you get a chance, this is a small fix (2 lines + test) for #2539. Happy to adjust if anything looks off.

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.

Bug: task_result_handler.py serializes None optional fields as JSON null, breaking Node SDK Zod validation

1 participant