Something went wrong. Try again.
mirror of github.com/PrefectHQ/fastmcp
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220---title: taskssidebarTitle: tasks---
# `fastmcp.client.tasks`
SEP-1686 client Task classes.
## Classes
### `TaskNotificationHandler` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/client/tasks.py#L26" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
MessageHandler that routes task status notifications to Task objects.
**Methods:**
#### `dispatch` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/client/tasks.py#L33" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythondispatch(self, message: Message) -> None```
Dispatch messages, including task status notifications.
### `Task` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/client/tasks.py#L47" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
Abstract base class for MCP background tasks (SEP-1686).
Provides a uniform API whether the server accepts background executionor executes synchronously (graceful degradation per SEP-1686).
**Methods:**
#### `task_id` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/client/tasks.py#L105" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythontask_id(self) -> str```
Get the task ID.
#### `returned_immediately` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/client/tasks.py#L110" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonreturned_immediately(self) -> bool```
Check if server executed the task immediately.
**Returns:**- True if server executed synchronously (graceful degradation or no task support)- False if server accepted background execution
#### `on_status_change` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/client/tasks.py#L145" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonon_status_change(self, callback: Callable[[GetTaskResult], None | Awaitable[None]]) -> None```
Register callback for status change notifications.
The callback will be invoked when a notifications/tasks/status is receivedfor this task (optional server feature per SEP-1686 lines 436-444).
Supports both sync and async callbacks (auto-detected).
**Args:**- `callback`: Function to call with GetTaskResult when status changes. Can return None (sync) or Awaitable[None] (async).
#### `status` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/client/tasks.py#L171" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonstatus(self) -> GetTaskResult```
Get current task status.
If server executed immediately, returns synthetic completed status.Otherwise queries the server for current status.
#### `result` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/client/tasks.py#L202" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonresult(self) -> TaskResultT```
Wait for and return the task result.
Must be implemented by subclasses to return the appropriate result type.
#### `wait` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/client/tasks.py#L209" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonwait(self) -> GetTaskResult```
Wait for task to reach a specific state or complete.
Uses event-based waiting when notifications are available (fast),with fallback to polling (reliable). Optimally wakes up immediatelyon status changes when server sends notifications/tasks/status.
**Args:**- `state`: Desired state ('submitted', 'working', 'completed', 'failed'). If None, waits for any terminal state (completed/failed)- `timeout`: Maximum time to wait in seconds
**Returns:**- Final task status
**Raises:**- `TimeoutError`: If desired state not reached within timeout
#### `cancel` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/client/tasks.py#L272" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythoncancel(self) -> None```
Cancel this task, transitioning it to cancelled state.
Sends a tasks/cancel protocol request. The server will attempt to haltexecution and move the task to cancelled state.
Note: If server executed immediately (graceful degradation), this is a no-opas there's no server-side task to cancel.
### `ToolTask` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/client/tasks.py#L294" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
Represents a tool call that may execute in background or immediately.
Provides a uniform API whether the server accepts background executionor executes synchronously (graceful degradation per SEP-1686).
**Methods:**
#### `result` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/client/tasks.py#L336" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonresult(self) -> CallToolResult```
Wait for and return the tool result.
If server executed immediately, returns the immediate result.Otherwise waits for background task to complete and retrieves result.
**Returns:**- The parsed tool result (same as call_tool returns)
### `PromptTask` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/client/tasks.py#L396" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
Represents a prompt call that may execute in background or immediately.
Provides a uniform API whether the server accepts background executionor executes synchronously (graceful degradation per SEP-1686).
**Methods:**
#### `result` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/client/tasks.py#L427" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonresult(self) -> mcp.types.GetPromptResult```
Wait for and return the prompt result.
If server executed immediately, returns the immediate result.Otherwise waits for background task to complete and retrieves result.
**Returns:**- The prompt result with messages and description
### `ResourceTask` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/client/tasks.py#L461" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
Represents a resource read that may execute in background or immediately.
Provides a uniform API whether the server accepts background executionor executes synchronously (graceful degradation per SEP-1686).
**Methods:**
#### `result` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/client/tasks.py#L497" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonresult(self) -> list[mcp.types.TextResourceContents | mcp.types.BlobResourceContents]```
Wait for and return the resource contents.
If server executed immediately, returns the immediate result.Otherwise waits for background task to complete and retrieves result.
**Returns:**- list\[ReadResourceContents]: The resource contents