Skip to content

Asynchronous Execution, Queues & Durable Workflows

litestar-mcp supports two complementary execution models for background MCP Tasks (io.modelcontextprotocol/tasks), multi-step DAG workflows (DynamicWorkflow), and streaming agent turns (AgentRuntime):

  1. Single-Process In-Memory Execution: Built into the base litestar-mcp package with zero external infrastructure dependencies. Ideal for local development, litestar mcp stdio servers, and single-replica deployments.
  2. Distributed Queue Execution (litestar-mcp[queues]): Powered by litestar-queues, Litestar's StoreRegistry, and ChannelsPlugin. Scales tool tasks, DAG nodes, and long-running agent turns across horizontally autoscaled worker replicas.

Single-Process vs Distributed Execution Matrix

Capability Single-Process (Base Package) Distributed (litestar-mcp[queues]) Shared State & Coordination
MCP Tasks (io.modelcontextprotocol/tasks) Built-in in-process runner (MCPTaskConfig(executor="in_process") or default executor=None without QueuePlugin; process-local asyncio.Task runners in MCPHandlerService and MCPTaskStore) QueueTaskExecutor (auto-wired when QueuePlugin is registered; dispatches litestar_mcp.mcp.run_tool_task) MCPTaskStore backed by the "litestar_mcp_tasks" Litestar Store + mcp-task:{id}:job tracking
Principal & DI Context Clones the active HTTP Request scope in-memory (stripping request-scoped Dishka containers for fresh resolution) Captures PrincipalSnapshot (user, auth, litestar-security SecurityContext) and rehydrates Provide + Dishka Scope.REQUEST in workers ToolTaskCall persisted in TaskRecord.dispatch (dispatchInternal)
Cooperative Cancellation & input_required Resume Local asyncio.Event / asyncio.Queue signalling within the originating process Cross-replica QueueService.cancel_task(include_running=True) and stateless executor.resume(call) re-enqueueing on tasks/update Shared Store record + litestar-queues durable cancellation signal
DAG Workflows (DynamicWorkflow) WorkflowEngine (topological wave execution via in-process AnyIO TaskGroup and memory streams) DurableWorkflowEngine (plugin injecting workflow_engine, scheduling advance_workflow, execute_workflow_node, and watch_workflow) WorkflowStore backed by "litestar_mcp_workflows" with single-writer run and write-once node keys
Agent Turn Execution & SSE Streaming AgentChatController (POST /agent/chat and POST /agent/turns running inline inside the HTTP request) QueuedAgentChatController + QueuedTurnManager (POST /agent/turns returns 202 Accepted; workers run litestar_mcp.agent.run_turn) LitestarStoreSessionStore + ChannelsPlugin history replay and live AgentStreamFrame fan-out

Architecture Overview

The interactive diagram below illustrates how task-augmented tools/call requests, principal snapshots, cooperative cancellation, and multi-round MCPInputRequiredResult resumptions flow through QueueTaskExecutor and the built-in in-process task runner:

Distributed QueueTaskExecutor and Task Progress/Log Forwarding on litestar-queues tools/call with params.task persists TaskRecord and ToolTaskCall (progress_token, session_id, log_level, PrincipalSnapshot) in MCPTaskStore, runs on litestar-queues workers calling ctx.report_progress() and ctx.log() to update MCPTaskStore.update_status_message() and fan out notifications/progress onto strictly matched 2025-* GET /mcp session SSE streams (while 2026-07-28 POST subscriptions/listen carries notifications/tasks and opted-in logLevel notifications/message while excluding notifications/progress), and refreshes log_level from MCPSessionStore on tasks/update. Task Promotion tools/call (params.task) Captures PrincipalSnapshot & owner_id ToolTaskCall.progress_token Persists session_id & log_level request.user & SecurityContext Persists TaskRecord before return Returns CreateTaskResult Durable Store & Broker MCPTaskStore + QueuePlugin Store: litestar_mcp_tasks (TTL) MCPTaskStore.update_status_message() QueueTaskExecutor.submit() / resume() MCPSessionStore log_level refresh on tasks/update • queue="..." Postgres / Redis / SQLSpec Worker Rehydration Replica Worker Execution Rehydrates user, auth & Dishka Restores progress_token & log_level ctx.report_progress() -> statusMessage ctx.log() RFC 5424 severity filter ChannelsPlugin cross-replica fan-out Cooperative CancelScope SSE & Task Control tasks/get • update • cancel 2025-11-25 GET /mcp SSE push: notifications/progress notifications/message MCPInputRequiredResult pause tasks/update refreshes log_level Cross-Replica Safe Cross-Replica Progress, Logging & Transport Isolation Worker ctx.report_progress() & ctx.log() -> update_status_message() + ChannelsPlugin -> 2025-11-25 GET /mcp SSE 2026-07-28 subscriptions/listen: notifications/tasks + opted-in logLevel (excludes progress) | tasks/get statusMessage
Built-In MCPTaskStore, Promoted Progress/Logging, and Elicitation Lifecycle Built-in MCPTaskStore persists strictly owner-scoped TaskRecord and ToolTaskCall metadata (progress_token, session_id, log_level), updates TaskRecord.statusMessage via MCPTaskStore.update_status_message(), and forwards notifications/progress and notifications/message onto 2025-* GET /mcp session SSE streams while 2026-07-28 subscriptions/listen carries taskIds and opted-in logLevel events (excluding progress). Promoted Context tools/call (params.task) Validates task="optional" | "required" Captures ToolTaskCall(progress_token, session_id, log_level, owner_id) Single-writer Store keys with TTL Detaches runner after HTTP return Immediate taskId handle MCPTaskStore State Machine litestar_mcp_tasks Store working -> input_required -> completed Executes tool via internal ASGI dispatch MCPTaskStore.update_status_message() Updates TaskRecord.statusMessage live Filters ctx.log() by session log_level Zero external broker required Session SSE, Poll & Resume GET /mcp SSE • tasks/get • update Forwards notifications/progress & message onto 2025-11-25 GET /mcp session SSE subscriptions/listen: tasks + logLevel only tasks/get returns statusMessage & result tasks/update resumes & refreshes log_level Owner-isolated lookup & tasks/cancel Post-Promotion Progress, RFC 5424 Logging & Transport Rules ToolTaskCall(progress_token, session_id, log_level) -> MCPTaskStore.update_status_message() -> TaskRecord.statusMessage 2025-* GET /mcp: notifications/progress + notifications/message | 2026-07-28 subscriptions/listen: excludes progress

Installing litestar-mcp[queues]

Install the queues extra to unlock litestar_mcp.queues (QueueTaskExecutor, DurableWorkflowEngine, WorkflowStore, TaskDependencyChain, QueuedAgentChatController, QueuedTurnManager, AgentRegistry, and PrincipalSnapshot):

uv add "litestar-mcp[queues]"
pip install "litestar-mcp[queues]"
pdm add "litestar-mcp[queues]"
poetry add "litestar-mcp[queues]"

Guides in This Section

MCP Tasks Extension

Configure MCPTaskConfig, declare task-capable tools, drive the working / input_required state machine with MCPInputRequiredResult, and handle SEP-2663 tool errors.

MCP Tasks Guide →

Distributed Task Queues

Auto-wire QueueTaskExecutor with QueuePlugin, override queues per controller or tool, rehydrate SecurityContext and Dishka dependencies in workers, and resume across replicas.

Distributed Queues →

Durable DAG Workflows

Build DynamicWorkflow and WorkflowNode pipelines, compare WorkflowEngine with DurableWorkflowEngine, and inspect single-writer WorkflowStore keys and watchdog recovery.

Durable Workflows →

Queued Agent Turns

Offload multi-step AgentRuntime turns to background workers via QueuedAgentChatController and QueuedTurnManager with deterministic SSE frame replay over ChannelsPlugin.

Queued Agent Turns →

Multi-Replica Deployment

Production checklist for horizontal scaling: stateless Streamable HTTP routing, shared StoreRegistry, cross-worker ChannelsPlugin, external worker placement, and conformance testing.

Deployment Checklist →