Title: stdio server makes two worker-thread hops for every message it writes · Issue #3888 · modelcontextprotocol/python-sdk · GitHub
Open Graph Title: stdio server makes two worker-thread hops for every message it writes · Issue #3888 · modelcontextprotocol/python-sdk
X Title: stdio server makes two worker-thread hops for every message it writes · Issue #3888 · modelcontextprotocol/python-sdk
Description: Description stdout_writer in src/mcp/server/stdio.py does await stdout.write(json + "\n") await stdout.flush() stdout is an anyio.AsyncFile, so each of those awaits is its own anyio.to_thread.run_sync round trip. The write is no use to t...
Open Graph Description: Description stdout_writer in src/mcp/server/stdio.py does await stdout.write(json + "\n") await stdout.flush() stdout is an anyio.AsyncFile, so each of those awaits is its own anyio.to_thread.run_s...
X Description: Description stdout_writer in src/mcp/server/stdio.py does await stdout.write(json + "\n") await stdout.flush() stdout is an anyio.AsyncFile, so each of those awaits is its own anyio.to_th...
Opengraph URL: https://github.com/modelcontextprotocol/python-sdk/issues/3888
X: @github
Domain: github.com
IPv4 address: 140.82.114.4
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"stdio server makes two worker-thread hops for every message it writes","articleBody":"### Description\n\n`stdout_writer` in `src/mcp/server/stdio.py` does\n\n```python\nawait stdout.write(json + \"\\n\")\nawait stdout.flush()\n```\n\n`stdout` is an `anyio.AsyncFile`, so each of those awaits is its own `anyio.to_thread.run_sync` round trip. The write is no use to the client until the flush, and the writer handles one message at a time, so every response and notification pays for two hops where one would do. It shows up when a tool sends progress or log notifications: each one queues behind two thread round trips.\n\nHop count, standalone (counts only the writer's hops):\n\n```python\nimport io\n\nimport anyio\nimport anyio.to_thread\nfrom mcp_types import JSONRPCNotification\n\nfrom mcp.server.stdio import stdio_server\nfrom mcp.shared.message import SessionMessage\n\nhops = 0\n_run_sync = anyio.to_thread.run_sync\n\n\nasync def counting_run_sync(func, *args, **kwargs):\n global hops\n hops += 1\n return await _run_sync(func, *args, **kwargs)\n\n\nasync def main() -> None:\n global hops\n stdin = anyio.AsyncFile(io.StringIO()) # empty: the reader hits EOF straight away\n stdout = anyio.AsyncFile(io.StringIO())\n async with stdio_server(stdin=stdin, stdout=stdout) as (read_stream, write_stream):\n async with read_stream:\n async for _ in read_stream:\n pass\n anyio.to_thread.run_sync = counting_run_sync # count only the writer from here on\n n = 10\n for i in range(n):\n note = JSONRPCNotification(\n jsonrpc=\"2.0\", method=\"notifications/progress\", params={\"progressToken\": 1, \"progress\": i}\n )\n await write_stream.send(SessionMessage(note))\n await write_stream.aclose()\n print(f\"{n} messages written: {hops} worker-thread hops\")\n\n\nanyio.run(main)\n```\n\nOn `main` (77001c9) this prints `10 messages written: 20 worker-thread hops`. I'd expect 10.\n\nTiming over a real stdio subprocess (`Client` spawning the server via `StdioServerParameters`), interleaved before/after runs, median wall time:\n\n| workload | before | after | change |\n|---|---|---|---|\n| tool sending 5 progress notifications per call, 300 calls | 578 ms | 402 ms | -30% |\n| tool sending 20 progress notifications per call, 300 calls (legacy protocol) | 1326 ms | 851 ms | -36% |\n| 50 concurrent async tool calls, 1000 total | 544 ms | 422 ms | -22% |\n| plain sequential `call_tool`, 800 calls | 498 ms | 443 ms | -11% |\n\nThe machine was busy during these runs (load average around 10), so treat the absolute numbers loosely. Plain one-request/one-response traffic gains little; it matters once a request produces several messages.\n\nDoing the write and the flush in one `anyio.to_thread.run_sync` call on `stdout.wrapped` fixes it. The output bytes are identical and it's about 10 lines. One thing to note: it would break a duck-typed `stdout` that has async `write`/`flush` but no `.wrapped`, though the parameter is typed `anyio.AsyncFile[str]`. Open PR #2680 rewrites this writer for a different reason (shutdown after EOF) and would remove the double hop as a side effect. I have a branch with the small fix and a test if you want it.\n\nEnvironment: macOS 27.2 on Apple M5, Python 3.12.16, mcp at 77001c9, anyio 4.14.2.\n\n### References\n\n- `src/mcp/server/stdio.py`, `stdout_writer` inside `stdio_server`\n- #2680 (open), which replaces the same writer\n\nI used an AI assistant to help find and measure this; I ran the repro above myself.\n","author":{"url":"https://github.com/patrick-immometrica","@type":"Person","name":"patrick-immometrica"},"datePublished":"2026-11-21T11:42:45Z","url":"https://github.com/modelcontextprotocol/python-sdk/issues/3888"}
| 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:239896cd-e1c7-c162-a334-60b4f9ef5a8f |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A86A:237AAE:8E89EA:C1E80A:6B047F35 |
| html-safe-nonce | b4e04ac466fc60622c455bffa663dd19ffaf11412fb2cebbb20ed0fe52b603db |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBODZBOjIzN0FBRTo4RTg5RUE6QzFFODBBOjZCMDQ3RjM1IiwidmlzaXRvcl9pZCI6IjU1Nzc2MzUzNzA2NjI5MTk5ODkiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | f9fbd82106ead69fa3e60992ad4aab22e5ef65121d7167f7ff313d737dc85657 |
| hovercard-subject-tag | issue:6528347238 |
| 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/3888/issue_layout |
| twitter:image | https://opengraph.githubassets.com/482fb2ee1de4b8d0cb7156bc0a690bf577cc41001bdf2443862e269038016912/modelcontextprotocol/python-sdk/issues/3888 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/482fb2ee1de4b8d0cb7156bc0a690bf577cc41001bdf2443862e269038016912/modelcontextprotocol/python-sdk/issues/3888 |
| og:image:alt | Description stdout_writer in src/mcp/server/stdio.py does await stdout.write(json + "\n") await stdout.flush() stdout is an anyio.AsyncFile, so each of those awaits is its own anyio.to_thread.run_s... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | patrick-immometrica |
| hostname | github.com |
| expected-hostname | github.com |
| None | 0366ddfb372682a32e92a5d86155cb08fa83e5bbe79953a968789f2f7e24c511 |
| 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 | ed3f2e01f6b7672780c586c8af0e72726374d7b6 |
| 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