Something went wrong. Try again.
mirror of github.com/PrefectHQ/fastmcp
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134---title: keyssidebarTitle: keys---
# `fastmcp.server.tasks.keys`
Docket and Redis key encoding for background tasks.
The compound Docket task key embeds the auth boundary so that the parser canreject cross-scope access without consulting Redis. Authenticated andanonymous tasks live in disjoint keyspaces:
auth:{enc_scope}:{client_task_id}:{task_type}:{enc_identifier} anon:{client_task_id}:{task_type}:{enc_identifier}
The same `auth/anon` partition is used for the per-task Redis prefix(``fastmcp:task:auth:{enc_scope}`` vs ``fastmcp:task:anon``) — see``task_redis_prefix``.
``task_scope`` is the raw scope identifier (typically derived from``client_id`` or ``client_id|sub``); encoding happens once, at the boundary,in this module.
## Functions
### `build_task_key` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/tasks/keys.py#L41" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonbuild_task_key(task_scope: str | None, client_task_id: str, task_type: str, component_identifier: str) -> str```
Build Docket task key with embedded metadata.
When ``task_scope`` is ``None`` the task is anonymous and lives in the``anon`` keyspace. Otherwise it lives under ``auth:{enc_scope}``.
**Args:**- `task_scope`: Raw authorization scope, or ``None`` for anonymous tasks- `client_task_id`: Client-provided task ID- `task_type`: Type of task ("tool", "prompt", "resource")- `component_identifier`: Tool name, prompt name, or resource URI
**Returns:**- Encoded task key for Docket
**Examples:**
>>> build_task_key("client-a", "task456", "tool", "my_tool")'auth:client-a:task456:tool:my_tool'>>> build_task_key(None, "task456", "tool", "my_tool")'anon:task456:tool:my_tool'>>> build_task_key("client-a", "task456", "resource", "file://data.txt")'auth:client-a:task456:resource:file%3A%2F%2Fdata.txt'
### `parse_task_key` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/tasks/keys.py#L80" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonparse_task_key(task_key: str) -> TaskKeyParts```
Parse Docket task key to extract metadata.
**Args:**- `task_key`: Encoded task key from Docket
**Returns:**- Dict with keys: ``task_scope`` (``str | None``), ``client_task_id``,- ``task_type``, ``component_identifier``.
**Raises:**- `ValueError`: If the key has an unrecognized tag or wrong segment count.
**Examples:**
>>> parse_task_key("auth:client-a:task456:tool:my_tool")`{'task_scope': 'client-a', 'client_task_id': 'task456', 'task_type': 'tool', 'component_identifier': 'my_tool'}`>>> parse_task_key("anon:task456:tool:my_tool")`{'task_scope': None, 'client_task_id': 'task456', 'task_type': 'tool', 'component_identifier': 'my_tool'}`
### `get_client_task_id_from_key` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/tasks/keys.py#L137" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonget_client_task_id_from_key(task_key: str) -> str```
Extract just the client task ID from a task key.
**Args:**- `task_key`: Full encoded task key
**Returns:**- Client-provided task ID
**Examples:**
>>> get_client_task_id_from_key("auth:client-a:task456:tool:my_tool")'task456'>>> get_client_task_id_from_key("anon:task456:tool:my_tool")'task456'
### `task_redis_prefix` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/tasks/keys.py#L156" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythontask_redis_prefix(task_scope: str | None) -> str```
Return the Redis key prefix that owns a given scope.
Authenticated tasks live under ``fastmcp:task:auth:{enc_scope}``;anonymous tasks live under ``fastmcp:task:anon``. Callers append``f":{task_id}:..."`` to compose the final key.
## Classes
### `TaskKeyParts` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/tasks/keys.py#L23" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
Decoded segments of a Docket task key.
``task_scope`` is ``None`` for anonymous tasks, the raw scope stringotherwise.