stdio server makes two worker-thread hops for every message it writes
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 2/5
- 見積もり時間
- 1〜3時間
- 初心者へのやさしさ
- 45/100
- issue の種類
- リファクタリング
- 明瞭さ
- 明確に書かれている
- 活発さ
- 活発
- 技術スタック
- python
- 領域
- performance
調査の方向性
Start at stdout_writer inside stdio_server in src/mcp/server/stdio.py, then run the issue's hop-counting script to confirm 20 hops for 10 messages on current main. Read open PR #2680 first, since it replaces the same writer and would remove the double hop as a side effect. Done looks like one worker-thread hop per message, byte-identical output, and the stdio server tests passing; a test counting anyio.to_thread.run_sync calls would pin it down.
索引モデルが issue の本文から書いたものです。
説明
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 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.
Hop count, standalone (counts only the writer's hops):
import io
import anyio
import anyio.to_thread
from mcp_types import JSONRPCNotification
from mcp.server.stdio import stdio_server
from mcp.shared.message import SessionMessage
hops = 0
_run_sync = anyio.to_thread.run_sync
async def counting_run_sync(func, *args, **kwargs):
global hops
hops += 1
return await _run_sync(func, *args, **kwargs)
async def main() -> None:
global hops
stdin = anyio.AsyncFile(io.StringIO()) # empty: the reader hits EOF straight away
stdout = anyio.AsyncFile(io.StringIO())
async with stdio_server(stdin=stdin, stdout=stdout) as (read_stream, write_stream):
async with read_stream:
async for _ in read_stream:
pass
anyio.to_thread.run_sync = counting_run_sync # count only the writer from here on
n = 10
for i in range(n):
note = JSONRPCNotification(
jsonrpc="2.0", method="notifications/progress", params={"progressToken": 1, "progress": i}
)
await write_stream.send(SessionMessage(note))
await write_stream.aclose()
print(f"{n} messages written: {hops} worker-thread hops")
anyio.run(main)
On main (77001c9) this prints 10 messages written: 20 worker-thread hops. I'd expect 10.
Timing over a real stdio subprocess (Client spawning the server via StdioServerParameters), interleaved before/after runs, median wall time:
| workload | before | after | change |
|---|---|---|---|
| tool sending 5 progress notifications per call, 300 calls | 578 ms | 402 ms | -30% |
| tool sending 20 progress notifications per call, 300 calls (legacy protocol) | 1326 ms | 851 ms | -36% |
| 50 concurrent async tool calls, 1000 total | 544 ms | 422 ms | -22% |
plain sequential call_tool, 800 calls |
498 ms | 443 ms | -11% |
The 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.
Doing 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.
Environment: macOS 27.2 on Apple M5, Python 3.12.16, mcp at 77001c9, anyio 4.14.2.
References
src/mcp/server/stdio.py,stdout_writerinsidestdio_server- #2680 (open), which replaces the same writer
I used an AI assistant to help find and measure this; I ran the repro above myself.
- 主要言語
- Python
- スター
- 25k
- フォーク
- 4.2k
- 平均マージ
- 10分
- マージ済み PR(30日)
- 1
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
modelcontextprotocol/python-sdk のほかの issue
-
v2
難易度 1/5 1時間未満 初心者へのやさしさ 86/100
modelcontextprotocol/python-sdk#3978 · コメント 2 件 ·
メンテナーはふだん 1 日以内に返信
-
Client serves cached tools after its context exits with an exception対応中かも @jonxsong が 1 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
modelcontextprotocol/python-sdk#3968 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 85/100
modelcontextprotocol/python-sdk#3957 ·
メンテナーはふだん 1 日以内に返信
-
v2
難易度 1/5 1時間未満 初心者へのやさしさ 91/100
modelcontextprotocol/python-sdk#3934 · コメント 2 件 ·
メンテナーはふだん 1 日以内に返信
-
bug v1
難易度 2/5 1〜3時間 初心者へのやさしさ 79/100
modelcontextprotocol/python-sdk#3929 · コメント 2 件 ·
メンテナーはふだん 1 日以内に返信
modelcontextprotocol/python-sdk の issue をすべて見る
似ている issue
-
soup prune-prompt exits 1 after writing its output on a file over 100,000 rows, and reports a capped row count otherwise対応中かも @abdulwaarith0 が今日担当しました。 オープンneeds-triage
難易度 2/5 1〜3時間 初心者へのやさしさ 84/100
MakazhanAlpamys/Soup#3690 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
-
bug data-types native-format
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
ClickHouse/clickhouse-connect#1320 ·
メンテナーはふだん 1 日以内に返信
-
accepted
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
future-agi/future-agi#6392 ·
メンテナーはふだん 1 日以内に返信