Temporal provides durable execution for your agent workflows, enabling automatic retries, pause/resume capabilities, and time-travel debugging. Perfect for production deployments.
Why Temporal?
mcp-agent supports bothasyncio and temporal execution engines. While asyncio works great for development and simple workflows, Temporal is recommended for production deployments because it provides:
Durable Execution
Workflows survive failures, restarts, and infrastructure issues
Automatic Retries
Failed activities are automatically retried with configurable policies
Pause & Resume
Workflows can be paused indefinitely and resumed with new data
Observability
Complete workflow history and time-travel debugging via Temporal UI
Scalability
Distribute workflow execution across multiple workers
Long-Running Workflows
Support for workflows that run for days, weeks, or months
Quick Start
1
Install Temporal CLI
Install the Temporal CLI for local development:
2
Start Temporal Server
Run a local Temporal server for development:This starts:
- Temporal Server on
localhost:7233 - Web UI on
http://localhost:8233
3
Configure mcp-agent
Update your
mcp_agent.config.yaml:mcp-agent preloads its built-in LLM providers automatically. Add extra modules
when you register custom @workflow_task activities outside the core packages so
the worker can discover them before starting. Entries are standard Python import paths.
The optional workflow_task_retry_policies mapping lets you tune Temporal retry
behaviour per activity (supports exact names, wildcards like prefix*, or *).
For provider SDKs, common non-retryable error types include:- OpenAI/Azure OpenAI:
AuthenticationError,PermissionDeniedError,BadRequestError,NotFoundError,UnprocessableEntityError. - Anthropic:
AuthenticationError,PermissionDeniedError,BadRequestError,NotFoundError,UnprocessableEntityError. - Azure AI Inference:
HttpResponseError(400/401/403/404/422). - Google GenAI:
InvalidArgument,FailedPrecondition,PermissionDenied,NotFound,Unauthenticated. mcp-agent raises aWorkflowApplicationErrorfor these cases so Temporal (or the asyncio executor) avoids retry loops even when the Temporal SDK is not installed locally.
4
Create Worker
Create a worker to process workflows:
worker.py
5
Run Workflow
Execute your workflow:
main.py
Temporal Architecture
Core Components
Temporal’s architecture provides robust workflow orchestration through several key components:Temporal Server
Manages workflow state, persists event history, and coordinates execution
Workers
Execute workflow and activity code, poll for tasks from the server
Event Store
Immutable log of all workflow events, enabling replay and fault tolerance
Task Queues
Distribute work between server and workers, enabling load balancing
Benefits of Temporal Architecture
Durability & Fault Tolerance: Temporal’s event sourcing model ensures that every workflow step is persisted. If a worker crashes, another worker can pick up where it left off by replaying the event history.Activity vs Workflow Distinction
Workflows are orchestration logic that must be deterministic:- No direct I/O operations
- No random number generation without seeds
- No current time checks (use
workflow.now()) - Pure coordination and decision making
- External API calls
- Database operations
- File I/O
- Any side effects
Advanced Workflow Features
Signal and Query Handlers
Signals allow external systems to communicate with running workflows:Workflow Versioning
Handle workflow updates without breaking running instances:Workflow Timeouts and Cancellation
Configure comprehensive timeout policies:Core Concepts
Workflow Definition
Temporal workflows are defined the same way as asyncio workflows:Signals for Human-in-the-Loop
Implement workflows that wait for human input:Long-Running Workflows
Handle workflows that run for extended periods:Advanced Patterns
Parallel Agent Execution
Run multiple agents in parallel with Temporal:Workflow Composition
Compose complex workflows from simpler ones:Error Handling with Compensations
Implement saga pattern for distributed transactions:Production Deployment
Infrastructure Requirements
Minimum Production Setup:- Temporal Server cluster (3+ nodes for HA)
- PostgreSQL/MySQL database with replication
- Elasticsearch for visibility (optional but recommended)
- Load balancer for Temporal frontend
- Monitoring stack (Prometheus, Grafana)
High Availability Configuration
Configure Temporal for production resilience:Temporal Cloud
For production, use Temporal Cloud:mcp_agent.config.yaml
Security Best Practices
Data Encryption:Worker Scaling
Scale workers for production workloads:Monitoring and Observability
Monitor workflows with Temporal UI and custom metrics:Debugging
Temporal Web UI
Access the Temporal Web UI athttp://localhost:8233 to:
- View all workflow executions
- Inspect workflow history step-by-step
- See pending activities and their retry attempts
- Send signals and queries to running workflows
- Download workflow history for offline debugging
- Monitor worker health and task queues
Workflow Replay
Debug production issues by replaying workflow history:Testing with Time Skipping
Test long-running workflows efficiently:Migration Guide
From Asyncio to Temporal
Your workflow code remains largely the same. Here’s what changes:Running Workflows
Best Practices
Use Deterministic Code
Use Deterministic Code
Workflows must be deterministic. Avoid:
- Random number generation without seeds
- Current time checks (use
workflow.now()) - Direct I/O operations (use activities)
- Non-deterministic data structures
Set Appropriate Timeouts
Set Appropriate Timeouts
Configure timeouts for workflows and activities:
Use Workflow IDs
Use Workflow IDs
Set meaningful workflow IDs for idempotency:
Handle Versioning
Handle Versioning
Version your workflows for safe updates:
Common Patterns
Polling External Systems
Scheduled Workflows
Examples
Explore complete Temporal examples:Basic Temporal Example
Simple workflows with Temporal
MCP Agent Server (Temporal)
Durable agent server implementation
Parallel Workflow
Fan-out/fan-in pattern
Orchestrator Workflow
Complex orchestration with Temporal
Next Steps
Deploy to Cloud
Deploy Temporal workflows to MCP Agent Cloud
Workflow Patterns
Explore workflow patterns with Temporal
Temporal Documentation
Deep dive into Temporal concepts
Production Guide
Temporal production deployment guide
