From 4a4543f57b230567c976a38316adbbf4cb06138b Mon Sep 17 00:00:00 2001 From: Dev Chiniwala Date: Sun, 22 Nov 2026 19:25:54 +0530 Subject: [PATCH] fix(resources): avoid extra thread hop when reading template resources create_resource wrapped the template result in a sync lambda, forcing FunctionResource.read() through anyio.to_thread.run_sync. Use an async wrapper so the result is returned directly on the event loop. Fixes #3889 Co-Authored-By: Claude Opus 4.6 --- src/mcp/server/mcpserver/resources/templates.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/mcp/server/mcpserver/resources/templates.py b/src/mcp/server/mcpserver/resources/templates.py index 621b2e9448..d48a7fd913 100644 --- a/src/mcp/server/mcpserver/resources/templates.py +++ b/src/mcp/server/mcpserver/resources/templates.py @@ -231,6 +231,9 @@ async def create_resource( if isinstance(result, InputRequiredResult): return result + async def _return_result() -> Any: + return result + return FunctionResource( uri=uri, # type: ignore name=self.name, @@ -240,7 +243,7 @@ async def create_resource( icons=self.icons, annotations=self.annotations, meta=self.meta, - fn=lambda: result, # Capture result in closure + fn=_return_result, ) except (ResourceError, MCPError): raise