fastmcp/tests/cli/test_tasks.py
Chris Guidry 66aaf420c9
[2.14] SEP-1686 tasks (#2378)
* Implement MCP background tasks (SEP-1686) using Docket

Adds support for background task execution via the MCP task protocol,
powered by Docket for task queue management.

- Tools, resources, and prompts can be marked with `task=True` to run async
- Progress dependency for tracking task progress
- CurrentDocket and CurrentWorker dependencies for advanced use cases
- Client API with `.call_tool(..., task=True)` returns task handles
- Task status notifications via subscriptions
- CLI worker command for distributed task processing

Configuration via environment:
- FASTMCP_ENABLE_DOCKET=true
- FASTMCP_ENABLE_TASKS=true
- FASTMCP_DOCKET_URL=redis://... (or memory:// for single-process)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

* Fix tasks example import (TaskStatusResponse → GetTaskResult)

The example was using a non-existent TaskStatusResponse type.
Updated to use mcp.types.GetTaskResult which is what the
on_status_change callback actually receives.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

* Fix env var name in Docket error messages

The error messages referenced FASTMCP_EXPERIMENTAL_ENABLE_DOCKET but the
actual setting is FASTMCP_ENABLE_DOCKET.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

* Remove deprecated code re-added from pre-#2329 branch

- Remove ExtendedEnvSettingsSource (FASTMCP_SERVER_ prefix support)
- Remove dependencies parameter from FastMCP.__init__

* Replace fakeredis git pin with PyPI release

* Remove redundant fakeredis dev dep (pulled via pydocket)

---------

Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: Jeremiah Lowin <153965+jlowin@users.noreply.github.com>
2025-12-04 20:10:35 -05:00

65 lines
2.3 KiB
Python

"""Tests for the fastmcp tasks CLI."""
import pytest
from fastmcp.cli.tasks import check_docket_enabled, tasks_app
from fastmcp.utilities.tests import temporary_settings
class TestCheckDocketEnabled:
"""Test the Docket enabled checker function."""
def test_succeeds_when_docket_enabled_with_redis(self):
"""Test that it succeeds when Docket is enabled with Redis."""
with temporary_settings(
enable_docket=True,
docket__url="redis://localhost:6379/0",
):
check_docket_enabled()
def test_exits_when_docket_not_enabled(self):
"""Test that it exits with helpful error when Docket not enabled."""
with temporary_settings(enable_docket=False):
with pytest.raises(SystemExit) as exc_info:
check_docket_enabled()
assert isinstance(exc_info.value, SystemExit)
assert exc_info.value.code == 1
def test_exits_with_helpful_error_for_memory_url(self):
"""Test that it exits with helpful error for memory:// URLs."""
with temporary_settings(
enable_docket=True,
docket__url="memory://test-123",
):
with pytest.raises(SystemExit) as exc_info:
check_docket_enabled()
assert isinstance(exc_info.value, SystemExit)
assert exc_info.value.code == 1
class TestWorkerCommand:
"""Test the worker command."""
def test_worker_command_parsing(self):
"""Test that worker command parses arguments correctly."""
command, bound, _ = tasks_app.parse_args(["worker", "server.py"])
assert command.__name__ == "worker" # type: ignore[attr-defined]
assert bound.arguments["server_spec"] == "server.py"
class TestTasksAppIntegration:
"""Test the tasks app integration."""
def test_tasks_app_exists(self):
"""Test that the tasks app is properly configured."""
assert "tasks" in tasks_app.name
assert "Docket" in tasks_app.help
def test_tasks_app_has_commands(self):
"""Test that all expected commands are registered."""
# Just verify the app exists and has the right metadata
# Detailed command testing is done in individual test classes
assert "tasks" in tasks_app.name
assert tasks_app.help