Something went wrong. Try again.
mirror of github.com/PrefectHQ/fastmcp
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859"""Protocol-level task behavior for SEP-2663 tasks.
Generic protocol behaviors driven in-process via the task helpers: a submittedtask carries a server-generated id and a TTL, and a task whose tool raisessurfaces its error rather than a result."""
from __future__ import annotations
from fastmcp import FastMCPfrom fastmcp.exceptions import ToolErrorfrom fastmcp_tasks import TasksExtensionfrom tests.tasks.task_helpers import ( run_task, running_task_server, submit_task,)
def _task_server() -> FastMCP: mcp = FastMCP("task-test-server") mcp.add_extension(TasksExtension())
@mcp.tool(task=True) async def simple_tool(message: str) -> str: return f"Processed: {message}"
@mcp.tool(task=True) async def failing_tool() -> str: raise ToolError("This tool always fails")
return mcp
async def test_task_metadata_includes_task_id_and_ttl(): """A submitted task carries a server-generated id and a positive TTL.""" mcp = _task_server() async with running_task_server(mcp): created = await submit_task(mcp, "simple_tool", {"message": "test"}) assert isinstance(created.task_id, str) assert created.task_id assert created.ttl_ms is not None and created.ttl_ms > 0
async def test_raised_tool_error_completes_with_is_error(): """A task whose tool raises completes with an is_error result (SEP-2663).
`failed` is reserved for protocol faults; a raised tool error is the same `isError` result a live tools/call returns. """ mcp = _task_server() async with running_task_server(mcp): final = await run_task(mcp, "failing_tool", {}) assert final.status == "completed" assert final.error is None assert final.result is not None assert final.result["isError"] is True assert "This tool always fails" in final.result["content"][0]["text"]