docs/python-sdk/fastmcp-server-dependencies.mdx
fastmcp.server.dependenciesDependency injection for FastMCP.
DI features (Depends, CurrentContext, CurrentFastMCP) work without pydocket using the uncalled-for DI engine. Only task-related dependencies (CurrentDocket, CurrentWorker) and background task execution require fastmcp[tasks].
is_docket_available <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L114" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>is_docket_available() -> bool
Check if a compatible pydocket (>= 0.19.0) is installed and importable.
Three things have to be true for fastmcp's task features to work:
_MIN_DOCKET_VERSION (older versions are
missing symbols like docket.dependencies.current_execution,
which fastmcp imports on the request hot path)import docket blows upAny of those failing means we treat docket as unavailable and fall back to the no-tasks code paths instead of crashing deep inside a request.
require_docket <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L143" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>require_docket(feature: str) -> None
Raise ImportError with install instructions if docket not available.
Args:
feature: Description of what requires docket (e.g., "task=True",
"CurrentDocket()"). Will be included in the error message.transform_context_annotations <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L183" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>transform_context_annotations(fn: Callable[..., Any]) -> Callable[..., Any]
Transform ctx: Context into ctx: Context = CurrentContext().
Transforms ALL params typed as Context to use Docket's DI system, unless they already have a Dependency-based default (like CurrentContext()).
This unifies the legacy type annotation DI with Docket's Depends() system, allowing both patterns to work through a single resolution path.
Note: Only POSITIONAL_OR_KEYWORD parameters are reordered (params with defaults after those without). KEYWORD_ONLY parameters keep their position since Python allows them to have defaults in any order.
Args:
fn: Function to transformReturns:
get_context <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L324" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>get_context() -> Context
Get the current FastMCP Context instance directly.
get_server <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L334" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>get_server() -> FastMCP
Get the current FastMCP server instance directly.
In a background-task worker, checks the task-server map first so that mounted-child tasks resolve to the child server (not the parent that started the worker).
Returns:
Raises:
RuntimeError: If no server in contextget_http_request <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L364" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>get_http_request() -> Request
Get the current HTTP request.
Tries MCP SDK's request_ctx first, then falls back to FastMCP's HTTP context. In background tasks, returns a synthetic request populated with the snapshotted headers from the originating HTTP request.
get_http_headers <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L411" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>get_http_headers(include_all: bool = False, include: set[str] | None = None) -> dict[str, str]
Extract headers from the current HTTP request if available.
Never raises an exception, even if there is no active HTTP request (in which case an empty dict is returned).
By default, strips problematic headers like content-length and authorization
that cause issues if forwarded to downstream services. If include_all is True,
all headers are returned.
The include parameter allows specific headers to be included even if they would
normally be excluded. This is useful for proxy transports that need to forward
authorization headers to upstream MCP servers.
get_access_token <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L468" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>get_access_token() -> AccessToken | None
Get the FastMCP access token from the current context.
This function first tries to get the token from the current HTTP request's scope, which is more reliable for long-lived connections where the SDK's auth_context_var may become stale after token refresh. Falls back to the SDK's context var if no request is available. In background tasks (Docket workers), falls back to the token snapshot stored in Redis at task submission time.
Returns:
without_injected_parameters <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L539" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>without_injected_parameters(fn: Callable[..., Any]) -> Callable[..., Any]
Create a wrapper function without injected parameters.
Returns a wrapper that excludes Context and Docket dependency parameters, making it safe to use with Pydantic TypeAdapter for schema generation and validation. The wrapper internally handles all dependency resolution and Context injection when called.
Handles:
Args:
fn: Original function with Context and/or dependenciesReturns:
resolve_dependencies <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L688" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>resolve_dependencies(fn: Callable[..., Any], arguments: dict[str, Any]) -> AsyncGenerator[dict[str, Any], None]
Resolve dependencies for a FastMCP function.
This function:
The filtering prevents external callers from overriding injected parameters by providing values for dependency parameter names. This is a security feature.
Note: Context injection is handled via transform_context_annotations() which
converts ctx: Context to ctx: Context = Depends(get_context) at registration
time, so all injection goes through the unified DI system.
Args:
fn: The function to resolve dependencies forarguments: User arguments (may contain keys that match dependency names,
which will be filtered out)CurrentContext <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L831" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>CurrentContext() -> Context
Get the current FastMCP Context instance.
This dependency provides access to the active FastMCP Context for the current MCP operation (tool/resource/prompt call).
Returns:
Raises:
RuntimeError: If no active context found (during resolution)OptionalCurrentContext <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L856" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>OptionalCurrentContext() -> Context | None
Get the current FastMCP Context, or None when no context is active.
CurrentDocket <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L891" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>CurrentDocket() -> Docket
Get the current Docket instance managed by FastMCP.
This dependency provides access to the Docket instance that FastMCP automatically creates for background task scheduling.
Returns:
Raises:
RuntimeError: If not within a FastMCP server contextImportError: If fastmcp[tasks] not installedCurrentWorker <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L947" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>CurrentWorker() -> Worker
Get the current Docket Worker instance managed by FastMCP.
This dependency provides access to the Worker instance that FastMCP automatically creates for background task processing.
Returns:
Raises:
RuntimeError: If not within a FastMCP server contextImportError: If fastmcp[tasks] not installedCurrentFastMCP <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L988" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>CurrentFastMCP() -> FastMCP
Get the current FastMCP server instance.
This dependency provides access to the active FastMCP server.
Returns:
Raises:
RuntimeError: If no server in context (during resolution)CurrentRequest <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L1028" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>CurrentRequest() -> Request
Get the current HTTP request.
This dependency provides access to the Starlette Request object for the current HTTP request. Only available when running over HTTP transports (SSE or Streamable HTTP).
Returns:
Raises:
RuntimeError: If no HTTP request in context (e.g., STDIO transport)CurrentHeaders <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L1069" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>CurrentHeaders() -> dict[str, str]
Get the current HTTP request headers.
This dependency provides access to the HTTP headers for the current request, including the authorization header. Returns an empty dictionary when no HTTP request is available, making it safe to use in code that might run over any transport.
Returns:
CurrentAccessToken <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L1287" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>CurrentAccessToken() -> AccessToken
Get the current access token for the authenticated user.
This dependency provides access to the AccessToken for the current authenticated request. Raises an error if no authentication is present.
Returns:
Raises:
RuntimeError: If no authenticated user (use get_access_token() for optional)TokenClaim <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L1344" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>TokenClaim(name: str) -> str
Get a specific claim from the access token.
This dependency extracts a single claim value from the current access token. It's useful for getting user identifiers, roles, or other token claims without needing the full token object.
Args:
name: The name of the claim to extract (e.g., "oid", "sub", "email")Returns:
Raises:
RuntimeError: If no access token is available or claim is missingProgressLike <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L1097" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>Protocol for progress tracking interface.
Defines the common interface between InMemoryProgress (server context) and Docket's Progress (worker context).
Methods:
current <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L1105" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>current(self) -> int | None
Current progress value.
total <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L1110" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>total(self) -> int
Total/target progress value.
message <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L1115" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>message(self) -> str | None
Current progress message.
set_total <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L1119" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>set_total(self, total: int) -> None
Set the total/target value for progress tracking.
increment <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L1123" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>increment(self, amount: int = 1) -> None
Atomically increment the current progress value.
set_message <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L1127" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>set_message(self, message: str | None) -> None
Update the progress status message.
InMemoryProgress <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L1132" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>In-memory progress tracker for immediate tool execution.
Provides the same interface as Docket's Progress but stores state in memory instead of Redis. Useful for testing and immediate execution where progress doesn't need to be observable across processes.
Methods:
current <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L1157" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>current(self) -> int | None
total <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L1161" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>total(self) -> int
message <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L1165" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>message(self) -> str | None
set_total <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L1168" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>set_total(self, total: int) -> None
Set the total/target value for progress tracking.
increment <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L1174" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>increment(self, amount: int = 1) -> None
Atomically increment the current progress value.
set_message <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L1183" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>set_message(self, message: str | None) -> None
Update the progress status message.
Progress <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L1188" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>Progress dependency that works in both server and worker contexts.
In a Docket worker, delegates to the execution's Redis-backed progress (observable across processes). Otherwise, uses in-memory tracking.
The shared default instance acts as a stateless factory — __aenter__
creates a fresh Progress per invocation so concurrent tasks never
share mutable state.
Methods:
current <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L1229" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>current(self) -> int | None
Current progress value.
total <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L1235" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>total(self) -> int
Total/target progress value.
message <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L1241" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>message(self) -> str | None
Current progress message.
set_total <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L1246" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>set_total(self, total: int) -> None
Set the total/target value for progress tracking.
increment <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L1251" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>increment(self, amount: int = 1) -> None
Atomically increment the current progress value.
set_message <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L1256" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>set_message(self, message: str | None) -> None
Update the progress status message.