Title: A tool returning a non-finite float fails on the client with an error naming the tool · Issue #3341 · modelcontextprotocol/python-sdk · GitHub
Open Graph Title: A tool returning a non-finite float fails on the client with an error naming the tool · Issue #3341 · modelcontextprotocol/python-sdk
X Title: A tool returning a non-finite float fails on the client with an error naming the tool · Issue #3341 · modelcontextprotocol/python-sdk
Description: Initial Checks I confirm that I'm using the newest release of my line (verified on 2.0.0) I confirm that I searched for my issue before opening this issue Release line 2.x (current stable). Also reproduces on 1.29.0. Bug description A to...
Open Graph Description: Initial Checks I confirm that I'm using the newest release of my line (verified on 2.0.0) I confirm that I searched for my issue before opening this issue Release line 2.x (current stable). Also re...
X Description: Initial Checks I confirm that I'm using the newest release of my line (verified on 2.0.0) I confirm that I searched for my issue before opening this issue Release line 2.x (current stable). Als...
Opengraph URL: https://github.com/modelcontextprotocol/python-sdk/issues/3341
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"A tool returning a non-finite float fails on the client with an error naming the tool","articleBody":"## Initial Checks\n\n- [x] I confirm that I'm using the newest release of my line (verified on 2.0.0)\n- [x] I confirm that I searched for my issue before opening this issue\n\n## Release line\n\n`2.x (current stable)`. Also reproduces on 1.29.0.\n\n## Bug description\n\nA tool returning a non-finite float fails on the client with an error that names\nthe tool. `float(\"inf\")`, `float(\"-inf\")` and `float(\"nan\")` all reproduce it. A\nfinite float is fine.\n\nThe handler returned an ordinary Python float. The value that fails validation is\nproduced by the SDK's own outbound serialization.\n\n## Steps to reproduce\n\n```python\nimport anyio\nfrom mcp.client import Client\nfrom mcp.server.mcpserver import MCPServer\n\nserver = MCPServer(\"probe\")\n\n\n@server.tool()\ndef ratio(a: float, b: float) -> float:\n \"\"\"Divide a by b.\"\"\"\n return a / b if b else float(\"inf\")\n\n\nasync def main() -> None:\n async with Client(server) as client:\n print(await client.call_tool(\"ratio\", {\"a\": 1.0, \"b\": 2.0}))\n print(await client.call_tool(\"ratio\", {\"a\": 1.0, \"b\": 0.0}))\n\n\nanyio.run(main)\n```\n\n## Actual behaviour\n\nThe first call returns `structured_content={'result': 0.5}`. The second raises:\n\n```text\nRuntimeError: Invalid structured content returned by tool ratio: None is not of type 'number'\n\nFailed validating 'type' in schema['properties']['result']:\n {'title': 'Result', 'type': 'number'}\n\nOn instance['result']:\n None\n```\n\nUncaught, this arrives as two nested `ExceptionGroup`s out of `Client.__aexit__`,\n84 lines in total, with the line naming the tool last. Catching the `RuntimeError`\nleaves the session usable and later calls succeed.\n\nThe value cannot be recovered. `validate_tool_result` runs unconditionally for any\nnon-error result (`client/session.py:1109`, `client/client.py:828`) with no opt-out,\nso the caller never reaches the `CallToolResult` to read either\n`structured_content` or the text block.\n\n## Expected behaviour\n\nEither the value survives the round trip, or the failure names the real cause. At\npresent the message directs the reader to the handler, which is the one component\nbehaving correctly.\n\nThe SDK already rejects non-finite floats explicitly elsewhere, at\n`server/request_state.py:128`, so refusing them at the point of return with a clear\nmessage would match existing practice.\n\n## Root cause\n\n1. `func_metadata.convert_result` builds `structured_content = {'result': inf}`.\n Correct so far.\n2. `_dump_result` (`server/runner.py:118`) serializes with\n `model_dump(by_alias=True, mode=\"json\", exclude_none=True)`. Pydantic's JSON mode\n writes non-finite floats as `null`, since JSON has no `Infinity` or `NaN` and\n `ser_json_inf_nan` defaults to `\"null\"`. The SDK does not set that setting\n anywhere.\n3. The client validates `{\"result\": null}` against the generated output schema\n `{\"result\": {\"type\": \"number\"}}`, which rejects it.\n4. `client/session.py:1155` raises, interpolating the tool name.\n\nThis is server-side and independent of transport, so it is not specific to the\nin-memory client used above.\n\nThe emitted JSON stays valid, so this is not an interoperability problem. The\nproblem is that the value is dropped silently and the diagnostic points at the\nwrong component.\n\nNon-finite floats arise from ordinary arithmetic, including division by zero,\noverflow, `statistics` on degenerate input, and numpy interop, so a handler can\nproduce one without ever writing `float(\"inf\")`.\n\n## Why the tests do not catch it\n\nNo test returns a non-finite float from a tool. `math.inf` appears in the suite\nonly as an unbounded stream buffer size (`tests/client/test_stdio.py:149`,\n`tests/interaction/transports/_bridge.py:120`) and as a rejected TTL input\n(`tests/server/test_request_state.py:355`). The one `-> float` division helper,\n`tests/server/mcpserver/test_tool_manager.py:838`, is marked `# pragma: no cover`.\n\n## Not a duplicate of #3100 or PR #3118\n\n#3100 reports the same end state, an output schema that rejects the SDK's own\nstructured result, but its cause is validation and serialization shapes differing\non aliases and computed fields. This reproduces with a plain `-> float` return and\nno Pydantic model.\n\nPR #3118 moves output schema generation into serialization mode. That does not\nchange this case: a `float` serializes as `number` either way, so `null` still\nfails the schema.\n\n#3224 is also distinct. It concerns nulls injected for absent `NotRequired`\nTypedDict keys, not values destroyed by JSON serialization.\n\n## Python & MCP Python SDK\n\n```text\nPython 3.13.11\nmcp 2.0.0 (also reproduced on main @ 0d921927 and on mcp 1.29.0)\npydantic 2.12.5\nLinux x86_64\n```\n\n---\n\nAI assistance was used to investigate and draft this report.\n","author":{"url":"https://github.com/jayhemnani9910","@type":"Person","name":"jayhemnani9910"},"datePublished":"2026-08-20T07:04:18Z","url":"https://github.com/modelcontextprotocol/python-sdk/issues/3341"}
| 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:263dca58-c2f4-0063-ef55-6ed546e7258d |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E0B2:24ACB9:AA399F:E23FFF:6B00E7FA |
| html-safe-nonce | 73b51235cdb7aa7f96a796313f7b52759e337edfbbf064a0133cc3c07cf0ec2c |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFMEIyOjI0QUNCOTpBQTM5OUY6RTIzRkZGOjZCMDBFN0ZBIiwidmlzaXRvcl9pZCI6Ijg5MDE4NjM3MTA0MjgwMzA5NzAiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 6459355578928702c9c418fc8f7008c68974c29dfee8882cae416407f3065b59 |
| hovercard-subject-tag | issue:5200669861 |
| 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/3341/issue_layout |
| twitter:image | https://opengraph.githubassets.com/8ed99109da93e664f894e8cf94249b63775408ac394efcdb9fcf76a00247e3e3/modelcontextprotocol/python-sdk/issues/3341 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/8ed99109da93e664f894e8cf94249b63775408ac394efcdb9fcf76a00247e3e3/modelcontextprotocol/python-sdk/issues/3341 |
| og:image:alt | Initial Checks I confirm that I'm using the newest release of my line (verified on 2.0.0) I confirm that I searched for my issue before opening this issue Release line 2.x (current stable). Also re... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | jayhemnani9910 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 27088b6977484d0d3321d9a11c907c6d6637b315cfa359ef7952dbf6292f9b14 |
| 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 | 624179fb8b88d26ba5bba08894eb60a1c5a1d93d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width