Skip to content

get_access_token() doesn't work with cluster redis #3897

@rain87

Description

@rain87

What happened?

In https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L400, only non-cluster redis client is supported. Therefore, when FASTMCP_DOCKET_URL points to redis+cluster://, get_access_token always returns None when used in background task, and warning log is printed Failed to load task snapshot via sync Redis

The actual persisting of the token relies on docket.redis which correctly handles cluster protocol - so data is stored correctly in https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/dependencies.py#L277

Example Code

import asyncio
import os
from datetime import datetime
from uuid import uuid4

from docket import Docket
from docket.dependencies import current_docket, current_execution
from docket.execution import Execution
from fastmcp.server.auth import AccessToken
from fastmcp.server.dependencies import TaskContextSnapshot, get_access_token

URL = os.getenv("FASTMCP_DOCKET_URL", "redis+cluster://127.0.0.1:7000/0")
print(f"testing with redis@{URL}")


async def main() -> None:
    session_id = "s1"
    task_id = "t1"
    token_orig = AccessToken(
        token="token",
        client_id="client_id",
        scopes=["scope1", "scope2"],
        claims={"claim1": "value1"},
    )
    task_context = TaskContextSnapshot(access_token_json=token_orig.model_dump_json())
    async with Docket(url=URL, name=f"repro_{uuid4()}") as docket:
        current_docket.set(docket)
        current_execution.set(
            Execution(
                docket=docket,
                function=main,
                args=(),
                kwargs={},
                key=f"{session_id}:{task_id}:tool:main",
                when=datetime.now(),
                attempt=0,
            )
        )
        await task_context.save(docket, session_id, task_id, 60)

        token = get_access_token()
        print(f"Token from task context: {token}")


asyncio.run(main())

Version Information

FastMCP version:                                                              3.2.3
MCP version:                                                                 1.27.0
Python version:                                                             3.13.13
Platform:                             Linux-6.12.76-linuxkit-aarch64-with-glibc2.39
FastMCP root path: /workspace/ata_portal/backend/.venv/lib/python3.13/site-packages

Metadata

Metadata

Assignees

Labels

authRelated to authentication (Bearer, JWT, OAuth, WorkOS) for client or server.bugSomething isn't working. Reports of errors, unexpected behavior, or broken functionality.serverRelated to FastMCP server implementation or server-side functionality.tasksRelated to background tasks, Docket, SEP-1686.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions