Title: Bug: task_result_handler.py serializes None optional fields as JSON null, breaking Node SDK Zod validation · Issue #2539 · modelcontextprotocol/python-sdk · GitHub
Open Graph Title: Bug: task_result_handler.py serializes None optional fields as JSON null, breaking Node SDK Zod validation · Issue #2539 · modelcontextprotocol/python-sdk
X Title: Bug: task_result_handler.py serializes None optional fields as JSON null, breaking Node SDK Zod validation · Issue #2539 · modelcontextprotocol/python-sdk
Description: Initial Checks I confirm that I'm using the latest version of MCP Python SDK I confirm that I searched for my issue in https://github.com/modelcontextprotocol/python-sdk/issues before opening this issue Description Bug: task_result_handl...
Open Graph Description: Initial Checks I confirm that I'm using the latest version of MCP Python SDK I confirm that I searched for my issue in https://github.com/modelcontextprotocol/python-sdk/issues before opening this ...
X Description: Initial Checks I confirm that I'm using the latest version of MCP Python SDK I confirm that I searched for my issue in https://github.com/modelcontextprotocol/python-sdk/issues before opening t...
Opengraph URL: https://github.com/modelcontextprotocol/python-sdk/issues/2539
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Bug: task_result_handler.py serializes None optional fields as JSON null, breaking Node SDK Zod validation","articleBody":"### Initial Checks\n\n- [x] I confirm that I'm using the latest version of MCP Python SDK\n- [x] I confirm that I searched for my issue in https://github.com/modelcontextprotocol/python-sdk/issues before opening this issue\n\n### Description\n\n Bug: task_result_handler.py serializes None optional fields as JSON null,\n breaking Node SDK Zod validation\n\n What the spec says\n\n The MCP spec defines TextContent with optional fields:\n\n interface TextContent {\n type: \"text\";\n text: string;\n annotations?: Annotations; // optional — may be absent\n _meta?: Record\u003cstring, unknown>; // optional — may be absent\n }\n\n \"Optional\" in the spec means the field may be absent from the JSON. Both SDKs\n agree on this:\n - Python SDK's TextContentSchema defines annotations and _meta with\n Optional[...] = None\n - Node SDK's TextContentSchema defines them with .optional() — accepts\n undefined (absent) but not null\n\n What the Python SDK does\n\n The Python SDK has two serialization paths for CallToolResult:\n\n 1. Normal responses (_send_response in session.py): uses\n model_dump(by_alias=True, mode=\"json\", exclude_none=True). This correctly omits\n None fields from the JSON. Works fine.\n 2. Task result delivery (handle in task_result_handler.py, line 131): uses\n result.model_dump(by_alias=True) without exclude_none=True. This serializes\n None fields as explicit JSON null:\n\n # task_result_handler.py line 131\n result_data = result.model_dump(by_alias=True) # ← missing exclude_none=True\n\n Produces:\n {\n \"content\": [{\n \"type\": \"text\",\n \"text\": \"counted to 20\",\n \"annotations\": null,\n \"_meta\": null\n }],\n \"isError\": false\n }\n\n What breaks\n\n The Node SDK's requestStream polls tasks/result when a task completes, then\n parses the response with CallToolResultSchema. The Zod discriminated union for\n content blocks uses:\n\n annotations: AnnotationsSchema.optional() // accepts undefined, rejects null\n _meta: z.record(z.string(), z.unknown()).optional() // accepts undefined,\n rejects null\n\n null ≠ undefined in Zod. The parse fails:\n \"expected object, received null\" at path [\"annotations\"]\n \"expected record, received null\" at path [\"_meta\"]\n\n This kills the task result stream. The runtime falls back to polling tasks/get\n + tasks/result, but by then the result may already be consumed.\n\n Fix\n\n One line — add exclude_none=True to the model_dump call in\n task_result_handler.py:\n\n # Before (line 131):\n result_data = result.model_dump(by_alias=True)\n\n # After:\n result_data = result.model_dump(by_alias=True, exclude_none=True)\n\n This matches the pattern used everywhere else in the SDK (_send_response,\n send_notification).\n\n Reproduction\n\n 1. Python MCP server with a task-aware tool that returns\n CallToolResult(content=[TextContent(type=\"text\", text=\"hello\")])\n 2. Node MCP client connects and calls the tool with task: {}\n 3. Task completes → client calls tasks/result → Zod parse fails on annotations:\n null\n\n### Example Code\n\n```Python\n\n```\n\n### Python & MCP Python SDK\n\n```Text\n- mcp (Python): 1.27.0\n - @modelcontextprotocol/sdk (Node): 1.29.0\n```","author":{"url":"https://github.com/rynowak","@type":"Person","name":"rynowak"},"datePublished":"2026-05-04T23:07:55Z","url":"https://github.com/modelcontextprotocol/python-sdk/issues/2539"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:73422b8b-9878-f16a-ee9f-1c2b4fdc65d3 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9392:9EAA1:42EF8E:58BA37:6AFCEAFD |
| html-safe-nonce | dbe65ac28171e9709531d8ff539072ae4a822a97b1aeb60f3937f42d615bd574 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5MzkyOjlFQUExOjQyRUY4RTo1OEJBMzc6NkFGQ0VBRkQiLCJ2aXNpdG9yX2lkIjoiMTM5MjkyMTA2MTk3MTc4MjM5NyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | da235f0720f05402462991572605f5c2b2291412365fd0329ca4d752b695b69d |
| hovercard-subject-tag | issue:4380268656 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/modelcontextprotocol/python-sdk/2539/issue_layout |
| twitter:image | https://opengraph.githubassets.com/d5505564d536425bb709e5084a5bf42af84e72eec60cf423cbdc5c0858f336f8/modelcontextprotocol/python-sdk/issues/2539 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/d5505564d536425bb709e5084a5bf42af84e72eec60cf423cbdc5c0858f336f8/modelcontextprotocol/python-sdk/issues/2539 |
| og:image:alt | Initial Checks I confirm that I'm using the latest version of MCP Python SDK I confirm that I searched for my issue in https://github.com/modelcontextprotocol/python-sdk/issues before opening this ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | rynowak |
| hostname | github.com |
| expected-hostname | github.com |
| None | 99aede4c5f55ec5cf06666a8c9e03ebf9f85f7231223e7f1c38c16e5504835b3 |
| turbo-cache-control | no-preview |
| go-import | github.com/modelcontextprotocol/python-sdk git https://github.com/modelcontextprotocol/python-sdk.git |
| octolytics-dimension-user_id | 182288589 |
| octolytics-dimension-user_login | modelcontextprotocol |
| octolytics-dimension-repository_id | 862584018 |
| octolytics-dimension-repository_nwo | modelcontextprotocol/python-sdk |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 862584018 |
| octolytics-dimension-repository_network_root_nwo | modelcontextprotocol/python-sdk |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | ecfd63ca125ec74b42f74eeb158b3fa081c8c497 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width