π The fast, Pythonic way to build MCP servers and clients.
fix(tools): consume sync generators within dispatched invocation (#5376)
HootRock8a88ef4eede8
diff --git a/fastmcp_slim/fastmcp/tools/function_tool.py b/fastmcp_slim/fastmcp/tools/function_tool.pyindex 1398eabf9..3444cff4d 100644--- a/fastmcp_slim/fastmcp/tools/function_tool.py+++ b/fastmcp_slim/fastmcp/tools/function_tool.py@@ -66,13 +66,17 @@ class _ToolBodyError(Exception):@lru_cache(maxsize=5000)-def _wrap_body_errors(fn: Callable[..., Any]) -> Callable[..., Any]:+def _wrap_body_errors(+ fn: Callable[..., Any], *, materialize_generators: bool = False+) -> Callable[..., Any]:"""Wrap ``fn`` so a ``pydantic.ValidationError`` raised by its body isre-raised as ``_ToolBodyError``.The wrapper preserves ``fn``'s signature and annotations so the cached``TypeAdapter`` validates arguments identically β only body execution is- affected. Argument validation happens before the wrapper is called, so it+ affected. When requested, sync generators are consumed during this same+ invocation so dispatch and dependency lifetimes cover their bodies too.+ Argument validation happens before the wrapper is called, so itkeeps raising a bare ``pydantic.ValidationError``."""if is_coroutine_function(fn):@@ -86,7 +90,10 @@ def _wrap_body_errors(fn: Callable[..., Any]) -> Callable[..., Any]:def wrapper(*args: Any, **kwargs: Any) -> Any:try:- return fn(*args, **kwargs)+ result = fn(*args, **kwargs)+ if materialize_generators and inspect.isgenerator(result):+ return list(result)+ return resultexcept PydanticValidationError as e:raise _ToolBodyError from e@@ -378,8 +385,11 @@ class FunctionTool(Tool):"""from fastmcp.server.dependencies import without_injected_parameters+ # A sync generator's body runs during iteration, not creation. Consume+ # it inside the original invocation, before dispatch/DI can finish.+ body_fn = _wrap_body_errors(self.fn, materialize_generators=True)wrapper_fn = without_injected_parameters(- self.fn, run_in_thread=self.run_in_thread+ body_fn, run_in_thread=self.run_in_thread)# Tag pydantic errors raised by the body so they can be distinguished# from argument-validation errors (which pydantic raises first). See #4128.diff --git a/tests/tools/test_generator_thread_dispatch.py b/tests/tools/test_generator_thread_dispatch.pynew file mode 100644index 000000000..be1ada929--- /dev/null+++ b/tests/tools/test_generator_thread_dispatch.py@@ -0,0 +1,171 @@+"""Generator tool bodies follow the synchronous thread-dispatch policy."""++import asyncio+import functools+import json+import threading+from collections.abc import AsyncIterator, Callable, Iterator+from contextlib import contextmanager+from typing import Any++import pytest+from mcp_types import TextContent++from fastmcp import Client, Context, FastMCP+from fastmcp.dependencies import Depends+from fastmcp.exceptions import ToolError+++def _sync_wrapper(fn: Callable[..., Any]) -> Callable[..., Any]:+ @functools.wraps(fn)+ def wrapped(*args: Any, **kwargs: Any) -> Any:+ return fn(*args, **kwargs)++ return wrapped++[email protected]("run_in_thread", [True, False])[email protected]("inject_context", [True, False])+async def test_sync_generator_obeys_thread_dispatch(+ run_in_thread: bool, inject_context: bool+) -> None:+ mcp = FastMCP()+ loop_thread = threading.get_ident()++ if inject_context:++ @mcp.tool(run_in_thread=run_in_thread)+ def thread_ids(ctx: Context) -> Iterator[int]:+ assert ctx.fastmcp is mcp+ yield threading.get_ident()+ yield threading.get_ident()++ else:++ @mcp.tool(run_in_thread=run_in_thread)+ def thread_ids() -> Iterator[int]:+ yield threading.get_ident()+ yield threading.get_ident()++ async with Client(mcp) as client:+ result = await client.call_tool("thread_ids")++ assert isinstance(result.content[0], TextContent)+ ids = json.loads(result.content[0].text)+ assert len(ids) == 2+ assert all((tid != loop_thread) == run_in_thread for tid in ids)++[email protected]("run_in_thread", [True, False])+async def test_sync_factory_generator_obeys_thread_dispatch(+ run_in_thread: bool,+) -> None:+ mcp = FastMCP()+ loop_thread = threading.get_ident()++ @mcp.tool(run_in_thread=run_in_thread)+ def thread_ids() -> Iterator[int]:+ return (threading.get_ident() for _ in range(2))++ async with Client(mcp) as client:+ result = await client.call_tool("thread_ids")++ assert isinstance(result.content[0], TextContent)+ ids = json.loads(result.content[0].text)+ assert len(ids) == 2+ assert all((tid != loop_thread) == run_in_thread for tid in ids)++[email protected]("run_in_thread", [True, False])+async def test_async_generator_stays_on_event_loop(run_in_thread: bool) -> None:+ mcp = FastMCP()+ loop_thread = threading.get_ident()++ @mcp.tool(run_in_thread=run_in_thread)+ async def thread_ids() -> AsyncIterator[int]:+ yield threading.get_ident()++ async with Client(mcp) as client:+ result = await client.call_tool("thread_ids")++ assert isinstance(result.content[0], TextContent)+ assert json.loads(result.content[0].text) == [loop_thread]++[email protected]("run_in_thread", [True, False])+async def test_async_factory_generator_stays_on_event_loop(run_in_thread: bool) -> None:+ mcp = FastMCP()+ loop_thread = threading.get_ident()++ @mcp.tool(run_in_thread=run_in_thread)+ async def thread_ids() -> Iterator[int]:+ return (threading.get_ident() for _ in range(2))++ async with Client(mcp) as client:+ result = await client.call_tool("thread_ids")++ assert isinstance(result.content[0], TextContent)+ assert json.loads(result.content[0].text) == [loop_thread, loop_thread]++[email protected]("run_in_thread", [True, False])+async def test_generator_iteration_error_is_tool_error(run_in_thread: bool) -> None:+ mcp = FastMCP()++ @mcp.tool(run_in_thread=run_in_thread)+ def broken() -> Iterator[int]:+ yield 1+ raise ValueError("generator failed")++ async with Client(mcp) as client:+ with pytest.raises(ToolError, match="generator failed"):+ await client.call_tool("broken")++[email protected]("inject_context", [True, False])+async def test_sync_wrapper_of_async_generator_factory_stays_on_loop(+ inject_context: bool,+) -> None:+ mcp = FastMCP()++ if inject_context:++ async def thread_ids(ctx: Context) -> Iterator[bool]:+ assert ctx.fastmcp is mcp+ return (asyncio.get_running_loop().is_running() for _ in range(2))++ else:++ async def thread_ids() -> Iterator[bool]:+ return (asyncio.get_running_loop().is_running() for _ in range(2))++ mcp.tool(_sync_wrapper(thread_ids))+ async with Client(mcp) as client:+ result = await client.call_tool("thread_ids")++ assert isinstance(result.content[0], TextContent)+ assert json.loads(result.content[0].text) == [True, True]+++async def test_sync_generator_consumed_before_dependency_cleanup() -> None:+ mcp = FastMCP()+ state = {"open": False}++ @contextmanager+ def connection() -> Iterator[dict[str, bool]]:+ state["open"] = True+ try:+ yield state+ finally:+ state["open"] = False++ @mcp.tool+ def query(resource: dict[str, bool] = Depends(connection)) -> Iterator[bool]:+ yield resource["open"]++ async with Client(mcp) as client:+ result = await client.call_tool("query")++ assert isinstance(result.content[0], TextContent)+ assert json.loads(result.content[0].text) == [True]+ assert state["open"] is False