fix: exclude None optional fields from task result serialization#2544
Open
abhinavmathur-atlan wants to merge 1 commit intomodelcontextprotocol:mainfrom
Open
Conversation
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
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. |
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
Fixes #2539
task_result_handler.pycalledmodel_dump(by_alias=True)withoutexclude_none=True, causing optional fields likeTextContent.annotationsto be emitted as JSONnull. The Node SDK Zod schema marks these fields as optional (absent), not nullable, so receivingnullfails validation.Root cause: The
result_datadict built at line 119 carriednullvalues intoGetTaskPayloadResult.model_validate(). SinceGetTaskPayloadResultusesextra="allow", the nested content is stored as raw dict values — the transport-levelexclude_none=Truedoes not recursively clean them.Fix: Add
exclude_none=True, mode="json"to bothmodel_dump()calls in the terminal-state path, consistent with every other serialization site inshared/session.pyandserver/session.py.Changes
src/mcp/server/experimental/task_result_handler.py: addexclude_none=True, mode="json"toresult.model_dump(), andexclude_none=Truetorelated_task.model_dump()tests/experimental/tasks/server/test_task_result_handler.py: regression test assertingannotationsand_metaare absent (not null) in the serialized wire output