fastmcp/tests/server/tasks
Chris Guidry 9a063963f4
Scope tasks to authorization context, not session (#3800)
* Scope tasks to authorization context, not session

Tasks were keyed by the transport-layer Mcp-Session-Id, which is
server-assigned and changes on reconnect — so clients lost access to
their running tasks after any connection interruption.

The MCP spec says tasks should be bound to authorization context, not
session.  This replaces session_id with task_scope (derived from
AccessToken.client_id, URL-encoded) in all task data Redis keys and
Docket task keys.  When no auth is configured, a "_" sentinel is used
and security comes from UUID task ID entropy per the spec.

Session ID is still used for transport-level concerns (notification
queues, subscriber registration) and is now stored in the
TaskContextSnapshot payload so background workers can still deliver
notifications.

Also extracts all the task context infrastructure (TaskContextInfo,
TaskContextSnapshot, snapshot loading, session/server registries) from
server/dependencies.py into a new server/tasks/context.py to keep the
DI module from sprawling further.

Closes #3758

🤖 Generated with Claude Code

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* Rename _redis_key to _snapshot_redis_key

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* Document in-process session registry as an optimization

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* Tidy imports and docstrings

Hoist imports where safe, keep subscriptions/notifications deferred in
handlers.py since they pull in docket at module level. Sharpen docstrings
on keys.py and context.py so each module owns its lane. Clean up the
re-export block in dependencies.py.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* Fix misleading comment on re-export block

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* Tighten task scope: include sub claim, partition keyspaces

Addresses review feedback on #3800:

- Compose task scope from client_id and the JWT sub claim (when present)
  so fixed-OAuth deployments isolate per user, not just per client.
- Replace the "_" anonymous sentinel with a tagged keyspace partition.
  Docket keys are now auth:{enc_scope}:... or anon:..., and Redis keys
  use fastmcp:task:auth:{enc_scope}:... or fastmcp:task:anon:...,
  routed through a single task_redis_prefix() helper.
- get_task_scope() returns the raw scope (or None); encoding happens
  once at the keys.py boundary, collapsing the previous double-quote
  invariant.
- Drop the dormant fallback in notifications.py that routed
  input_required relays into the anon keyspace when task_scope was
  missing -- log and skip instead.
- Add comprehensive parser/encoder tests in test_task_keys.py covering
  round-trips, malformed keys, and adversarial scopes ("anon", "_", and
  scopes containing : / | %).
- Add cross-scope rejection tests: distinct client_ids, distinct sub
  claims under a shared client_id, and authenticated vs anonymous.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-13 13:50:43 -04:00
..
__init__.py [2.14] SEP-1686 tasks (#2378) 2025-12-04 20:10:35 -05:00
conftest.py Expose minimum_check_interval, reduce task pickup latency (#3500) 2026-03-14 16:08:28 -04:00
test_concurrent_dependencies.py Unify background task context forwarding, fix concurrent dependency bugs (#3710) 2026-04-03 10:48:27 -04:00
test_context_background_task.py Scope tasks to authorization context, not session (#3800) 2026-04-13 13:50:43 -04:00
test_custom_subclass_tasks.py fix: resolve Pyright "Module is not callable" on @tool, @resource, @prompt decorators (#3540) 2026-03-17 18:11:42 -04:00
test_notifications.py Move relay logic to elicitation.py, fix related-task metadata key 2026-02-10 16:00:01 -05:00
test_progress_dependency.py Remove enable_docket setting; Docket is now always on 2025-12-05 11:40:04 -05:00
test_resource_task_meta_parameter.py fix: resolve Pyright "Module is not callable" on @tool, @resource, @prompt decorators (#3540) 2026-03-17 18:11:42 -04:00
test_server_tasks_parameter.py Expose minimum_check_interval, reduce task pickup latency (#3500) 2026-03-14 16:08:28 -04:00
test_sync_function_task_disabled.py Decorators return functions instead of component objects (#2856) 2026-01-12 21:58:07 -05:00
test_task_capabilities.py Version-check is_docket_available() to avoid transitive pydocket crash (#3807) 2026-04-09 21:52:21 -04:00
test_task_config.py fix: resolve Pyright "Module is not callable" on @tool, @resource, @prompt decorators (#3540) 2026-03-17 18:11:42 -04:00
test_task_dependencies.py Unify background task context forwarding, fix concurrent dependency bugs (#3710) 2026-04-03 10:48:27 -04:00
test_task_elicitation_relay.py Relay task elicitation through standard MCP protocol 2026-02-10 14:58:19 -05:00
test_task_keys.py Scope tasks to authorization context, not session (#3800) 2026-04-13 13:50:43 -04:00
test_task_meta_parameter.py fix: resolve Pyright "Module is not callable" on @tool, @resource, @prompt decorators (#3540) 2026-03-17 18:11:42 -04:00
test_task_metadata.py Move relay logic to elicitation.py, fix related-task metadata key 2026-02-10 16:00:01 -05:00
test_task_methods.py Expose minimum_check_interval, reduce task pickup latency (#3500) 2026-03-14 16:08:28 -04:00
test_task_mount.py fix: bump ty to >=0.0.29 and suppress new false positives (#3790) 2026-04-07 19:49:09 -04:00
test_task_prompts.py Refactor Client class into mixins and add timeout utilities (#2933) 2026-01-19 09:54:47 -05:00
test_task_protocol.py fix: stabilize task notification integration tests 2026-02-08 15:20:22 +01:00
test_task_proxy.py Expose minimum_check_interval, reduce task pickup latency (#3500) 2026-03-14 16:08:28 -04:00
test_task_resources.py Refactor Client class into mixins and add timeout utilities (#2933) 2026-01-19 09:54:47 -05:00
test_task_return_types.py fix: retry when LLM returns text instead of calling final_response (#3850) 2026-04-12 12:52:16 -04:00
test_task_security.py Scope tasks to authorization context, not session (#3800) 2026-04-13 13:50:43 -04:00
test_task_status_notifications.py [2.14] SEP-1686 tasks (#2378) 2025-12-04 20:10:35 -05:00
test_task_tools.py Refactor Client class into mixins and add timeout utilities (#2933) 2026-01-19 09:54:47 -05:00
test_task_ttl.py [2.14] SEP-1686 tasks (#2378) 2025-12-04 20:10:35 -05:00