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):
- Single-Process In-Memory Execution: Built into the base
litestar-mcppackage with zero external infrastructure dependencies. Ideal for local development,litestar mcp stdioservers, and single-replica deployments. - Distributed Queue Execution (
litestar-mcp[queues]): Powered bylitestar-queues, Litestar'sStoreRegistry, andChannelsPlugin. 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) |
InProcessTaskExecutor (process-local asyncio.Task runners in 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 InProcessTaskExecutor:
Installing litestar-mcp[queues]¶
Install the queues extra to unlock litestar_mcp.queues (QueueTaskExecutor, DurableWorkflowEngine, WorkflowStore, TaskDependencyChain, QueuedAgentChatController, QueuedTurnManager, AgentRegistry, and PrincipalSnapshot):
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.
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.
Durable DAG Workflows
Build DynamicWorkflow and WorkflowNode pipelines, compare WorkflowEngine with DurableWorkflowEngine, and inspect single-writer WorkflowStore keys and watchdog recovery.
Queued Agent Turns
Offload multi-step AgentRuntime turns to background workers via QueuedAgentChatController and QueuedTurnManager with deterministic SSE frame replay over ChannelsPlugin.
Multi-Replica Deployment
Production checklist for horizontal scaling: stateless Streamable HTTP routing, shared StoreRegistry, cross-worker ChannelsPlugin, external worker placement, and conformance testing.