Something went wrong. Try again.
mirror of github.com/PrefectHQ/fastmcp
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153"""The explicit `ToolTask` handle (the return-quickly surface, SEP-2663).
`call_tool_task` returns a `ToolTask` as soon as the server accepts the task, sothe caller can do other work and drive it: `status`, `wait`, `result`, `cancel`,or `await`. This contrasts with `client.call_tool`, which polls to completiontransparently. All tests use a real `Client(mode="auto")` over the in-memorytransport, since tasks are modern-only."""
from __future__ import annotations
import asyncio
import pytestfrom fastmcp_tasks.models import MISSING_REQUIRED_CLIENT_CAPABILITYfrom mcp.shared.exceptions import MCPError
from fastmcp import Context, FastMCPfrom fastmcp.client import Clientfrom fastmcp.exceptions import ToolErrorfrom fastmcp.utilities.tasks import TaskConfigfrom fastmcp_tasks import TasksExtension, ToolTask, call_tool_task
@pytest.fixturedef tool_task_server() -> FastMCP: mcp = FastMCP("tool-task-test") mcp.add_extension(TasksExtension())
@mcp.tool(task=True) async def echo(message: str) -> str: return f"Echo: {message}"
@mcp.tool(task=True) async def multiply(a: int, b: int) -> int: return a * b
@mcp.tool(task=True) async def boom() -> str: raise ValueError("background task failure")
return mcp
async def test_call_tool_task_returns_tool_task(tool_task_server: FastMCP): async with Client(tool_task_server, mode="auto") as client: task = await call_tool_task(client, "echo", {"message": "hello"})
assert isinstance(task, ToolTask) assert isinstance(task.task_id, str) assert task.task_id
async def test_tool_task_result_returns_parsed_result(tool_task_server: FastMCP): async with Client(tool_task_server, mode="auto") as client: task = await call_tool_task(client, "multiply", {"a": 6, "b": 7}) result = await task.result() assert result.data == 42
async def test_tool_task_await_syntax(tool_task_server: FastMCP): async with Client(tool_task_server, mode="auto") as client: task = await call_tool_task(client, "multiply", {"a": 7, "b": 6}) result = await task assert result.data == 42
async def test_tool_task_status_and_wait(tool_task_server: FastMCP): async with Client(tool_task_server, mode="auto") as client: task = await call_tool_task(client, "echo", {"message": "test"})
status = await task.status() assert status.task_id == task.task_id assert status.status in {"working", "completed"}
final = await task.wait(timeout=2.0) assert final.status == "completed"
async def test_tool_task_result_is_cached(tool_task_server: FastMCP): """Repeated result() calls return the same cached object without re-polling.""" async with Client(tool_task_server, mode="auto") as client: task = await call_tool_task(client, "multiply", {"a": 2, "b": 5})
result1 = await task.result() result2 = await task.result() result3 = await task assert result1 is result2 is result3 assert result1.data == 10
async def test_background_task_raises_on_error_by_default(tool_task_server: FastMCP): async with Client(tool_task_server, mode="auto") as client: task = await call_tool_task(client, "boom", {}) with pytest.raises(ToolError, match="background task failure"): await task.result()
async def test_background_task_returns_error_when_not_raising( tool_task_server: FastMCP,): async with Client(tool_task_server, mode="auto") as client: task = await call_tool_task(client, "boom", {}, raise_on_error=False) result = await task.result() assert result.is_error assert "background task failure" in str(result)
async def test_multiple_concurrent_tool_tasks(tool_task_server: FastMCP): async with Client(tool_task_server, mode="auto") as client: tasks = [ (await call_tool_task(client, "multiply", {"a": i, "b": 2}), i * 2) for i in range(5) ] for task, expected in tasks: result = await task.result() assert result.data == expected
async def test_tool_task_cancel(): """A long-running task can be cancelled through the handle.""" mcp = FastMCP("cancel-test") mcp.add_extension(TasksExtension())
@mcp.tool(task=True) async def forever(ctx: Context) -> str: await asyncio.Event().wait() return "never"
async with Client(mcp, mode="auto") as client: task = await call_tool_task(client, "forever", {}) await task.wait(state="working", timeout=2.0) await task.cancel() final = await task.wait(timeout=2.0) assert final.status == "cancelled"
async def test_required_mode_without_optin_raises_32021(): """A legacy client never negotiates the tasks capability, so a required-mode tool call is rejected with the -32021 missing-capability error.""" mcp = FastMCP("required-test") mcp.add_extension(TasksExtension())
@mcp.tool(task=TaskConfig(mode="required")) async def must_task(x: int) -> int: return x
async with Client(mcp, mode="legacy") as client: with pytest.raises(MCPError) as excinfo: await client.call_tool("must_task", {"x": 1})
assert excinfo.value.error.code == MISSING_REQUIRED_CLIENT_CAPABILITY