Skip to content

Commit 1c258ae

Browse files
authored
Don't block the event loop on non-async functions (#1909)
1 parent 5fdd48a commit 1c258ae

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/mcp/server/fastmcp/utilities/func_metadata.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
import functools
12
import inspect
23
import json
34
from collections.abc import Awaitable, Callable, Sequence
45
from itertools import chain
56
from types import GenericAlias
67
from typing import Annotated, Any, cast, get_args, get_origin, get_type_hints
78

9+
import anyio
10+
import anyio.to_thread
811
import pydantic_core
912
from pydantic import BaseModel, ConfigDict, Field, WithJsonSchema, create_model
1013
from pydantic.fields import FieldInfo
@@ -53,9 +56,7 @@ def model_dump_one_level(self) -> dict[str, Any]:
5356
kwargs[output_name] = value
5457
return kwargs
5558

56-
model_config = ConfigDict(
57-
arbitrary_types_allowed=True,
58-
)
59+
model_config = ConfigDict(arbitrary_types_allowed=True)
5960

6061

6162
class FuncMetadata(BaseModel):
@@ -85,7 +86,7 @@ async def call_fn_with_arg_validation(
8586
if fn_is_async:
8687
return await fn(**arguments_parsed_dict)
8788
else:
88-
return fn(**arguments_parsed_dict)
89+
return await anyio.to_thread.run_sync(functools.partial(fn, **arguments_parsed_dict))
8990

9091
def convert_result(self, result: Any) -> Any:
9192
"""Convert the result of a function call to the appropriate format for

0 commit comments

Comments
 (0)