Skip to content

πŸš€ The fast, Pythonic way to build MCP servers and clients.

Make background task execution TTL configurable (#6428)

nate nowackacae5e6777f1
diff --git a/docs/servers/tasks.mdx b/docs/servers/tasks.mdx
index 4c95cafd7..5e07c8b86 100644
--- a/docs/servers/tasks.mdx
+++ b/docs/servers/tasks.mdx
@@ -168,6 +168,7 @@ mcp.add_extension(TasksExtension(url="redis://localhost:6379/0", concurrency=20)
| `FASTMCP_DOCKET_URL` | `memory://` | Backend URL (`memory://` or `redis://host:port/db`) |
| `FASTMCP_DOCKET_NAME` | `fastmcp` | Queue name. Servers and workers sharing a name and URL share a queue. |
| `FASTMCP_DOCKET_CONCURRENCY` | `10` | Maximum concurrent tasks per worker. |
+| `FASTMCP_DOCKET_EXECUTION_TTL` | `PT15M` | How long task state and results are kept, reported to clients as `ttlMs`. Accepts an ISO 8601 duration or seconds. |
| `FASTMCP_TASKS_ENCRYPTION_KEY` | (unset) | Encrypts [task context snapshots at rest](#credentials-at-rest). Every server and worker sharing a queue must set the same key. |
## Backends
diff --git a/fastmcp_tasks/fastmcp_tasks/extension.py b/fastmcp_tasks/fastmcp_tasks/extension.py
index 6c9488e90..62a836b10 100644
--- a/fastmcp_tasks/fastmcp_tasks/extension.py
+++ b/fastmcp_tasks/fastmcp_tasks/extension.py
@@ -106,6 +106,7 @@ class TasksExtension(ServerExtension):
redelivery_timeout: timedelta | None = None,
reconnection_delay: timedelta | None = None,
minimum_check_interval: timedelta | None = None,
+ execution_ttl: timedelta | None = None,
) -> None:
if credential_provider is not None:
from redis.credentials import StreamingCredentialProvider
@@ -126,6 +127,7 @@ class TasksExtension(ServerExtension):
"redelivery_timeout": redelivery_timeout,
"reconnection_delay": reconnection_delay,
"minimum_check_interval": minimum_check_interval,
+ "execution_ttl": execution_ttl,
}
self._settings = DocketSettings(
**{k: v for k, v in overrides.items() if v is not None}
diff --git a/fastmcp_tasks/fastmcp_tasks/lifespan.py b/fastmcp_tasks/fastmcp_tasks/lifespan.py
index 0e90b3aba..fd56552d8 100644
--- a/fastmcp_tasks/fastmcp_tasks/lifespan.py
+++ b/fastmcp_tasks/fastmcp_tasks/lifespan.py
@@ -72,7 +72,10 @@ async def docket_lifespan(
return
async with Docket(
- name=settings.name, url=settings.url, credential_provider=credential_provider
+ name=settings.name,
+ url=settings.url,
+ execution_ttl=settings.execution_ttl,
+ credential_provider=credential_provider,
) as docket:
server._docket = docket
for component in task_components:
diff --git a/fastmcp_tasks/fastmcp_tasks/settings.py b/fastmcp_tasks/fastmcp_tasks/settings.py
index 8dcc96504..a179c1e88 100644
--- a/fastmcp_tasks/fastmcp_tasks/settings.py
+++ b/fastmcp_tasks/fastmcp_tasks/settings.py
@@ -125,6 +125,20 @@ class DocketSettings(BaseSettings):
),
] = timedelta(milliseconds=50)
+ execution_ttl: Annotated[
+ timedelta,
+ Field(
+ description=inspect.cleandoc(
+ """
+ How long a task's state and result are kept in the Docket
+ backend. Clients see this as the task's ttlMs. Raise it for
+ tasks that run, or whose results are retrieved, after more
+ than the default 15 minutes.
+ """
+ ),
+ ),
+ ] = timedelta(minutes=15)
+
docket_settings = DocketSettings()
diff --git a/tests/tasks/server/test_task_ttl.py b/tests/tasks/server/test_task_ttl.py
index 6eb64a4ad..e55a3de7a 100644
--- a/tests/tasks/server/test_task_ttl.py
+++ b/tests/tasks/server/test_task_ttl.py
@@ -8,6 +8,7 @@ execution TTL (900000 ms) when none is configured.
from __future__ import annotations
import asyncio
+from datetime import timedelta
from fastmcp import FastMCP
from fastmcp_tasks import TasksExtension
@@ -71,6 +72,28 @@ async def test_default_ttl_when_unspecified():
assert got.ttl_ms == DEFAULT_TTL_MS
+async def test_configured_execution_ttl_reported():
+ """A configured execution TTL reaches Docket and is reported as ttlMs."""
+ mcp = FastMCP("ttl-config-test")
+ mcp.add_extension(TasksExtension(execution_ttl=timedelta(hours=2)))
+
+ @mcp.tool(task=True)
+ async def quick_task(value: int) -> int:
+ return value * 2
+
+ async with running_task_server(mcp):
+ created = await submit_task(mcp, "quick_task", {"value": 1})
+ assert created.ttl_ms == 2 * 60 * 60 * 1000
+ final = await wait_for_task(mcp, created.task_id)
+ assert final.ttl_ms == 2 * 60 * 60 * 1000
+
+
+def test_execution_ttl_reads_from_env(monkeypatch):
+ """FASTMCP_DOCKET_EXECUTION_TTL configures the execution TTL."""
+ monkeypatch.setenv("FASTMCP_DOCKET_EXECUTION_TTL", "PT2H")
+ assert TasksExtension().docket_settings.execution_ttl == timedelta(hours=2)
+
+
async def test_poll_refreshes_routing_key_ttl():
"""A poll extends the current-leg pointer's TTL (sliding expiration).