Dependency 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].
Get the current task context if running inside a background task worker.This function extracts task information from the Docket execution context.
Returns None if not running in a task context (e.g., foreground execution).Returns:
TaskContextInfo with task_id and session_id, or None if not in a task.
Register a session for Context access in background tasks.Called automatically when a task is submitted to Docket. The session is
stored as a weakref so it doesn’t prevent garbage collection when the
client disconnects.Args:
Register the server for a background task.Called at task-submission time (inside the child server’s call_tool
context) so that background workers can resolve CurrentFastMCP() and
ctx.fastmcp to the child server for mounted tasks.The map is bounded to avoid unbounded growth in long-lived servers.
Evicted entries fall back to the ContextVar (parent server).
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 transform
Returns:
Function with modified signature (same function object, updated signature)
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:
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.
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 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:
The access token if an authenticated user is available, None otherwise.
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:
Legacy Context injection (always works)
Depends() injection (always works - uses docket or vendored DI engine)
Args:
fn: Original function with Context and/or dependencies
Returns:
Async wrapper function without injected parameters
Resolve dependencies for a FastMCP function.This function:
Filters out any dependency parameter names from user arguments (security)
Resolves Depends() parameters via the DI system
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 for
arguments: User arguments (may contain keys that match dependency names,
which will be filtered out)
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:
A dependency that resolves to the active Context instance
Raises:
RuntimeError: If no active context found (during resolution)
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:
A dependency that resolves to the active Docket instance
Raises:
RuntimeError: If not within a FastMCP server context
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:
A dependency that resolves to the active Worker instance
Raises:
RuntimeError: If not within a FastMCP server context
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:
A dependency that resolves to the active Starlette Request
Raises:
RuntimeError: If no HTTP request in context (e.g., STDIO transport)
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:
A dependency that resolves to a dictionary of header name -> value
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:
A dependency that resolves to the active AccessToken
Raises:
RuntimeError: If no authenticated user (use get_access_token() for optional)
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:
A dependency that resolves to the claim value as a string
Raises:
RuntimeError: If no access token is available or claim is missing
Information about the current background task context.Returned by get_task_context() when running inside a Docket worker.
Contains identifiers needed to communicate with the MCP session.
Protocol for progress tracking interface.Defines the common interface between InMemoryProgress (server context)
and Docket’s Progress (worker context).Methods:
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:
FastMCP Progress dependency that works in both server and worker contexts.Handles three execution modes:
In Docket worker: Uses the execution’s progress (observable via Redis)
In FastMCP server with Docket: Falls back to in-memory progress
In FastMCP server without Docket: Uses in-memory progress
This allows tools to use Progress() regardless of whether they’re called
immediately or as background tasks, and regardless of whether pydocket
is installed.Methods: