Something went wrong. Try again.
mirror of github.com/PrefectHQ/fastmcp
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697---title: elicitationsidebarTitle: elicitation---
# `fastmcp.server.tasks.elicitation`
Background task elicitation support (SEP-1686).
This module provides elicitation capabilities for background tasks runningin Docket workers. Unlike regular MCP requests, background tasks don't havean active request context, so elicitation requires special handling:
1. Set task status to "input_required" via Redis2. Send notifications/tasks/status with elicitation metadata3. Wait for client to send input via tasks/sendInput4. Resume task execution with the provided input
This uses the public MCP SDK APIs where possible, with minimal use ofinternal APIs for background task coordination.
## Functions
### `elicit_for_task` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/tasks/elicitation.py#L42" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonelicit_for_task(task_id: str, session: ServerSession | None, message: str, schema: dict[str, Any], fastmcp: FastMCP) -> mcp.types.ElicitResult```
Send an elicitation request from a background task.
This function handles the complexity of eliciting user input when runningin a Docket worker context where there's no active MCP request.
**Args:**- `task_id`: The background task ID- `session`: The MCP ServerSession for this task- `message`: The message to display to the user- `schema`: The JSON schema for the expected response- `fastmcp`: The FastMCP server instance
**Returns:**- ElicitResult containing the user's response
**Raises:**- `RuntimeError`: If Docket is not available- `McpError`: If the elicitation request fails
### `relay_elicitation` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/tasks/elicitation.py#L234" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonrelay_elicitation(session: ServerSession, session_id: str, task_id: str, elicitation: dict[str, Any], fastmcp: FastMCP) -> None```
Relay elicitation from a background task worker to the client.
Called by the notification subscriber when it detects an input_requirednotification with elicitation metadata. Sends a standard elicitation/createrequest to the client session, then uses handle_task_input() to push theresponse to Redis so the blocked worker can resume.
**Args:**- `session`: MCP ServerSession- `session_id`: Session identifier- `task_id`: Background task ID- `elicitation`: Elicitation metadata (message, requestedSchema)- `fastmcp`: FastMCP server instance
### `handle_task_input` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/tasks/elicitation.py#L290" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonhandle_task_input(task_id: str, session_id: str, action: str, content: dict[str, Any] | None, fastmcp: FastMCP) -> bool```
Handle input sent to a background task via tasks/sendInput.
This is called when a client sends input in response to an elicitationrequest from a background task.
**Args:**- `task_id`: The background task ID- `session_id`: The MCP session ID- `action`: The elicitation action ("accept", "decline", "cancel")- `content`: The response content (for "accept" action)- `fastmcp`: The FastMCP server instance
**Returns:**- True if the input was successfully stored, False otherwise