Reading a resource template makes an extra worker-thread round trip to return a value it already has
メンテナーはふだん 1 日以内に返信
評価
- 難易度
- 2/5
- 見積もり時間
- 1〜3時間
- 初心者へのやさしさ
- 52/100
- issue の種類
- リファクタリング
- 明瞭さ
- 明確に書かれている
- 活発さ
- 活発
- 技術スタック
- python
- 領域
- performance
調査の方向性
Start in src/mcp/server/mcpserver/resources/templates.py at the fn=lambda: result in create_resource, then read FunctionResource.read in src/mcp/server/mcpserver/resources/types.py to see why a sync callable is offloaded via anyio.to_thread.run_sync. Replace the wrapping lambda with an async closure returning the already-computed result, then rerun the repro to confirm one hop for users://1 and zero for orders://1, and run the existing resources/templates server tests. Note the reporter already has a branch and test and offers to open a PR, so check for that before starting.
索引モデルが issue の本文から書いたものです。
説明
Description
What happens
ResourceTemplate.create_resource (src/mcp/server/mcpserver/resources/templates.py) runs the template function, then wraps the result as FunctionResource(fn=lambda: result). FunctionResource.read() sees a sync callable and calls anyio.to_thread.run_sync(self.fn). So every template read crosses into a worker thread just to return a constant:
- sync template function: 2 hops per read (the function itself, then the lambda)
- async template function: 1 hop per read, where it needs none
Expected
1 hop for a sync template function (to run it), 0 for an async one.
Repro
import anyio
import anyio.to_thread
from mcp import Client
from mcp.server.mcpserver import MCPServer
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)
anyio.to_thread.run_sync = counting_run_sync
mcp = MCPServer("demo")
@mcp.resource("users://{user_id}")
def user(user_id: str) -> str:
return f"user {user_id}"
@mcp.resource("orders://{order_id}")
async def order(order_id: str) -> str:
return f"order {order_id}"
async def main() -> None:
global hops
async with Client(mcp) as client:
for uri in ("users://1", "orders://1"):
hops = 0
await client.read_resource(uri)
print(f"{uri}: {hops} worker-thread hop(s)")
anyio.run(main)
Output on main (77001c9):
users://1: 2 worker-thread hop(s)
orders://1: 1 worker-thread hop(s)
Cost
Interleaved before/after runs of 1000–2000 template reads, median wall time. The gain depends on the transport, so here are all of them:
- in-process
Client(server): -33% (sync template), -35% (async template) - legacy JSON-RPC streams, 100 reads in flight: -25% (sync template), -13% (async template)
- legacy JSON-RPC streams, sequential: -5% to -22% across runs
- streamable HTTP, SSE responses, sequential: -9% to -14%
- streamable HTTP, JSON responses: about -20% sequential (borderline), about 0% with 100 in flight
The machine was loaded (load average around 10), so the numbers are noisy. Over HTTP the hop is a small part of each request.
Possible fix
Wrap the result in async def value(): return result and pass fn=value. The content returned is the same. The one visible difference: for template-built resources, FunctionResource.fn becomes a coroutine function instead of a plain callable. I couldn't find anything in the repo that calls .fn() directly. I have a branch with this and a test, and I'm happy to open a PR if it's wanted.
Python 3.12.16, macOS 27.2 (Apple M5), mcp at 77001c9, anyio 4.14.2, pydantic 2.12.5.
References
src/mcp/server/mcpserver/resources/templates.py, thefn=lambda: resultincreate_resourcesrc/mcp/server/mcpserver/resources/types.py,FunctionResource.read(its thread offload came from #2380)
Disclosure: an AI assistant helped me find and benchmark this. I checked the repro output 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
-
auto-reported bug
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
メンテナーはふだん 1 日以内に返信
-
stix-atlas.json: the same relationship id is used for several different mitigates relationshipsオープン
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
-
backend bug
難易度 2/5 1〜3時間 初心者へのやさしさ 86/100
nolte/kamerplanter#3193 ·
メンテナーはふだん 1 日以内に返信
-
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 日以内に返信