Title: Using transport="stdio" closes real stdio, causing ValueError after server exits · Issue #1933 · modelcontextprotocol/python-sdk · GitHub
Open Graph Title: Using transport="stdio" closes real stdio, causing ValueError after server exits · Issue #1933 · modelcontextprotocol/python-sdk
X Title: Using transport="stdio" closes real stdio, causing ValueError after server exits · Issue #1933 · 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 Hi! I ran into an issu...
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/1933
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Using transport=\"stdio\" closes real stdio, causing ValueError after server exits","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\nHi! I ran into an issue where running the server with `transport=\"stdio\"` causes subsequent stdio operations to fail after the server exits.\n\n### Minimal reproduction\n\n```python\nfrom mcp.server.fastmcp import FastMCP\n\nmcp = FastMCP(\"Demo\")\nmcp.run(transport=\"stdio\")\nprint(\"?\")\n```\n\nWhen using `Ctrl+D` to exit the server, the final print raises:\n\n```bash\nTraceback (most recent call last):\n File \"...\", line 5, in \u003cmodule\u003e\n print(\"?\")\nValueError: I/O operation on closed file.\n```\n\nI’m not sure if this is intended behavior though.\n\n### Likely cause\n\nIn stdio.py, stdio is wrapped like this:\n\u003chttps://github.com/modelcontextprotocol/python-sdk/blob/5301298225968ce0fa8ae62870f950709da14dc6/src/mcp/server/stdio.py#L44-L47\u003e\n\nWhen these wrappers are closed, they also close the sys.stdin.buffer / sys.stdout.buffer.\n\n### Proposed fix\n\n```python\n if not stdin:\n stdin_fd = os.dup(sys.stdin.fileno())\n stdin_bin = os.fdopen(stdin_fd, \"rb\", closefd=True)\n stdin = anyio.wrap_file(TextIOWrapper(stdin_bin, encoding=\"utf-8\"))\n if not stdout:\n stdout_fd = os.dup(sys.stdout.fileno())\n stdout_bin = os.fdopen(stdout_fd, \"wb\", closefd=True)\n stdout = anyio.wrap_file(TextIOWrapper(stdout_bin, encoding=\"utf-8\"))\n```\n\n### Python \u0026 MCP Python SDK\n\n```Text\nPython 3.10.15\nmcp==1.25.0\n```","author":{"url":"https://github.com/hyn0027","@type":"Person","name":"hyn0027"},"datePublished":"2026-01-22T23:15:42.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":3},"url":"https://github.com/1933/python-sdk/issues/1933"}
| 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:54d71536-acc7-87a1-8bca-687ef24323a8 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A260:70EA8:D39045:12EE06E:6A5A06CD |
| html-safe-nonce | fb4989fc616729471615d5da0ad5745b4a74fad6c52c96b6b525bc0b91b5b858 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBMjYwOjcwRUE4OkQzOTA0NToxMkVFMDZFOjZBNUEwNkNEIiwidmlzaXRvcl9pZCI6IjkwNjc3NTA0ODcxNDgzMzI3NTAiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | cd8185b46094cb2547e0c5d79b769f838b116e4ece5d340cc07f6f3fa752c296 |
| hovercard-subject-tag | issue:3845135525 |
| 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/1933/issue_layout |
| twitter:image | https://opengraph.githubassets.com/fdf75add7fd39d811f1ab0b417368acdbfe3ee8349087b3d5f63b0a2884949e3/modelcontextprotocol/python-sdk/issues/1933 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/fdf75add7fd39d811f1ab0b417368acdbfe3ee8349087b3d5f63b0a2884949e3/modelcontextprotocol/python-sdk/issues/1933 |
| 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 | hyn0027 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 8c2fe7351e4e987cb8f75e2425a0b895d807779bc4c2d3c312be0a54082219f7 |
| 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 | 491710c7a8eecca9d0a06bd157646f2685d8c87b |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width