Title: FastMCP/stdio: in-flight tool responses dropped on stdin EOF when input is bash-redirected from a file · Issue #2678 · modelcontextprotocol/python-sdk · GitHub
Open Graph Title: FastMCP/stdio: in-flight tool responses dropped on stdin EOF when input is bash-redirected from a file · Issue #2678 · modelcontextprotocol/python-sdk
X Title: FastMCP/stdio: in-flight tool responses dropped on stdin EOF when input is bash-redirected from a file · Issue #2678 · modelcontextprotocol/python-sdk
Description: 1. Initial Checks I have searched the issue tracker for similar bugs (closest existing: #1933, but that's about premature close of real stdio during shutdown — different failure layer) I am using a supported MCP SDK version (pinned mcp>=...
Open Graph Description: 1. Initial Checks I have searched the issue tracker for similar bugs (closest existing: #1933, but that's about premature close of real stdio during shutdown — different failure layer) I am using a...
X Description: 1. Initial Checks I have searched the issue tracker for similar bugs (closest existing: #1933, but that's about premature close of real stdio during shutdown — different failure layer) I am usi...
Opengraph URL: https://github.com/modelcontextprotocol/python-sdk/issues/2678
X: @github
Domain: github.com
IPv4 address: 140.82.114.3
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"FastMCP/stdio: in-flight tool responses dropped on stdin EOF when input is bash-redirected from a file","articleBody":"**1. Initial Checks**\n- [x] I have searched the issue tracker for similar bugs (closest existing: #1933, but that's about premature close of *real* stdio during shutdown — different failure layer)\n- [x] I am using a supported MCP SDK version (pinned `mcp>=1.27,\u003c2`)\n\n**2. Description**\n\n*Triage note: checked `jlowin/fastmcp` (now PrefectHQ fork) — FastMCP's server-side stdio delegates to `mcp.server.stdio` via a transport mixin, so the bug belongs here in the SDK rather than in the FastMCP wrapper.*\n\nWhen driving a FastMCP stdio server with a file-redirected stdin (e.g. `python -m my_server \u003c payload.jsonl > response.jsonl`), in-flight tool-call responses can be dropped if their response writer hasn't been scheduled when stdin EOF arrives.\n\nThe stdio read loop appears to treat stdin EOF as an immediate-shutdown signal, cancelling pending writer tasks before they flush their JSON-RPC responses to stdout. The failure is silent — no traceback, no log line, the response is simply absent from stdout.\n\n**Expected:** All responses for processed requests appear on stdout before the server exits.\n**Actual:** Responses for the last-issued requests can be missing entirely.\n\n**3. Example Code (minimal reproducible)**\n\n```python\n# server.py\nfrom mcp.server.fastmcp import FastMCP\nimport asyncio\n\nmcp = FastMCP(\"repro\")\n\n@mcp.tool()\nasync def slow_echo(text: str) -> str:\n await asyncio.sleep(0.05) # guaranteed yield point so writer scheduling is observable\n return text\n\nif __name__ == \"__main__\":\n mcp.run(transport=\"stdio\")\n```\n\n```jsonl\n# payload.jsonl\n{\"jsonrpc\":\"2.0\",\"id\":0,\"method\":\"initialize\",\"params\":{\"protocolVersion\":\"2024-11-05\",\"capabilities\":{},\"clientInfo\":{\"name\":\"repro\",\"version\":\"0.1\"}}}\n{\"jsonrpc\":\"2.0\",\"method\":\"notifications/initialized\",\"params\":{}}\n{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"tools/call\",\"params\":{\"name\":\"slow_echo\",\"arguments\":{\"text\":\"first\"}}}\n{\"jsonrpc\":\"2.0\",\"id\":2,\"method\":\"tools/call\",\"params\":{\"name\":\"slow_echo\",\"arguments\":{\"text\":\"second\"}}}\n```\n\n```bash\npython server.py \u003c payload.jsonl > response.jsonl\n# Observed: response.jsonl contains id=0 + id=1 results, id=2 is absent.\n```\n\n*The race is timing-sensitive; if id=2 surfaces on the first run, increase the `asyncio.sleep` delay or run repeatedly. We saw it deterministically in production where the tool body did a real HTTP call (~50-200ms).*\n\n**Diagnostic fingerprint:** the difference between this transport race and a quality bug in the tool itself is *absence-of-response* vs *response-with-empty-result*. If you ever see `id=N` missing entirely (not `{\"id\":N,\"result\":[]}`), suspect this race.\n\n**4. Python and MCP Python SDK**\n\n- Python: 3.12 (`python:3.12-slim` Docker base)\n- MCP SDK: 1.27.x (`mcp>=1.27,\u003c2`)\n- OS: Linux (Debian Bookworm; reproduced on Synology DSM 7.2 host)\n- Transport: stdio via `mcp.run(transport=\"stdio\")`\n\n**Workaround we shipped (in case it helps the fix design):** wrote a Python driver that owns both pipes via `subprocess.Popen(stdin=PIPE, stdout=PIPE)` and refuses to close stdin until the response for the last-issued id is observed on stdout. ~140 lines stdlib-only.\n\n**Suggested fix:** before exiting on stdin EOF, await any pending writer tasks (with a small timeout) so their JSON-RPC responses reach stdout before the process terminates.\n","author":{"url":"https://github.com/theone139344","@type":"Person","name":"theone139344"},"datePublished":"2026-05-25T14:25:01Z","url":"https://github.com/modelcontextprotocol/python-sdk/issues/2678"}
| 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:29c45e51-92a8-e676-8973-5b6df901ae4f |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A22A:1514D:59F4F7:7BB62A:6B04749C |
| html-safe-nonce | 6309eb19bb792350c48c04994b7191df0e3cfac1ea693f12482d20bb57119303 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBMjJBOjE1MTREOjU5RjRGNzo3QkI2MkE6NkIwNDc0OUMiLCJ2aXNpdG9yX2lkIjoiNTYwMzkwODAzNzc5OTg2NzU0OCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 852fd55822d7b2ac4ea37ecc820a14ca8b159a0937b2513b09e8d4f3e9edf1a3 |
| hovercard-subject-tag | issue:4517587555 |
| 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/2678/issue_layout |
| twitter:image | https://opengraph.githubassets.com/b8da3d3b668333e6d437b1b1c23d168c2c1b73da9f4b916fcf03d8f1dd94ccf0/modelcontextprotocol/python-sdk/issues/2678 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/b8da3d3b668333e6d437b1b1c23d168c2c1b73da9f4b916fcf03d8f1dd94ccf0/modelcontextprotocol/python-sdk/issues/2678 |
| og:image:alt | 1. Initial Checks I have searched the issue tracker for similar bugs (closest existing: #1933, but that's about premature close of real stdio during shutdown — different failure layer) I am using a... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | theone139344 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 9c099f1b85def81ca352464c276510dfef32deaf3c305404e65b8d270284a58b |
| 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 | cf73f57448d52b3a7b619195e182b33d3b3454ca |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width