FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Offload sync function calls to a thread by Rifa-111 · Pull Request #3010 · modelcontextprotocol/python-sdk · GitHub

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (1) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
6 changes: 5 additions & 1 deletion src/mcp/server/fastmcp/utilities/func_metadata.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,11 @@ async def call_fn_with_arg_validation(
if fn_is_async:
return await fn(**arguments_parsed_dict)
else:
return fn(**arguments_parsed_dict)
return await anyio.to_thread.run_sync(lambda: fn(**arguments_parsed_dict))

cubic-dev-ai Bot Jun 27, 2026
edited
Loading

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

P1: Add the missing anyio import before using anyio.to_thread.run_sync; otherwise every synchronous tool call fails at runtime.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At src/mcp/server/fastmcp/utilities/func_metadata.py, line 96:

<comment>Add the missing `anyio` import before using `anyio.to_thread.run_sync`; otherwise every synchronous tool call fails at runtime.</comment>

<file context>
@@ -93,7 +93,11 @@ async def call_fn_with_arg_validation(
             return await fn(**arguments_parsed_dict)
         else:
-            return fn(**arguments_parsed_dict)
+            return await anyio.to_thread.run_sync(lambda: fn(**arguments_parsed_dict))
+            # Sync functions are offloaded to a thread to avoid blocking the event loop.
+            # anyio.to_thread.run_sync() uses copy_context() internally, so contextvars
</file context>

# Sync functions are offloaded to a thread to avoid blocking the event loop.
# anyio.to_thread.run_sync() uses copy_context() internally, so contextvars
# propagate correctly. Context is passed explicitly via arguments_parsed_dict,
# not through contextvars, so it is unaffected.

def convert_result(self, result: Any) -> Any:
"""
Expand Down
Loading

Back | FazBrowse Home | New Git URL