Skip to content

get_access_token returns None in background tasks (task=True) #3095

@cristiangreco94

Description

@cristiangreco94

Enhancement

I'm experiencing an issue where the get_access_token dependency returns None when used in tools marked with task=True, even though authentication is properly configured and
working.

Setup:

  • FastMCP server with JWT authentication configured
  • Tool functions using task=True for background execution
  • Attempting to use get_access_token dependency to access authenticated user info

Problem:
When a tool is marked with task=True, the access_token parameter injected via Depends(get_access_token) is always None, even when the request is properly authenticated. The
same dependency works fine in non-background tools.

Minimal reproducible example:

from fastmcp import FastMCP
from fastmcp.server.auth import JWTVerifier, AccessToken
from fastmcp.server.dependencies import get_access_token
from fastmcp.dependencies import Depends

# Server with JWT auth
jwks_uri = "https://cognito-idp.us-east-1.amazonaws.com/us-east-1_XXXXX/.well-known/jwks.json"
auth = JWTVerifier(jwks_uri=jwks_uri)
mcp = FastMCP("TestServer", auth=auth)

# This works - access_token is populated
@mcp.tool(name="test_sync")
async def test_sync(
  access_token: AccessToken | None = Depends(get_access_token),
) -> str:
  if access_token:
      return f"Token: {access_token.token[:20]}..."
  return "No token!"  # Never reaches here when authenticated

# This doesn't work - access_token is always None
@mcp.tool(name="test_background", task=True)
async def test_background(
  access_token: AccessToken | None = Depends(get_access_token),
) -> str:
  if access_token:
      return f"Token: {access_token.token[:20]}..."
  return "No token!"  # Always returns this, even with valid auth

Expected behavior:
Dependencies should be resolved in the request context before the background task is spawned, so access_token should contain the authenticated user's token.

Actual behavior:
access_token is None in background tasks, making it impossible to access authenticated user information in task=True functions.

Questions:

  1. Is this a known limitation of how background tasks handle dependency injection?
  2. Is there a recommended pattern for accessing authentication context in background tasks?
  3. I noticed LifespanContext is always empty in background tasks as well, is this expected?
  4. Should dependencies be resolved before spawning the background task, or is the current behavior intentional?

Any guidance would be appreciated! Thanks for the great framework.

Metadata

Metadata

Assignees

No one assigned

    Labels

    authRelated to authentication (Bearer, JWT, OAuth, WorkOS) for client or server.bugSomething isn't working. Reports of errors, unexpected behavior, or broken functionality.duplicateDuplicates an existing open issue. Reference the original issue when applying.serverRelated to FastMCP server implementation or server-side functionality.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions